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 Communication and Cloud
Introduction to IoT Device Security for Students
Why IoT devices are attractive targets, the weaknesses that cause most incidents, and the practical habits that prevent them.
IoT Communication and Cloud
Wi-Fi vs Bluetooth vs LoRaWAN: Choosing an IoT Network
Range, power, data rate and infrastructure decide which wireless technology fits an IoT project. Compare Wi-Fi, Bluetooth Low Energy and LoRaWAN.
Raspberry Pi and Python
Raspberry Pi vs ESP32: Which Should You Choose for IoT?
Raspberry Pi is a Linux computer; ESP32 is a microcontroller. See when each is the right tool for an IoT project and when to use both.
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.
Learning from your city?
City guides show how online IoT training works from where you are, with project themes for your area.