If you have been working with IoT projects involving ESP32, Arduino, or Raspberry Pi, you have almost certainly come across the term MQTT. It appears in tutorials on Home Assistant, Node-RED, and virtually every connected device project. Yet many beginners skip past it without fully understanding what it is or why it matters. This guide explains MQTT from the ground up — what it is, how it works, and how to use it in your own IoT projects.
- Table of Contents
- What is MQTT?
- Why Use MQTT for IoT?
- MQTT vs HTTP: What is the Difference?
- How MQTT Works: Core Concepts
- MQTT in Practice: A Real-World Example
- Getting Started with MQTT on ESP32
- MQTT Security Best Practices
- Popular MQTT Tools and Clients
- Conclusion
- Further Reading and Official Resources
- Related Guides
Table of Contents
What is MQTT?
MQTT (Message Queuing Telemetry Transport) is a lightweight, open-standard messaging protocol designed specifically for constrained devices and low-bandwidth, high-latency networks. It was originally developed by IBM in the late 1990s for monitoring oil pipelines via satellite and has since become the de facto standard for IoT communication.
MQTT operates on a publish-subscribe model, meaning devices do not communicate directly with each other. Instead, they send messages to a central server called a broker, which then distributes those messages to any device that has subscribed to receive them.
MQTT is to IoT what HTTP is to the web — a foundational communication protocol that makes everything else possible.
Why Use MQTT for IoT?
IoT devices such as sensors and microcontrollers operate under very different constraints compared to standard computers. They typically have limited processing power, small amounts of memory, and may run on battery power for extended periods. MQTT was designed with these constraints in mind:
- Minimal bandwidth usage — MQTT headers are as small as 2 bytes, making it far more efficient than HTTP for frequent data transmissions.
- Low power consumption — Devices can sleep between transmissions and reconnect quickly, extending battery life significantly.
- Reliable delivery — MQTT supports three levels of Quality of Service to guarantee message delivery even on unreliable networks.
- Scalable architecture — A single broker can handle thousands of connected devices simultaneously.
- Persistent sessions — The broker can store messages for offline devices and deliver them when the device reconnects.
MQTT vs HTTP: What is the Difference?
| Feature | MQTT | HTTP |
|---|---|---|
| Architecture | Publish-Subscribe | Request-Response |
| Header Size | 2 bytes minimum | Hundreds of bytes |
| Connection | Persistent | New connection per request |
| Power Usage | Very low | Higher |
| Best For | Real-time sensor data, IoT devices | Web APIs, file transfers |
| Port | 1883 (standard), 8883 (TLS) | 80 (standard), 443 (HTTPS) |
How MQTT Works: Core Concepts
The Broker
The MQTT broker is the central server that receives all messages and routes them to the appropriate subscribers. Popular brokers include:
- Mosquitto — A lightweight, open-source broker ideal for self-hosting on a Raspberry Pi or local server.
- HiveMQ — A cloud-based broker with a free tier, widely used in production IoT deployments.
- EMQX — A high-performance, scalable broker suited to enterprise applications.
- broker.hivemq.com — A free public broker useful for testing and development.
Publishers and Subscribers
Any MQTT client can act as a publisher, a subscriber, or both simultaneously.
- A publisher sends a message to the broker on a specific topic. Example: an ESP32 publishes a temperature reading to
home/livingroom/temperature. - A subscriber tells the broker which topics it wants to receive. Example: a mobile app subscribes to
home/livingroom/temperatureand receives every update the ESP32 sends.
Topics
MQTT topics are hierarchical strings that act as message addresses, using forward slashes to create logical structure:
home/bedroom/temperature
home/bedroom/humidity
home/garage/door/status
factory/line1/machine3/rpm
farm/field2/soil/moisture
Subscribers can use wildcards to receive multiple topics at once. The + wildcard matches a single level (e.g. home/+/temperature), while # matches all levels below a point (e.g. home/# matches every topic starting with home/).
Quality of Service (QoS)
| QoS Level | Guarantee | Use Case |
|---|---|---|
| QoS 0 — At most once | No guarantee. Message may be lost. | Regular sensor readings |
| QoS 1 — At least once | Delivered at least once, may duplicate. | Alerts and commands |
| QoS 2 — Exactly once | Delivered exactly once, no duplicates. | Critical actuator triggers |
Retained Messages
When a publisher marks a message as retained, the broker stores it. Any new subscriber immediately receives that stored message upon connecting, rather than waiting for the next publish. This is useful for device status topics.
Last Will and Testament (LWT)
A client can define a Last Will and Testament message when connecting. If the client disconnects unexpectedly due to power loss or a network fault, the broker automatically publishes the LWT message, allowing other devices or dashboards to detect and respond to the disconnection.
MQTT in Practice: A Real-World Example
Consider a simple home monitoring system built with ESP32 boards:
- An ESP32 in the living room reads temperature from a DHT22 sensor and publishes to
home/livingroom/temperatureevery 30 seconds. - An ESP32 in the garage monitors a door sensor and publishes
openorclosedtohome/garage/dooron state change. - A Raspberry Pi running Mosquitto acts as the broker, receiving all messages.
- Home Assistant subscribes to all
home/#topics and displays live data on a dashboard. - A mobile app subscribes to
home/garage/doorand sends a push notification whenever the door opens.
All of this happens in real time, with minimal bandwidth, and the system continues to function reliably even if individual devices temporarily lose connectivity.
Getting Started with MQTT on ESP32
The most common way to use MQTT with an ESP32 is through the PubSubClient library in the Arduino IDE. Here is a minimal example that connects an ESP32 to a broker and publishes a sensor reading:
#include <WiFi.h>
#include <PubSubClient.h>
const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";
const char* broker = "broker.hivemq.com";
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) delay(500);
client.setServer(broker, 1883);
client.connect("ESP32_Client");
}
void loop() {
if (!client.connected()) client.connect("ESP32_Client");
client.publish("home/livingroom/temperature", "24.5");
delay(30000);
}
This sketch connects to Wi-Fi, establishes an MQTT connection to the HiveMQ public broker, and publishes a temperature value every 30 seconds.
MQTT Security Best Practices
A default MQTT setup transmits data in plain text with no authentication, which is acceptable for local development but unsuitable for any internet-facing deployment. Follow these practices when deploying MQTT in production:
- Use TLS encryption — Connect on port 8883 with TLS to encrypt all traffic between clients and the broker.
- Enable authentication — Configure the broker to require a username and password for all client connections.
- Use access control lists (ACLs) — Restrict each client to only the topics it legitimately needs to access.
- Self-host your broker — For sensitive data, run Mosquitto on a private server rather than using a public broker.
- Rotate credentials regularly — Update passwords and certificates on a scheduled basis.
Popular MQTT Tools and Clients
- MQTT Explorer — A desktop GUI client for browsing, publishing, and subscribing to topics. Excellent for debugging.
- MQTTX — A modern, cross-platform MQTT desktop and CLI client with a clean interface.
- Node-RED — A flow-based visual programming tool with built-in MQTT nodes, ideal for building IoT dashboards and automations.
- Home Assistant — Integrates natively with MQTT for smart home device management.
- Mosquitto CLI — Command-line tools for quick publishing and subscribing directly from a terminal.
Conclusion
MQTT is one of the most important MQTT IoT protocols in the ecosystem. Understanding it opens the door to building robust, scalable, and efficient connected systems. Whether you are building a simple home sensor network or a multi-device industrial monitoring platform, MQTT provides the reliable and lightweight communication layer your project needs.
Once you are comfortable with the basics covered in this guide, the natural next steps are setting up your own Mosquitto broker on a Raspberry Pi, integrating MQTT with Node-RED for visual automation, and connecting your ESP32 projects to Home Assistant for a fully featured smart home dashboard.
Further Reading and Official Resources
- MQTT.org — The official MQTT protocol specification and FAQ.
- Eclipse Mosquitto — Official documentation for the open-source Mosquitto MQTT broker.
- HiveMQ MQTT Essentials — A comprehensive series covering every aspect of the MQTT protocol in depth.
- Node-RED — Official documentation for the flow-based IoT automation platform.
Related Guides
- What is IoT? A Beginner’s Guide to the Internet of Things
- Arduino for Beginners: A Complete Getting Started Guide
- ESP32 Getting Started Guide: Setup, Pinout and First Projects
- Top 5 Development Boards for IoT Projects in 2026
- ESP32 MQTT Tutorial: Publish and Subscribe with HiveMQ
- ESP32 Firebase Realtime Database Tutorial