MQTT (Message Queuing Telemetry Transport) is a simple messaging protocol often used in IoT applications. The protocol is based on a publish-subscribe system where both types of devices that publish and subscribe MQTT messages are called MQTT clients. The exchange of messages between the publishers and subscribers is done with the help of a broker, which must be running on a computer device that should remain live indefinitely.
In large IoT projects, a cloud system or service is often assigned as an MQTT broker. The cloud services have almost 100 percent uptime and can be accessed from anywhere. Therefore, the cloud-based MQTT brokers can connect MQTT publishers and subscribers worldwide. For small-scale or local IoT projects like home automation or industrial automation, cloud services for MQTT brokers are more than sufficient but are not cost-effective. For such applications, it is much more economical to use microcomputers or single-board computers. Microcomputers such as Raspberry Pi can be left powered on all the time with low power consumption and operational cost. These microcomputers can easily connect with WiFi networks, facilitating an IoT network spanning over a Wide Area Network (WAN). Even the WiFi networks used by microcomputer brokers can be easily extended using WiFi repeaters or access points whenever required. With many MQTT brokers, it is even possible to connect with MQTT clients worldwide even if the broker is installed on a microcomputer.
In this article, we will discuss the role of a broker in the MQTT publish-subscribe system and discuss how a Raspberry Pi can be configured as an MQTT broker.
What is MQTT protocol?
MQTT)is a messaging protocol designed for low-bandwidth internet connections that is suitable for data exchange between constraint devices such as those often deployed within IoT networks. The protocol allows for data communication to multiple devices simultaneously. Also, multiple devices can broadcast data at the same time.
MQTT is completely different from typical internet protocols in which the data is often communicated peer-to-peer in a client-server model. Instead, MQTT follows a publish-subscribe system. In MQTT, both types of devices that broadcast data and those that receive data are MQTT clients. The clients can both publish data on a specific topic and receive data on a specific topic. Otherwise, they may be only publishing data on specific topics or receiving data for subscribed topics. Even a device can publish and subscribe to messages for the same topic. The MQTT data that is published or received by an MQTT client is always in context to a specific topic. The MQTT broker defines the topics and manages entire data communication among MQTT clients.
How it works
The MQTT publish-subscribe system whereby the MQTT broker defines specific topics. The topics are represented by strings separated by a forward slash that serves to separate the topic levels. The following is a valid example of a topic for a home automation system.
The string ‘home/bedroom1/fan’ altogether is a specific topic. The strings ‘home’, ‘bedroom1’, and ‘fan’ are different topic levels. The forward slash is the topic level separator. The devices/MQTT clients that need to publish messages/data related to a topic register as publisher for the specific topic with the MQTT broker. The devices/MQTT clients need to receive messages/data related to a topic and register as a subscriber for the specific topic with the MQTT broker. For example, a remote control or mobile device used to control the fan will register as a publisher to the topic ‘ home/bedroom1/fan ‘. While a device controlling the fan regulator will register as a subscriber to the topic ‘ home/bedroom1/fan ‘. Whenever the publisher (remote control) will broadcast some message regarding the topic, it will be routed to the subscriber (fan regulator circuit) by the MQTT broker. The publisher may subscribe to messages like “on”, “off”, or speed levels like “1”, “2”, “3” etc to control the fan.
It is worth noting that topics are case-sensitive. A topic ‘ home/bedroom1/fan ‘ is different from ‘ Home/bedroom1/fan ‘ or ‘ home/bedroom1/Fan ‘ and so on. Often, in small IoT networks like home automation, WiFi development boards like ESP8266, ESP32, or specific automation control platforms are configured as publishers.
Messages are the textual data communicated on specific topics and may contain data from the sensor nodes or commands from a controlling device. The devices that need to transmit sensor data over the IoT network or those that have to communicate control commands always register as publishers. The MQTT clients that need to take action based on remote sensor data or remote commands register as subscribers.
As mentioned already, a device or MQTT client can both publish and subscribe. However, a device can also even publish and subscribe to the same topic. The case may be that a device may be publishing messages for certain topics and may have subscribed to receive data for other topics. This all depends upon the nature of the application.
Some topics may be defined such that many MQTT clients could subscribe to them. In this case, when the message related to such a topic is published, it is received by all the subscribers. For example, in a smart home, there may be smart clocks in all the rooms. A particular MQTT client may publish messages for topics like ‘date-time’, ‘temperature’, and ‘humidity’. All the smart clocks may have subscribed to these topics and can receive messages for them simultaneously.
All the messages between MQTT clients are managed by an MQTT broker, which can run on a microcomputer or a cloud platform. The MQTT brokers running on microcomputers are even capable of communicating messages to IoT devices that are not part of the local network by using the internet. One of the important things for an MQTT broker is that it should have 100 percent uptime because the MQTT clients may publish data anytime and may need data on subscribed topics anytime.
Applications of MQTT protocol
Interestingly, MQTT started as a proprietary protocol of IBM for communication with SCADA systems in the oil and gas industry. Later, it became an open-source protocol that is maintained by the Organization for the advancement of Structured Information Standards (OASIS). At present, some of the notable applications of the MQTT protocol include home automation, smart metering, remote sensing, smart farming, and wearable IoT. Even Meta uses MQTT protocol for its Facebook messenger and Instagram chats.
In an MQTT publish-subscribe system, the broker act as a server for MQTT clients. The broker is responsible for receiving messages published by MQTT clients, filtering the messages, figuring out the subscribers, and communicating messages to the subscribed MQTT clients. MQTT brokers are basically server programs that are designed to manage MQTT communication. Some of the best MQTT brokers include Mosquitto, Cassandana, Hive MQ, EMQ X, Ejjaberd, EMQ, EMQ Pulse, Apache ActiveMQ, Apache ActiveMQ Pulse, MQTTnet, RabbitMQ, RabbitMQ Pulse, Wave, VerneMQ, VerneMQ Pulse, Mosquitto Pulse, Mosca, and Mosca Pulse.
Preparing Raspberry Pi for MQTT broker
Raspberry Pi is a great choice for installing an MQTT broker. This single-board computer can be left powered on all the time with no fuss, just like a modem or router remains on for all time. The board has a very low power footprint and can reliably keep running a high-level application without failure. The board can also be easily interfaced with a display for troubleshooting or as a graphical user interface to an IoT application.
Firstly, you need to set up your Raspberry Pi and install Raspbian on the board, setting it up like a desktop system. Check out this tutorial to install Raspbian and get Raspberry Pi ready. The MQTT broker that we will install on Raspberry Pi is Mosquitto.
Installing Mosquitto broker on Raspberry Pi
After getting the Raspberry Pi ready with the Raspbian installed on it, you can install the MQTT broker on it. Open the terminal window by navigating to Accessories-> Terminal or clicking the Terminal icon. Run the following command to update the system.
sudo apt update
After updating Raspberry Pi, upgrade the system by running the following command.
sudo apt upgrade
Press Y and Enter, when prompted to download packages. It will take 10~20 minutes to upgrade the Raspberry Pi. Now install Mosquitto Broker by executing the following command.
sudo apt install -y mosquitto mosquitto-clients
Check the Mosquitto installation by executing the following command. The command will return the version of the Mosquitto installation.
You can make the Mosquitto Broker run on the startup by executing the following command.
sudo systemctl enable mosquitto.service
Enabling remote access on Raspberry Pi
MQTT clients cannot communicate with Mosquitto Broker on Raspberry Pi until remote access is enabled on the microcomputer. In Mosquitto 2.0 and higher, you need to choose authentication options explicitly before connecting MQTT clients. In lower versions, clients connect with the broker by default without authentication.
To enable remote access, you need to create or edit a configuration file. In order to open the Mosquitto configuration file mosquitto.conf in nano editor, execute the following command in Terminal.
sudo nano etc/mosquitto/mosquitto.conf
This will open mosquitto.conf in nano editor as shown in the image below.
Move to the end of the file using arrow keys and type the following lines.
Close the file by pressing Ctrl + X and save changes by pressing Y and Enter.
After saving the file, restart Mosquitto Broker by executing the following command in Terminal, so that the changes in mosquitto.conf file takes effect.
sudo systemctl restart mosquitto
Enabling authentication on MQTT broker
To add a user/password authentication to the Mosquitto Broker, run the following command in the Terminal.
sudo mosquitto_passwd -c /etc/mosquitto/passwd USERNAME
Note that you will need to replace the ‘USERNAME’ with your desired username. When you press Enter, you will be asked to create a username and prompted to enter a password. While entering the password, no characters will be displayed. You will then be prompted to re-enter your password. Type the same password again and press Enter. Again, no characters will be displayed while entering the password. A password file passwd will be created in the /etc/mosquitto directory.
Now that to allow authentication with this username and password, you need to modify mosquitto.conf file. Open the mosquitto.conf file in nano editor by executing the following command in the Terminal.
sudo nano /etc/mosquitto/mosquitto.conf
Add the following lines at the top of the file.
The above lines must be added to the very top of the file. Otherwise, these lines will not take any effect. Next, add the following lines at the end of the file.
If the mosquitto.conf file has been previously edited for remote access without authentication, remove the following lines before adding the above-mentioned lines.
The mosquitto.conf file will look like as shown in the image below after editing.
Close the file by pressing Ctrl + X and save changes by pressing Y and Enter. After saving the file, restart the Mosquitto Broker by executing the following command in Terminal, so that the changes in mosquitto.conf file takes effect.
sudo systemctl restart mosquitto
Once the authentication with username and password is enabled, any MQTT client willing to communicate with the MQTT Broker will need to supply them for authentication.
You can change the password for the existing username by executing the following command in the Terminal.
mosquitto_passwd <password file> <username>
For example, execute the following command to change the password of the username created with passwd file. Replace USERNAME with your own username.
sudo mosquitto_passwd /etc/mosquitto/passwd USERNAME
You will be prompted to enter a new password. No characters will be displayed while you will enter the password. After entering the desired password, press Enter. You will be now prompted to re-enter the password. Enter the same password and press Enter. Again, no characters will be displayed while entering the password.
After already creating one username, for adding more username-password, execute the following command in the Terminal with a different password file.
mosquitto_passwd <password file> <username>
For example, you may create another password file passwd2 as follows.
sudo mosquitto_passwd /etc/mosquitto/passwd2 USERNAME
Note that you need to replace USERNAME with your own username in the above command. You can configure the Mosquitto Broker to use the new username-password by modifying the mosquitto.conf and adding the following line at the end of the file.
Accessing Raspberry Pi over WLAN
Apart from username and password (if authentication is enabled), MQTT clients need to know the IP address of Raspberry Pi for connecting with the broker. The IP address of the Raspberry Pi can be found by executing the following command in the Terminal.
This will return both IPv4 and IPv6 addresses. The first address returned is the IPv4 address. Only the IPv4 address can be found by executing the following command in the Terminal.
Testing Mosquitto broker on Raspberry Pi
The broker itself can also publish messages. Therefore, if the Mosquitto client is also installed on Raspberry Pi, it is handy to test Mosquitto Broker on Raspberry Pi. We had already installed the Mosquitto client by executing the following command in the Terminal.
sudo apt install -y mosquitto mosquitto-clients
First of all, you need to run the Mosquitto Broker in the background by executing the following command in the Terminal.
Now we can subscribe a test topic to the Mosquitto client by executing the following command in the Terminal.
mosquitto_sub -d -t testTopic
If Mosquitto Broker is successfully installed, you can notice it receiving the published message, as shown in the image below.
As we have also installed the Mosquitto client on the Raspberry Pi, we can receive a published message on both the broker as well as the MQTT client. Open one more Terminal window and run a Mosquitto client by executing the following command in that window.
mosquitto_sub -d -t testTopic
Now again, publish a new message by executing the following command in the second Terminal window.
mosquitto_pub -d -t testTopic -m “another message”
You can see that both broker and client via the broker receive the message, as shown in the image below.
The MQTT broker act as a server in MQTT publish-subscribe system and is responsible for communicating messages between the MQTT clients. It is quite convenient to use Raspberry Pi as MQTT broker in small-scale IoT systems like home automation, office automation, or industrial automation. The MQTT brokers such as Mosquitto can be easily installed and configured on Raspberry Pi. The Raspberry Pi can be left powered on indefinitely, so that MQTT clients connected via it can communicate anytime. As we can also install the Mosquitto client on the Raspberry Pi along with Mosquitto Broker, testing the broker is really simple and does not require other devices like ESP8266, ESP32, or Arduino to connect as an MQTT client.
You may also like:
Filed Under: Tech Articles