In this article, I will be discussing one of the most trending topics in IoT. I will take you through a beginner level tutorial on MQTT which is currently the most used protocol in IOT projects.
MQTT stands for Message Queueing Telemetry Transport Protocol. To put MQTT in a nutshell, it is “A lightweight event and message-oriented protocol allowing devices to asynchronously communicate efficiently across constrained networks to remote systems”. I know that this doesn’t really help much. So let’s try to decode that definition and understand what MQTT is and how to use it.
What is MQTT?
Again, for people who have no idea about MQTT, it is a protocol for machine-to-machine communication. It uses a publisher-subscriber model for communication. If you are from a programming background, you probably would have some knowledge about the publisher-subscriber model. Anyway, we will discuss the publisher-subscriber model and how MQTT works later in the tutorial.
MQTT over HTTP for IoT
Before going onto discuss how MQTT works, let’s first try to understand how it came to existence. MQTT came to exist as a replacement for HTTP because HTTP could not properly answer the challenges in IOT and M2M projects. Unlike web applications, IOT projects have some peculiar challenges. One of the main concerns is that IOT requires the event-driven paradigm. Some of the features of this event-driven paradigm are:
- Emitting information one-to-many
- Listening to events whenever they happen
- Distributing minimal packets of data in huge volumes
- Pushing information over unreliable networks
Some other challenges you face in an M2M application
- Volume (cost) of data being transmitted
- Power consumption
- Responsiveness
- Reliable delivery over fragile connections
- Security and privacy
- Scalability
MQTT was successfully able to cope with these challenges due to their features.
Why MQTT is good for M2M and IoT applications
MQTT has unique features you can hardly find in other protocols, like:
- It’s easy to implement in software as it is a lightweight protocol.
- MQTT is based on a messaging technique. This makes it faster in data transmission compared to its alternatives.
- It uses minimized data packets which results in low network usage.
- Low power usage. As a result, it saves the connected device’s battery.
- Most importantly it works on real-time which makes it ideal for IoT applications.
We learnt earlier that MQTT works through a publisher-subscriber model. In a P2S system, the publisher sends its messages to a topic. Then, every subscriber of that topic will receive the message. In MQTT, Broker handles the topic and messaging process while MQTT clients behave as publishers and subscribers.
Components of MQTT
To learn about how MQTT works, we have to understand some concepts in MQTT. The fundamental components of MQQT protocol are explained below.
Broker
The broker is a server that handles the communication and data transmission between the clients. It is responsible for the distribution, management and storage of data sent and retrieved by the clients. The broker acts like a centralized hub that regulates the message exchange.
In the case where a broker breaks down, the whole communication process breaks down as there is no way for the clients to communicate with each other directly. Therefore, the Broker Bridging mechanism was introduced to prevent such cases and build a fail-safe broker network.
There is a number of broker applications available on the internet including the popular ones; Mosquitto and HiveMQ or you can also use cloud-based brokers from cloud providers such as IBM or Azure.
Clients (Publisher, Subscriber)
These are basically the end-users who retrieve the data distributed by the broker. Each client is assigned a unique ID to identify themselves and the session when connected to the broker. A client could either be a publisher who publishes messages under a specific topic or a subscriber who receives messages relevant to a topic, at one time.
Message
These are the chunks of data sent and received by the clients. Each message consists of a command and a payload section. The command part determines the type of message and there are 14 message types available in MQQT.
Topic
This is the namespace or literally the topic that describes what the message is about. Each message gets assigned to a topic and clients can publish, subscribe or do both to a topic. The clients can also unsubscribe from a topic if they want to. MQTT topics are just strings with a hierarchical structure.
Assume that there is a topic called “home/kitchen”. We call home and kitchen as levels of the topic while home being topper level topic than the kitchen. Also, topics can use wild cards such as ‘+’ and ‘#’.
Publish
This is the process of clients (Publisher) sending data to the broker under a topic be distributed among the clients (Subscriber) who have requested data from the same topic.
Subscribe
This is the process of Clients (Subscribers) receiving data specific to a topic they have previously subscribed to, from the Clients (Publishers) through the broker.
QOS: Quality of Service
Each message is given an integer value from 0 to 2 to specify the delivery mode. This is known as Quality of Service. There are three different types of QOS.
- 0 (Fire and forget) – the message is delivered only once, acknowledgement not given, high-speed delivery method.
- 1 (Acknowledgement) – the message is delivered once or several times until an acknowledgement is received.
- 2 (Synchronized) – the message is delivered only once, guaranteed delivery, comparatively slower.
Practical use of MQTT
Its time to do some practical things here and get used to dealing with the MQTT protocol. As you learnt previously, there are many MQTT clients developed for each programming language. I will use Paho python MQTT client as I am a fan of Python and it is probably the best MQTT client out there.
First, you need a broker to create an application with MQTT. One of the most popular MQTT brokers is Mosquitto. You can install it with the following command.
sudo apt-get install mosquitto
We set up it to work on our localhost. By default, Mosquitto listens to port 1883. Next, install the MQTT client with pip command.
sudo apt-get install mosquitto
We setup it to work on our localhost. By default, Mosquitto listens to port 1883. Next, install the MQTT client with pip command.
pip install paho-mqtt
This command will install python MQTT client library on your machine. The core of the client library is the client class which provides all of the functions to publish messages and subscribe to topics.
There are several important methods in Paho MQTT client class which you should know:
- connect()
- disconnect()
- subscribe()
- unsubscribe()
- publish()
Each of these methods is associated with a callback.
Publishing a message
One of the main tasks you do with MQTT is publishing messages. A simple code that publishes a message usually has 4 steps.
- Import the paho.mqtt.client class
- Creating a client instance with Client() constructor
- Connecting to the server with connect() method
- Publish messages with publish() message.
import paho.mqtt.client as mqtt clientName = mqtt.Client("uniqueClientId) clientName.connect("localhost",1883,60) clientName.publish("TopicLevel1/test", "Your Message Here"); clientName.disconnect();
Most of the code is self-explaining. First, you create an instance of an MQTT client. Then you connect with the broker running on Localhost. Then the client publishes its message on “TopicLevel1/test” topic. After that, it disconnects from the broker.
Subscribing to a topic
You know that MQTT is not a one to one messaging protocol as it connects many devices. The trick here is that message from any device is assigned to a topic. Any devices that are subscribed to that topic will receive the message. Similarly, you can publish messages to Topics.
You can subscribe to a topic with subscribe() method in Client class. Subscribing to a topic has the same steps as to publishing messages. I am not going to repeat it as you can easily identify these steps from the code.
import paho.mqtt.client as mqtt def on_connect(subscriber, userdata, flags, rc): subscriber.subscribe("topic/test") def on_message(client, userdata, msg): if msg.payload.decode() == "Disconnect!": subscriber.disconnect() subscriber = mqtt.Client("subscribeeId") subscriber.connect("localhost",1883,60) subscriber.on_connect = on_connect subscriber.on_message = on_message subscriber.loop_forever()
In this application, the client works as a subscriber. It subscribes to the topic to the broker which in this case, runs on localhost. Whenever it receives a message, it calls for the on_message() method. If the received message is disconnected, it immediately disconnects from the broker. This is a very simple use of subscriber method. You can write more complicated logic using the same callback functions.
So in this article, you get a concise yet comprehensive idea about MQTT. Its’ time to move on to the conclusion to recall yourself as to what is the gist of the article.
Conclusion
MQTT is a lightweight, flexible and a simple but very efficient protocol that has a definite advantage over others when it comes to IoT and M2M solutions; considering its low bandwidth and low power consumption, response time and multiple usages. In conclusion, it could be said that MQTT is the best protocol so far when it comes to IOT development.
If you want to know more about MQTT, you can check the links below or have any questions, please reach out to me: bjorn.nostdahl@gunnebo.com
good info, but please change the background!
It creates a lot of disturbs to the sight when reading.
Switch to a static one!