MQTT Protocol Tutorial: Technical description with practical Mosquitto example

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:

MQTT protocol tutorial: publisher subscriber

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 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:

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
how to install Mosquitto on raspberry pi

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

mosquitto repo url

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:

subscribe to a mqtt channel using mosquitto

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.

publish a message to a MQTT channel

In the example, the MQTT publisher sends “Hello Protocol” message. On the subscriber side, we get the message:

send a message using mosquitto

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.

Summary

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.

    1. Vishnu k December 28, 2016
      • Francesco Azzola December 30, 2016
    2. A.G. March 14, 2017
    3. Muhoza yves June 20, 2017
      • Francesco Azzola June 20, 2017
    4. Judith Coelho June 20, 2018
    5. Jiby September 7, 2018
      • Francesco Azzola September 7, 2018
    6. Chandrakanth September 28, 2018
    7. Chandrakanth September 28, 2018
    8. JIby December 15, 2018

    Add Your Comment