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,smarthomeorindustrial_control).device_id: Unique ESP32 identifier (for example,esp32_001orgateway_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.
| Purpose | Example topic | Payload | Notes |
|---|---|---|---|
| 1) Request a key | smart/connect | JSON: {"device_id": "esp32_ID"} | Sends device information to the server to obtain a key and connect. Shared by all devices. |
| 2) Disconnect | smart/esp32_ID/disconnect | JSON: {"key": "..."} | Disconnects from the server. |
| 3) Device status | smart/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 status | smart/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 image | smart/esp32_ID/input/camera | JSON IMAGE: {"uid": "...", "key": "...", "image": "..."} | Sends image data; uid must match the UID in the corresponding input status message. |
| 6) Key status | smart/esp32_ID/key/status | success:{"success": true, "data":{"key": "..."}}error:{"success": false, "data": {"key": "...","errorCode":..., "message": ...}} | |
| 7) Camera status | smart/esp32_ID/camera/status | success:{"success": true, "data":{"key": ...,"ip": ..., "port": ..., "uri": ..., "user": ..., "pass": ..., "code": 0/1/2/3}}error:{"success": false, "data": {"key": "...","errorCode":..., "message": ...}} | |
| 8) Phone update status | smart/esp32_ID/phones/status | success:{"success": true, "data":{"key": "...","phones":"..."}}error:{"success": false, "data": {"key": "...","errorCode":..., "message": ...}} | |
| 9) Firmware status | smart/esp32_ID/firmware/status | success:{"success": true, "data":{"key": "...","fw_version": "..."}}error:{"success": false, "data": {"key": "...","errorCode":..., "message": ...}} | |
| 10) Test input status | smart/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.
| Purpose | Example topic | Payload | Notes |
|---|---|---|---|
| 1) Receive a key | smart/esp32_ID/key | {"key":"...","name":"...","address":"..."} | Key used by the device to communicate with the server. |
| 2) Control output | smart/esp32_ID/control | OUTPUT:{"code": 0/1/2/3,"state": 0/1} | Output control command; code identifies the output. |
| 3) Restart command | smart/esp32_ID/cmd/restart | {"key": ...,"state": 0/1} | Server request to restart the device. |
| 4) Configure camera | smart/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 device | smart/esp32_ID/ping | {"key": "..."} | |
| 6) Set phone numbers | smart/esp32_ID/phones | {"key":"...","phones":"sdt1,sdt2,sdt3,..."} | |
| 7) Input acknowledgment | smart/esp32_ID/input/status | {"uid":"...","code":...,"status":true/false } | |
| 8) Set Wi-Fi | smart/esp32_ID/wifi | {"key":"...","ssid":"...","pass":"..."} | |
| 9) New firmware notification | smart/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_esp32uses 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_esp32SUBSCRIBES to this topic, receives a value such as180seconds, 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.
- Capture: The ESP32 retrieves an image, usually JPEG, from the camera using Digest Auth.
- Encode: Convert raw image bytes to Base64 for the MQTT payload.
- Publish: The device PUBLISHES the Base64 string to
smart/notice_device_esp32/data/camera. - Process: The server decodes, stores, or displays the image.
- 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.
- The ESP32 uses an internal timer.
- Every interval
T(for example, 3 or 5 minutes), it PUBLISHES complete status tosmart/notice_device_esp32/status/device. - 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}tosmart/notice_device_esp32/config/interval(180 seconds = 3 minutes). - The ESP32 SUBSCRIBES to this topic and updates its internal
Tto 180 seconds. Its new reporting interval is then 3 minutes.