06 MQTT - The Standard For IoT Messaging
06 MQTT - The Standard For IoT Messaging
Subscriber #1
Publish Topic
Subscriber #2
Topic
Publisher Subscriber #3
Broker
MQTT: Formal Introduction
▪ And just as functioning of the heart is critical for the human body, a
reliable and performant MQTT broker is critical for IoT operations.
▪ The MQTT broker receives the data from the senders, filters the data
packets, and forwards them to the receiving clients.
MQTT: Publishers & Subscribers
▪ Messages are the data that you want to exchange between your devices.
▪ For example, a message can be a command or data like sensor readings.
MQTT: Topics
▪ A topic is the way you register interest for incoming messages or how you
specify where you want to publish the message.
MQTT: Functionality
▪ First, the publisher sends the data collected to the broker on a particular
topic, which is similar to a channel for data transmission.
▪ Please note that a topic can have several subtopics too.
▪ For example, in an application where you send the temperature data from
a sensor connected to your fridge, the topic will look something like this:
Kitchen/Fridge
▪ The main topic is the kitchen, and the Fridge is the subtopic.
▪ The message will be Temperature:14 on the given topic.
MQTT: Functionality
Highly Secure
▪ MQTT makes it easy to encrypt messages.
▪ The standard unsecured port is 1883.
▪ The default secured MQTT broker port is 8883.
▪ The use of ACLs (Access Control Lists) allows restriction of subscriptions
and publishing of clients.
MQTT: Features
Highly Scalable
▪ There is no worry about maintaining clients’ addresses or IDs.
▪ It is effortless to expand the MQTT network.
▪ The only things required are the broker’s IP address and the topic name.
Reliability
▪ MQTT is highly reliable when it comes to message delivery.
▪ MQTT comes with three predefined quality of service:
QoS 0: At most once
QoS 1: At least once
QoS 2: Exactly once
MQTT: Quality of Service (QoS)
▪ In QoS Level 0 (Fire and Forget Level), messages are sent without any
confirmation from the receiver.
▪ This means it is technically possible for a message to get lost, given an
unreliable connection.
MQTT: QoS Level 1
▪ Click Next.
Mosquitto Broker: Installation on Windows
▪ Click Next.
Mosquitto Broker: Installation on Windows
▪ MQTT clients typically connect to the broker on port 1883, which is the
default port for unencrypted MQTT communication.
▪ When allow_anonymous is set to true, clients can connect without
providing a username or password.
Mosquitto Broker: Starting the Broker
▪ Open CMD window and write ipconfig to get the broker IP.
Paho MQTT Python Library
▪ The Paho Python Client provides a client class with support for MQTT.
▪ It provides a simple API for working with MQTT, allowing developers to
easily integrate MQTT functionality into their Python applications.
▪ “Paho” means “communicate with everyone”.
▪ You can install the Paho Python Client using the following pip command:
pip install paho-mqtt==1.6.1
Paho MQTT Python Library: Simple Publisher and Subscriber
# Connect to the MQTT broker using the specified IP address and port
client.connect(broker_ip, port)
# Connect to the MQTT broker using the specified IP address and port
client.connect(broker_address, port)
NodeMCU ESP32
PubSubClient Library: Installation on Arduino IDE
void setup() {
Serial.begin(115200); // Initialize serial communication at baudrate of 115200
void loop() {
const char* message = "Turn On"; // The message to be published
client.publish(topic, message); // Publish the message to the specified topic
Serial.print("Published message: "); // A message prefix
Serial.println(message); // Print the published message
for (int i = 0; i < length; i++) // Loop through the message bytes
Serial.print((char)message[i]); // Print each character to the Serial Monitor
void loop() {
client.loop(); // Start MQTT client loop to receive messages
}
NodeMCU as Subscriber: Python Output
NodeMCU as Subscriber: NodeMCU Output
References and Tutorials