MQTT Publish, Subscribe and QoS
Model topics, retained state, QoS and Last Will using a lab broker or the local simulator.
Topology and objective
Sensor publisher → MQTT broker → dashboard subscriber; command topic returns to device.
Hardware and tools
- Supported development board
- USB data cable
- Current-limited supply
- Serial monitor and logic analyser when applicable
Security and RF safety
- Use TLS for untrusted networks.nUse per-device credentials and topic ACLs.nNever expose an unauthenticated broker to the internet.
Practical procedure
- Design device-specific telemetry and command topics.nConnect with a unique client ID.nPublish JSON telemetry at QoS 0.nSubscribe to a command topic.nTest retained state and Last Will offline status.
Telemetry / payload
{"device_id":"esp32-01","ts":1724659200,"temperature":25.2,"online":true}Starter code / config
telemetry: tcs/lab/esp32-01/telemetryncommands: tcs/lab/esp32-01/commandnstatus: tcs/lab/esp32-01/statusExpected results
| Checkpoint | Expected | Tool |
|---|---|---|
| Publish | Subscriber receives valid JSON | MQTT clientnOffline |
Troubleshooting
| Symptom | Check | Action |
|---|---|---|
| Connection refused | Verify host, port, TLS and credentials | Correct one setting at a timenDuplicate messages |
Local-only sandbox
Commands & telemetry simulator
Send command
Simulator ready. No network connection is used.
