MQTT Explained with Examples
MQTT (Message Queuing Telemetry Transport) is a lightweight publish/subscribe messaging protocol designed for constrained devices and unreliable networks. Devices publish messages to topics on a broker, and any client subscribed to a matching topic receives them.
The core idea: broker, topics, clients
- Broker: the server that receives all messages and forwards them to subscribers (for example Mosquitto).
- Client: any device or app that connects to the broker, publishing, subscribing or both.
- Topic: a hierarchical string such as
farm/field1/soil/moisturethat labels a message.
Publishers and subscribers do not know about each other, which makes it easy to add a dashboard or a logger without changing the device.
Example flow
| Who | Action | Topic | Payload |
|---|---|---|---|
| ESP32 sensor | Publishes | home/livingroom/temp |
27.4 |
| Dashboard | Subscribes | home/+/temp |
receives 27.4 |
| Phone app | Publishes | home/livingroom/fan/set |
ON |
| ESP32 relay | Subscribes | home/livingroom/fan/set |
switches the fan |
Wildcards: + matches one level (home/+/temp), # matches all remaining levels (home/#).
Quality of Service (QoS)
| QoS | Meaning | Use when |
|---|---|---|
| 0 | At most once; no acknowledgement | Frequent readings where a miss is harmless |
| 1 | At least once; duplicates possible | Commands and alerts that must arrive |
| 2 | Exactly once; slowest | Rare cases where duplicates are harmful |
Retained messages and Last Will
A retained message is stored by the broker and sent to new subscribers immediately, so a dashboard shows the last known value as soon as it connects. A Last Will and Testament is a message the broker publishes if a client disconnects unexpectedly, which is the standard way to detect that a device went offline.
Security basics
- Use authentication and per-client access control lists.
- Use TLS (port 8883 is the registered port for MQTT over TLS; plain MQTT uses 1883).
- Never expose an unauthenticated broker to the internet.
Minimal ESP32 publish example
#include <WiFi.h>
#include <PubSubClient.h>
WiFiClient net;
PubSubClient mqtt(net);
void setup() {
WiFi.begin("YOUR_WIFI", "YOUR_PASSWORD");
while (WiFi.status() != WL_CONNECTED) delay(500);
mqtt.setServer("broker.local", 1883);
}
void loop() {
if (!mqtt.connected()) mqtt.connect("esp32-node-01");
mqtt.loop();
mqtt.publish("home/livingroom/temp", "27.4");
delay(10000);
}
In real projects add reconnection back-off, a Last Will and credentials. The MQTT and Node-RED course and the ESP32 course cover these patterns. Compare protocols in MQTT vs HTTP for IoT.
References
- OASIS, MQTT Version 5.0: docs.oasis-open.org
- MQTT.org: mqtt.org
- Eclipse Mosquitto broker: mosquitto.org
Related reading
IoT Fundamentals
IoT Learning Roadmap for Engineering Students: What to Learn and When
A semester-by-semester IoT roadmap for engineering and diploma students, from electronics basics to a documented project.
IoT Projects
IoT Project Ideas for Students by Skill Level
Fifteen practical IoT project ideas for students, grouped by skill level, with the hardware, difficulty and what makes each project strong.
IoT Projects
Soil Moisture Monitoring with ESP32 and MQTT
Build a soil moisture monitor with ESP32 and a capacitive sensor: wiring, calibration, averaging, MQTT publishing and common mistakes.
Arduino and ESP32
Arduino Uno vs ESP32 for IoT Projects
Which board should you use for an IoT project? A practical comparison of Arduino Uno and ESP32 on connectivity, power, I/O and learning curve.
Learning from your city?
City guides show how online IoT training works from where you are, with project themes for your area.