MQTT
MQTT
MQTT operates on top of the TCP/IP protocol and uses a client-server model, where the
central component is the Broker, which manages the communication between the clients.
MQTT uses a publish-subscribe messaging pattern rather than the traditional client-
server model. Clients can either publish data to a topic or subscribe to a topic to
receive data.
This decouples the sender and receiver, which allows more flexible communication,
especially for devices with limited resources.
MQTT is designed to minimize the amount of data transmitted over the network. It
uses small message headers and simple packet formats, making it suitable for low-
power, low-bandwidth environments.
MQTT supports three levels of Quality of Service (QoS) to ensure message delivery:
4. Retained Messages:
1/9
MQTT allows a message to be retained by the broker, meaning that new subscribers
to a topic can receive the last retained message immediately upon subscribing.
MQTT supports a Last Will and Testament (LWT) feature that allows clients to send
a "last message" when they disconnect unexpectedly. This can be used to inform
other clients about the unexpected disconnection.
6. Keep-Alive Mechanism:
Components of MQTT:
1. Broker:
The central server that facilitates communication between MQTT clients. The broker
manages message routing, subscription handling, and message delivery according
to the QoS levels and topics.
2. Publisher:
A client that sends messages to a specific topic. Publishers do not need to know who
the subscribers are; they simply publish messages to the broker.
3. Subscriber:
A client that receives messages from a specific topic. Subscribers subscribe to topics
of interest to receive updates when messages are published to those topics.
2. Publish Message:
The broker routes the published message to all clients that are subscribed to that
topic.
2/9
Subscribers receive the message from the broker if they are subscribed to the topic
to which the message was published.
Fixed Header: Contains the message type (e.g., CONNECT, PUBLISH, SUBSCRIBE)
and flags.
Payload: Contains the actual data being sent (e.g., sensor readings, commands,
etc.).
Advantages of MQTT:
1. Low Bandwidth: MQTT uses minimal data and small message sizes, making it ideal for
networks with low bandwidth.
2. Reliable Delivery: With the QoS levels, MQTT can ensure message delivery even in
unreliable networks.
4. Scalability: MQTT can handle a large number of devices, making it suitable for large IoT
deployments.
5. Security: While MQTT itself does not specify security, it can be secured using TLS/SSL to
encrypt communication between clients and brokers.
3/9
MQTT Communication Diagram:
Here's a simple diagram illustrating the MQTT communication flow:
lua
+-----------------+ +-----------------+
| Publisher | Publish | Broker |
| (e.g., Sensor) | -------------------> | (Central Server)|
| | | |
+-----------------+ +-----------------+
^ |
| Subscribe |
| v
+-----------------+ +-----------------+
| Subscriber | Receive | |
| (e.g., Monitor)| <------------------- | |
| | | |
+-----------------+ +-----------------+
2. Broker: Receives the message and stores it temporarily, then sends it to any clients
subscribed to the /sensor/temperature topic.
3. Subscriber (Monitoring System): Receives the temperature data from the broker and
displays it.
Conclusion:
MQTT is an efficient and lightweight protocol suited for real-time communication,
particularly in IoT applications. It allows devices with limited resources to exchange
messages reliably, even in conditions where network reliability is low. Its publish-subscribe
model, combined with features like Quality of Service (QoS), retained messages, and last will,
makes it highly flexible and robust for a wide range of applications in industries like home
automation, smart cities, healthcare, and more.
4/9
Example of MQTT in Action
Let’s walk through a simple example of how MQTT works, particularly in the context of IoT,
using a temperature sensor as a Publisher and a monitoring system as a Subscriber.
Scenario:
Publisher (Sensor): A temperature sensor (like a DHT11 sensor) collects temperature
data and sends it to an MQTT broker.
MQTT Flow:
The sensor connects to the MQTT broker over TCP/IP. For example, the sensor might use
a device like an ESP8266/ESP32 microcontroller to connect to the broker.
The sensor’s MQTT client might be connected to a broker hosted on a cloud service (like
AWS, or a local broker like Mosquitto).
The temperature sensor reads a temperature value (e.g., 22°C ) and publishes this data
to a topic, say sensor/temperature .
Message Format:
Topic: sensor/temperature
Message: 22°C
python
5/9
import paho.mqtt.client as mqtt
import random
import time
The broker receives the message from the sensor (e.g., 22°C on sensor/temperature
topic).
The broker stores this message temporarily and checks if there are any clients that have
subscribed to the sensor/temperature topic.
When the subscriber connects to the broker, it sends a subscription request to the
broker for the topic sensor/temperature .
python
6/9
import paho.mqtt.client as mqtt
After subscribing, the monitoring system will receive any new temperature readings
published on the sensor/temperature topic.
The broker sends the last message published to the sensor/temperature topic to the
subscriber, and continues to forward any new messages as they come in.
For example:
Key Points:
Publisher (Sensor) sends temperature data (e.g., 22°C ) to the MQTT broker.
7/9
The Broker is responsible for routing messages between the publisher and subscriber.
Topic: sensor/temperature
Message: 22°C
xml
<sensorData>
<temperature>22°C</temperature>
<timestamp>2024-12-10T12:00:00Z</timestamp>
</sensorData>
The monitoring system, upon subscribing, will receive the same message.
8/9
Explanation of Diagram:
1. Publisher (Sensor): The sensor collects data and sends it to the broker.
2. Broker: The broker processes and stores the message, then forwards it to all clients that
have subscribed to the topic.
3. Subscriber (Monitoring System): The monitoring system receives the message from the
broker and processes it (e.g., displays it on a dashboard).
Conclusion:
In this example, MQTT allows the temperature sensor and the monitoring system to
communicate efficiently. The sensor can send updates about the temperature at regular
intervals, and the monitoring system can instantly receive those updates, even if the sensor
and the monitor are on different devices or networks. This makes MQTT highly effective for
real-time IoT applications.
9/9