Sitemap

Understanding MQTT: The Messaging Protocol Powering the Internet of Things

4 min readNov 19, 2025

--

The Internet of Things (IoT) is transforming the way devices communicate, from smart homes to industrial automation. At the heart of many IoT systems lies a communication protocol that is lightweight, fast, and reliable: MQTT (Message Queuing Telemetry Transport). In this article, we’ll explore MQTT, why it’s used, how it works, and its real-world applications.

Press enter or click to view image in full size

What is MQTT?

MQTT is a lightweight messaging protocol designed for low-power devices and unreliable networks. Developed in 1999 by Andy Stanford-Clark and Arlen Nipper, MQTT was specifically designed for situations where network bandwidth is limited, or devices have low processing power.

In simple terms, MQTT is the WhatsApp of IoT devices: it allows devices (sensors, microcontrollers, or edge devices) to send messages to each other in real time. Unlike traditional communication methods, MQTT is publish/subscribe-based, not request/response.

How MQTT Works

MQTT uses a broker-based architecture, which means all communication goes through a central server called a broker. There are three main components:

  1. Publisher — The device that sends data.
    Example: An ESP32 sensor sending temperature data.
  2. Subscriber — The device or system that receives data.
    Example: A mobile app displaying temperature readings.
  3. Broker — The middleman that receives messages from publishers and delivers them to subscribers.
    Example: HiveMQ, Mosquitto, or Adafruit IO.

Publish/Subscribe Model

Unlike HTTP’s request/response model, MQTT uses topics for communication:

  • Publisher sends messages to a topic
  • Subscriber listens to that topic
  • The broker delivers messages only to subscribers of that topic
Publisher: ESP32 publishes to topic "home/livingroom/temperature"
Subscriber: Mobile app subscribes to "home/livingroom/temperature"
Broker: Delivers message instantly

This architecture allows for scalable, real-time, and efficient communication.

MQTT vs HTTP: Why MQTT is Better for IoT

Both MQTT and HTTP can send data over the internet, but they are designed for different purposes:

Press enter or click to view image in full size

Analogy:

  • MQTT = WhatsApp chat (instant messages)
  • HTTP = Email (slower, request/response)

Key Features of MQTT

  1. Lightweight
    MQTT messages have minimal overhead, ideal for devices with low bandwidth.
  2. Quality of Service (QoS)
    MQTT supports three levels of delivery guarantee:
  • QoS 0: At most once
  • QoS 1: At least once
  • QoS 2: Exactly once
  1. Persistent Sessions
    Devices can reconnect and receive messages they missed while offline.
  2. Retained Messages
    The broker can store the last message for new subscribers.
  3. Last Will and Testament (LWT)
    If a device disconnects unexpectedly, the broker can notify subscribers.

Real-World Applications of MQTT

Smart Homes

  • Controlling lights, fans, and smart appliances
  • Motion sensors and security systems

Industrial IoT (IIoT)

  • Factory automation
  • Machine monitoring
  • Predictive maintenance

Agriculture

  • Soil moisture monitoring
  • Greenhouse temperature control
  • Irrigation automation

Healthcare

  • Wearable sensors
  • Remote patient monitoring

Smart Cities

  • Streetlights and parking sensors
  • Air quality monitoring

MQTT Brokers

To use MQTT, you need a broker. Popular brokers include:

  • Mosquitto — Open-source, lightweight
  • HiveMQ — Cloud and enterprise options
  • Adafruit IO — Beginner-friendly, supports MQTT and dashboards
  • EMQX — Enterprise-grade, scalable

Your choice depends on project scale, number of devices, and desired features.

ESP32 Example with MQTT

Here’s a short example of sending LDR data from an ESP32 to Adafruit IO using MQTT:

#include <WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#define WIFI_SSID "YourSSID"
#define WIFI_PASS "YourPassword"
#define AIO_SERVER "io.adafruit.com"
#define AIO_USERNAME "YourUsername"
#define AIO_KEY "YourAIOKey"

WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, 1883, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Publish ldrFeed = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/ldr");

void setup() {
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) { delay(500); }
Serial.println("WiFi Connected");
}

void loop() {
if (!mqtt.connected()) mqtt.connect();
mqtt.processPackets(1000);

int ldrValue = analogRead(34);
ldrFeed.publish(ldrValue);
delay(1000);
}

This code shows real-time sensor data delivery using MQTT.

Get Milan Achintha’s stories in your inbox

Join Medium for free to get updates from this writer.

MQTT is a lightweight, reliable, and real-time messaging protocol that powers most IoT systems today. Whether you’re building a smart home, industrial automation system, or remote sensor network, understanding MQTT is essential. Its combination of low bandwidth usage, reliability, and publish/subscribe model makes it superior to HTTP for real-time IoT applications.

For beginners, platforms like Adafruit IO, HiveMQ, or Mosquitto make MQTT accessible and easy to experiment with. Pairing MQTT with cloud storage like Firebase allows developers to achieve both instant communication and long-term data storage.

Takeaway:

  • MQTT → Instant communication, low power, scalable
  • HTTP / Firebase → Long-term storage, dashboards, simple logging
  • Best practice → Use MQTT for real-time control and optionally forward data to cloud storage for history

--

--

Milan Achintha
Milan Achintha

Written by Milan Achintha

Embedded systems & IoT enthusiast | Arduino, ESP32, PCB Design | Sharing projects, tutorials & real-world applications