MQTT protocol is a Machine to Machine (M2M) protocol widely used in Internet of things. The MQTT protocol is a message based protocol, extremely light-weight and for this reason, it is adopted in IoT ecosystem. Almost all IoT platforms support MQTT protocol to send and receive data from smart objects. There are several implementations for different IoT boards like Arduino, Raspberry and so on.
There are other IoT protocols used to implement IoT projects but MQTT is one of the most efficient.
MQTT Protocol Overview
The MQTT 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 very power-saving protocol. For all these reasons, it is suitable for IoT.
This protocol used publish-subscriber paradigm in contrast to HTTP based on request/response paradigm. It uses binary messages to exchange information with a 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 MQTT uses TCP stack as transmission substrate.MQTT protocol tutorial: How it works and how to use it in #IoT projects Click To Tweet
MQTT Message pattern
As said before, MQTT protocol implements publish-subscriber paradigm. This paradigm decouples a client that publishes a message (“publisher”) to other clients that receive the message (“subscribers”). Moreover, MQTT is asynchronous protocol, that means that it does not block the client while it waits for the message. In contrast to HTTP protocol, that is mainly a synchronous 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.
MQTT Publisher-subscriber pattern (MQTT Broker, MQTT Client)
As described above MQTT is a message based protocol that uses publisher-subscriber pattern. 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 publisher and dispatches these messages to the subscribers. 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. This topic is managed by the MQTT broker. 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 project 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 MQTT protocol
Now we have an overview about 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 Mosquitto, 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.key
Finally, let us add the fil .list:
sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.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 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
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:
As important aspect to note is that the MQTT is a plain protocol so the message is clear and everyone can read it. If the security is the main concern, you can read this article explaining how to make MQTT secure.
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 hopefully know how to publish a message using MQTT protocol.