MQTT protocol is a Machine to Machine (M2M) protocol widely used in IoT (Internet of things).
The MQTT protocol is a message based protocol, extremely light-weight and for this reason, it is adopted in IoT. Almost all IoT platforms support MQTT to send and receive data from smart objects.
This tutorial provides an in-depth MQTT tutorial covering:
- how MQTT works
- the MQTT messages
- how to use it in IoT projects
- how to configure Mosquitto MQTT to handle the MQTT protocol
There are several implementations for different IoT boards like Arduino, Raspberry, ESP32, ESP8266 and so on.
There are other IoT protocols used to implement IoT projects but MQTT is one of the most efficient.
MQTT Protocol Technical description
The MQTT IoT protocol was developed around 1999. The main goal of this protocol was to create a protocol very efficient from the bandwidth point of view. Moreover, it is a very power-saving protocol. For all these reasons, it is suitable for IoT.
This uses the publish-subscribe paradigm in contrast to HTTP based on the request/response paradigm. It uses binary messages to exchange information with low overhead. It is very simple to implement and it is open. All these aspects contribute to its large adoption in IoT. Another interesting aspect is the fact that the MQTT protocol uses TCP stack as a transmission substrate.
The role of the MQTT Broker and MQTT Client
As said before, the MQTT protocol implements a publish-subscribe paradigm. This paradigm decouples a client that publishes a message (“publisher”) to other clients that receive the message (“subscribers”). Moreover, MQTT is an asynchronous protocol, which means that it does not block the client while it waits for the message.
In contrast to the HTTP protocol, that is a mainly asynchronous protocol. Another interesting property of MQTT protocol is that it does not require that the client (“subscriber”) and the publisher are connected at the same time.
The key component in MQTT is the MQTT broker. The main task of MQTT broker is dispatching messages to the MQTT clients (“subscribers”). In other words, the MQTT broker receives messages from the publisher and dispatches these messages to the subscribers.
The MQTT topic
While it dispatches messages, the MQTT broker uses the topic to filter the MQTT clients that will receive the message. The topic is a string and it is possible to combine the topics creating topic levels.
A topic is a virtual channel that connects a publisher to its subscribers. MQTT broker manages this topic. Through this virtual channel, the publisher is decoupled from the subscribers and the MQTT clients (publishers or subscribers) do not have to know each other to exchange data. This makes this protocol highly scalable without a direct dependency from the message producer (“publisher”) and the message consumer (“subscriber”).
The schema below describes the MQTT architecture:
As you may already know there are other IoT protocols that you can use in an IoT as CoAP, HTTP, and so on. If you don’t know other IoT protocols you could give a look at my article.
How to use Cayenne IoT ESP8266 + MQTT
ESP32 MQTT client: Publish and Subscribe
ESP8266 MQTT Client: Publish and Subscribe with Node-RED Dashboard
How to build an Arduino MQTT client
Raspberry Pi IoT: Sensors, InfluxDB, MQTT and Grafana
How to use MQTT protocol with Mosquitto
Now we have an overview of MQTT and it is time to know how to use it using a real example. There are several implementations of MQTT, in this example, we will use Mosquitto, an implementation developed by Eclipse. The first step is installing the MQTT broker. We will install it on Raspberry PI. To install it, we have to add the repository that holds the application, so that we can download it. Before adding the repository it is necessary to add the key to verify that the download package is valid. Let us connect to Raspberry PI using ssh or a remote desktop connection. Now in the terminal, we have to write:
Now, it is time to import the key:
sudo apt-key add mosquitto-repo.gpg.keyCode language: CSS (css)
Finally, let us add the fil .list:
Now, that the repository link is configured properly, we can install Mosquitto on Raspberry PI.
apt-get install mosquitto
The MQTT server (aka MQTT broker) is installed on Raspberry Pi. This server is our MQTT broker as specified above. Now we need to install the client, or in other words, the publisher and the subscriber. In this example, we will install the client and server on the same Raspberry but you can install it on a different pc/server or IoT board.
apt-get install mosquitto-clients
You can experiment on how to use MQTT reading how to build an Arduino MQTT client. In another tutorial, I covered how to use MQTT with ESP8266 and Raspberry.
How to send an MQTT Message
We have installed and configured the client and the server, now we can register a subscriber to a specific topic and wait for an incoming message from a publisher. To register a subscriber we will use this command:
As you can notice, our subscriber is waiting for a message. In this example, we used a topic called swa_news. Now we will send a message using an MQTT publisher that uses the same topic swa_news.
In the example, the MQTT publisher sends “Hello Protocol” message. On the subscriber side, we get the message:
An important aspect to note is that the MQTT is a plain protocol so the message is clear and everyone can read it.
If you like to see the MQTT protocol in action, please check this article describing how to build an MQTT system using Raspberry, Grafana, and InfluxDB.
How to use MQTT on Android smartphone
In this last example, we will use an Android MQTT client so that the same message we sent is received by the Android MQTT client. This video shows how to configure an Android MQTT client.
At the end of this post, you know how to publish a message using MQTT protocol and how to use Mosquitto MQTT as a practical example. It is really important to know the MQTT protocol because it is widely used in IoT. Through this tutorial, we have covered some security aspects related to the MQTT. Finally, we have installed an MQTT broker, Mosquitto, that you can use for your next IoT project.
Hello. Actually to work with MQTT protocol, do i need application?. For communication between two esp8266 wifi modules apps are not possible thats why?
To work with MQTT you need a sever MQTT and then you can connect several client esp too. I use pubnsub library
I have running mosquitto and I want to do that only one client can connect to topic as publisher (maybe with password or something) others can only conncect as subscriber.
How can I achieve this?
HI using a password it is a good solution
. You have to configure the mosquitto. Give a look at https://medium.com/@eranda/setting-up-authentication-on-mosquitto-mqtt-broker-de5df2e29afc#.jsn6e0163
I read that. But it is not suitable for me. I want to connect as publisher with authentication but connecting as a subscriber should be without authentication
thanks for this doc
but I need help, how can I use mqtt protocol in django
I need reference or video
If you want to create a MQTT client in django you can google and find a library. I don’t know if it is supported
Sir, your blog is very helpful. I don’t have much software knowledge so I am a bit confused. How exactly do we set up a MQTT connection between esp8266 nodemcu and raspberry pi 3?
Sir, I’m interested in implementing mosquitto broker. I’ve downloaded mosquitto and eclipse platform. Can you help me the steps required to run mosquitto source code in eclipse platform?
you can download the broker directly without using the source code. Is there a specific use case that requires to use the source code?
Actually I am new in using MQTT. So I don’t know how to use it could you please be able to tell me how to use MQTT in ESP 8266 thing dev. I am understanding theoritically but in practical i am not getting any idea on MQTT. Thank you.
Actually, I am new in using MQTT. So I don’t know how to use it could you please be able to tell me how to use MQTT in ESP 8266 thing dev. I am understanding theoretically but in practice, I am not getting any idea on MQTT. I am using Arduino software Thank you.
in my blog, there are several examples of how to use MQTT. These are some links:
How to build an MQTT system using Arduino/ESP8266 and Cayenne
How to build an MQTT client using Arduino
I’m planning to modify Mosquitto broker source code to improve its features. I’ve downloaded mosquitto source code from https://mosquitto.org/download/. Can you help me in build and run moquitto source code.