My first IoT project was collecting data from various sensors in a hydroponics farm. I always puzzled over how to set up the device communication. Initially, I used HTTP to send the data to a web application through a POST request. The web application would store the data in a database. It would also send out e-mail notifications if the temperature was too high or if the soil moisture was too low.
Over time I had to scale the project to add more sensors & also create a two-way communication between the web server and the devices in the farm. Using HTTP did not make any sense so I changed to socket programming using TCP/IP. But socket programming meant writing a lot of code and making changes to it was a pain.
Finally, I found MQTT. It too runs over TCP/IP but its an out of the box solution with all the necessary functions. It has client libraries in all programming languages so you just need a device connected to the internet to use it.
Here I will explain how MQTT works, its features & how to implement it in Python.
1. What is MQTT?
In short, MQTT(Message Queuing Telemetry Transport) is a light-weight messaging protocol used in IoT for sending messages to & from devices. It runs over TCP/IP networks. There are many other messaging protocols that can be used for IoT but MQTT is fast becoming the most popular protocol because of its simplicity & ease-of-use. The latest version of MQTT is v5 but v3.1.1 & v3.1 are still the most commonly used.
2. How does MQTT Work?
MQTT works through what is called a publish/subscribe model. An MQTT setup has 2 main components:
Broker: A broker is a central hub or server that is responsible for all the connections between clients & storing of all the messages.
Client: A client is a device that connects to the broker and can publish & receive messages.
The MQTT broker is like a radio wave and the MQTT clients are the devices that broadcast & receive messages through that wave.
3. So how does the messaging actually work?
The clients that are connected to the broker can communicate with each other through topics.
A topic is like a radio frequency. When you broadcast a message to a certain frequency, anyone tuned-in to that frequency will receive that message. In MQTT, broadcasting is “Publishing” & tuning-in is “Subscribing”. You can use words as topics. For example:TemperatureSensor, Kitchen/Lights
If I have 2 clients connected to a broker and if I want to send messages between the clients, I have to publish the message to a topic name say “Lights” from one client. The other client that is supposed to receive the message has to be subscribed to that same topic “Lights”.
4. Advantages of MQTT for IoT over HTTP & UDP
- MQTT is very lightweight. The message size is very small in comparison to HTTP. HTTP a lot of extra, unnecessary fields that is sent in the request & response messages that is not required for IoT.
- MQTT is reliable. UDP is a a connectionless protocol as compared to TCP/IP which is connection oriented. UDP based protocols like MQTT-SN & CoAP are faster in sending messages but they are not reliable as the messages being sent to the broker are not acknowledged. So its difficult to know if the messages are being sent are not.
- MQTT is scalable. Although its a bit tricky, it can be scaled to handle millions of messages.
- The big cloud providers use MQTT. AWS, Azure, OpenStack have IoT modules which support MQTT messaging.
This doesn’t mean that MQTT is the best in every scenario. If you have to compromise reliability for speed then UDP based protocols like CoAP, may be better.
5. Getting Started With MQTT
If you want to jump right into programming MQTT clients, here is a tutorial of using Paho MQTT in Python:
I have explained other topics in MQTT in this guide:
Guide to Paho Python MQTT Client