Skip to main content

Protocol for basic alarm devices

MQTT topic structure​

Topics follow the pattern base_topic/device_id/category/information.

Where:

  • base_topic: Prefix shared by all devices (for example, smarthome or industrial_control).
  • device_id: Unique ESP32 identifier (for example, esp32_001 or gateway_A).

The following MQTT topics use notice_device_esp32 as the device name and describe the overall communication scheme.

The base topic pattern is smart/notice_device_esp32/category/information.


1. MQTT topics for esp32_ID​

A. Topics published by the device​

The esp32_ID device publishes data to these topics.

PurposeExample topicPayloadNotes
1) Request a keysmart/connectJSON: {"device_id": "esp32_ID"}Sends device information to the server to obtain a key and connect. Shared by all devices.
2) Disconnectsmart/esp32_ID/disconnectJSON: {"key": "..."}Disconnects from the server.
3) Device statussmart/esp32_ID/status/general{"key": "...","fourG": true/false, "pin": 100, "rssi": 100, "wifi": true/false}Overall status of the Power, 4G, Wi-Fi, Data, and signal indicators.
4) Input statussmart/esp32_ID/input{"uid": "...", "key": "...", "code":0/1/2/3}D1 button status; similarly for D2 through D6. Image data obtained with Digest Auth is also sent through this topic.
5) Send imagesmart/esp32_ID/input/cameraJSON IMAGE: {"uid": "...", "key": "...", "image": "..."}Sends image data; uid must match the UID in the corresponding input status message.
6) Key statussmart/esp32_ID/key/statussuccess:{"success": true, "data":{"key": "..."}}error:{"success": false, "data": {"key": "...","errorCode":..., "message": ...}}
7) Camera statussmart/esp32_ID/camera/statussuccess:{"success": true, "data":{"key": ...,"ip": ..., "port": ..., "uri": ..., "user": ..., "pass": ..., "code": 0/1/2/3}}error:{"success": false, "data": {"key": "...","errorCode":..., "message": ...}}
8) Phone update statussmart/esp32_ID/phones/statussuccess:{"success": true, "data":{"key": "...","phones":"..."}}error:{"success": false, "data": {"key": "...","errorCode":..., "message": ...}}
9) Firmware statussmart/esp32_ID/firmware/statussuccess:{"success": true, "data":{"key": "...","fw_version": "..."}}error:{"success": false, "data": {"key": "...","errorCode":..., "message": ...}}
10) Test input statussmart/esp32_ID/input/test{"uid": "...", "key": "...", "code":0/1/2/3}

B. Topics subscribed to by the device​

The esp32_ID device subscribes to these topics.

PurposeExample topicPayloadNotes
1) Receive a keysmart/esp32_ID/key{"key":"...","name":"...","address":"..."}Key used by the device to communicate with the server.
2) Control outputsmart/esp32_ID/controlOUTPUT:{"code": 0/1/2/3,"state": 0/1}Output control command; code identifies the output.
3) Restart commandsmart/esp32_ID/cmd/restart{"key": ...,"state": 0/1}Server request to restart the device.
4) Configure camerasmart/esp32_ID/input/setting{"key": ...,"ip": ..., "port": ..., "uri": ..., "user": ..., "pass": ..., "code": 0/1/2/3, "label":"..."}Sets the camera IP, port, URI, username, and password. code associates the camera with a device input.
5) Ping devicesmart/esp32_ID/ping{"key": "..."}
6) Set phone numberssmart/esp32_ID/phones{"key":"...","phones":"sdt1,sdt2,sdt3,..."}
7) Input acknowledgmentsmart/esp32_ID/input/status{"uid":"...","code":...,"status":true/false }
8) Set Wi-Fismart/esp32_ID/wifi{"key":"...","ssid":"...","pass":"..."}
9) New firmware notificationsmart/esp32_ID/firmware{"key":"...","fw_version": "...", "fw_title":"..."}

2. Communication mechanisms (parts 4 and 5)​

Bidirectional and periodic communication (requirement 5)​

Bidirectional communication uses PUBLISH topics for status and SUBSCRIBE topics for control commands.

  • Device telemetry: The ESP32 periodically, or when an event occurs (such as a button change), PUBLISHES to .../status/... and .../input/... topics.
  • Server commands: To control or configure the device, the server PUBLISHES to .../control/... and .../config/... topics. The ESP32 SUBSCRIBES to these topics and receives commands promptly.

Periodic heartbeat​

  • notice_device_esp32 uses an internal RTOS timer.
  • Every interval T (for example, 3 or 5 minutes), it PUBLISHES overall status to smart/notice_device_esp32/status/general.
  • This heartbeat tells the server that the device is still online and supplies updated status.

Set the interval from the server​

  • To change T, the server PUBLISHES to smart/notice_device_esp32/config/interval.
  • notice_device_esp32 SUBSCRIBES to this topic, receives a value such as 180 seconds, and updates its timer.
  • Note: Store the default interval (for example, 300 seconds) in ESP32 Flash/EEPROM so it survives restarts.

Image transmission (requirement 4)​

When transmitting images over MQTT, account for the broker's payload limit. In practice, keep images below a few hundred KB where possible.

  1. Capture: The ESP32 retrieves an image, usually JPEG, from the camera using Digest Auth.
  2. Encode: Convert raw image bytes to Base64 for the MQTT payload.
  3. Publish: The device PUBLISHES the Base64 string to smart/notice_device_esp32/data/camera.
  4. Process: The server decodes, stores, or displays the image.
  5. Quality of service: Use QoS 1 for important images when at-least-once delivery is required.

Periodic reporting and server configuration​

This mechanism supports device status monitoring and heartbeat messages.

  1. The ESP32 uses an internal timer.
  2. Every interval T (for example, 3 or 5 minutes), it PUBLISHES complete status to smart/notice_device_esp32/status/device.
  3. This heartbeat lets the server know the device is running and provides its latest status.
  • The ESP32 may start with a default interval, such as 300 seconds.
  • The server can PUBLISH {"interval_sec": 180} to smart/notice_device_esp32/config/interval (180 seconds = 3 minutes).
  • The ESP32 SUBSCRIBES to this topic and updates its internal T to 180 seconds. Its new reporting interval is then 3 minutes.