Skip to content

MQTT Communication

Overview

SMARTA L v2 uses MQTT for all remote telemetry, control, and OTA firmware updates. The device can connect to the broker via Wi-Fi or LTE, automatically failing over as needed. MQTT is managed using the PubSubClient library, with separate clients for Wi-Fi and LTE.

Connection and Will Message

  • Broker: smarta.ganarprojects.co.za (port 1883, user ..., password ...)
  • Client ID:
  • Wi-Fi: SMARTA_WIFI_<MAC>
  • LTE: SMARTA_LTE_<MAC>
  • Will Topic:
  • Wi-Fi: smarta/<MAC>/will_wifi
  • LTE: smarta/<MAC>/will_modem
  • Will Payload: JSON with state, time, pump_state, and level (e.g., { "state": "OFFLINE", "pump_state": 1, "level": 35 })
  • Subscriptions:
  • All device-specific topics: smarta/<MAC>/+
  • OTA topics: ota/<MAC>/#
  • Will topic (to resume pump state after reconnect)

Published Topics and Data

All topics are under smarta/<MAC>/ (where <MAC> is the device's unique 12-digit hex ID, no colons).

  • level: Current tank level (integer)
  • min_val, max_val: Configured min/max tank levels
  • current: Current sensor reading
  • state_value_byte: Error/status byte
  • pump_state: 1 if pump is running, 0 otherwise
  • last_update: Seconds since last sensor update
  • midpoint, noise: Sensor calibration/debug
  • pump_timer: Pump runtime in seconds
  • model: Always SMARTA-L
  • version: Firmware version
  • sim_present: 1 if SIM detected, 0 otherwise
  • sending_mqtt_via: wifi or modem
  • name: Device name
  • lte_connected: 1 if LTE is up
  • wifi_signal_strength: Wi-Fi RSSI
  • config: JSON with sensor_type, raw_zero, raw_max, height
  • ble_rssi, ble_data, ble_data_raw: BLE sensor data
  • filling: 1 if pump is running
  • debug: Debug messages
  • new_apn, new_min, new_max, new_lcgt, new_pushtoken, mute, new_level_known, new_raw_known, new_mac, new_raw_zero, restart_reason, status: Various config/status changes

Subscribed Topics and Commands

  • OTA:
  • ota/<MAC>/start: Begin OTA update (payload: firmware size in bytes)
  • ota/<MAC>/data: Firmware binary chunk
  • ota/<MAC>/cancel: Abort OTA update
  • Will Topic: Used to resume pump state after reconnect
  • Device Commands: (topic: smarta/<MAC>/<command>)
  • PU: Start pump
  • ET: Stop pump
  • RE: Reboot device
  • HOTSPOT: Reset Wi-Fi credentials to default
  • CE: Clear EEPROM and reboot
  • AT: Send AT command to modem (payload: command string)
  • RAW_ZERO, SENSOR_TYPE, COMMS_LOSS_MAX, WIFI_ONLY, LEVEL_KNOWN, RAW_KNOWN, LCGT: Update various settings (payload: value)

OTA Firmware Update via MQTT

  • Use the provided pushOTA.sh script to send firmware updates over MQTT.
  • The device suspends all main tasks during OTA, writes the firmware in chunks, and reboots on completion.
  • OTA progress and errors are shown on the OLED display.

Message Handling and Callbacks

  • wifiCallback and modemCallback handle all incoming MQTT messages, including OTA, will, and device commands.
  • handleMessage parses the command type and payload, updating settings, triggering actions, or rebooting as needed.
  • All settings changes are persisted to NVS (Preferences).

Configuration Parameters

  • MQTT server, port, user, and password are set in config.h.
  • Device ID is derived from the ESP32 MAC address.
  • Most settings (min/max level, APN, sensor calibration, etc.) are stored in NVS and can be updated via MQTT, BLE, or button interface.

Example Topic Map

Direction Topic Pattern Description
Publish smarta//level Current tank level
Publish smarta//config Sensor config JSON
Subscribe smarta//PU Start pump
Subscribe ota//start Begin OTA update
Subscribe smarta//RE Reboot device
... ... ...

References

  • MQTT logic: MQTT.ino
  • Main integration: main.ino, SMARTA_L_v2.ino
  • OTA script: pushOTA.sh
  • Configuration: config.h

This document is based on the actual firmware source and reflects the real MQTT implementation for SMARTA L v2.