Saturday, March 9, 2024

Install Mosquitto MQTT Broker on Raspberry Pi 5

Hello Everyone, In this post I will show you guys how to install, test, and configure the Moqsuitto MQTT for MQTT communication on the newly launched Raspberry Pi 5 board. 


This post will be the foundation for my upcoming Home Automation projects based on ESP32, Raspberry Pi, InfluxDB, and Grafana.

In this post the Mosquitto broker will run on the Raspberry Pi 5, which means that the devices connected on the same network can publish and subscribe, if you are looking for a Mosquitto broker that can be accessed globally over the internet then you need to look for options such as Cloud MQTT, Hive MQTT, Digital Ocean and others.


This post is sponsored by PCBWay, with more than a decade in the field of PCB prototype and fabrication, PCBWay is committed to meeting the needs of their customers from different industries in terms of quality, delivery, cost-effectiveness, and any other demanding requests.
As one of the most experienced PCB manufacturers in the World, PCBWay prides itself on being your best business partner as well as a good friend in every aspect of your PCB needs.

MQTT Basic Concepts

MQTT stands for Message Queuing Telemetry Transport. MQTT is a simple messaging protocol, designed for constrained devices with low bandwidth. So, it is the perfect solution to exchange data between multiple IoT devices.
MQTT communication works as a publish and subscribe system. Devices publish messages on a specific topic. All devices that are subscribed to that topic, will receive the message.

In MQTT there are a few basic concepts that we need to understand, and they are as below.

  • Publish/Subscribe
  • Messages
  • Topics
  • Broker

MQTT Publish/Subscribe

In a publish and subscribe system, a device can publish a message on a topic, or it can be subscribed to a particular topic to receive messages.

The above picture illustrates the simple publish and subscribe, here "Device-1" publishes on a topic, while "Device-2" is subscribed to the same topic that "Device-1" is publishing in, so "Device-2" will receive the message.

MQTT Messages

Messages are the information that we want to exchange between the devices, it can be a message like a command, to turn on or off a relay, or data like sensor readings.

MQTT Topics

Topics are the way we register interest in incoming messages or how we specify where we want to publish the messages. Topics are represented with strings, and topics can be nested also with the forward slash.
For example "MainTopic/SubTopic-1".
Please keep in mind that the topics are case-sensitive.

MQTT Broker

The MQTT broker is responsible for receiving all messages, filtering them, deciding which device is interested in them, and publishing the message to all subscribed clients. There are several brokers available in the market, but we will be using the Mosquitto MQTT broker.

The Mosquitto MQTT broker can be installed on any PC with all major operating systems, but Raspberry Pi 5 is a very convenient choice, as normally we can't keep our PC on always but this is not the problem with the Raspberry Pi, make sure to observe the temperature of the Raspberry Pi to avoid any damage, I will prepare an upcoming tutorial on this, and update links here once it is ready.

Prerequisites

Before continuing with this tutorial, you should be familiar with the Raspberry Pi board and its operating system, and you need the following hardware.

Installing Mosquitto Broker on Raspberry Pi OS

Installing Mosquitto Broker is very simple, but before installing the Mosquitto first make sure that your Linux distribution is updated with the latest available version, and this can be done using the following commands.

sudo apt update
sudo apt upgrade

These commands will update the Linux to the latest available version, and could take some time, but don't worry about this time.

Now use the following command to install the Mosquitto Broker and Clients.

sudo apt install -y mosquitto mosquitto-clients

Once the installation is completed, to make the Mosquitto auto starts when the Raspberry Pi boots, you need to run the following command.

sudo systemctl enable mosquitto.service

Now type the following command to start the Mosquitto broker.

mosquitto -v

Mosquitto Broker Running
This command will start the Mosquitto broker, and now we are good to proceed ahead with testing the "Publish" and "Subscription" of topics.

The above command also returns the version of the Mosquitto broker and will also prompt the message "Starting in local mode. Connections will only be possible from clients running on this machine. Create a configuration file that defines a listener to allow remote access".

This is normal, this means that by default we can't communicate with the Mosquitto broker from other devices, this means that if you want an external device, for example, ESP32 to publish some message to the Raspberry Pi, then it is not possible with the default configuration, we need to update the configuration to allow this, and we will see this in the upcoming section. But before proceeding further let's check the "Publish" and "Subscription" of the messages.

The following is the command to subscribe to the topics.

mosquitto_sub -h "localhost" -p 1883 -t "topic"

Here, I am using the hostname "localhost" and port number 1883, which is the default port for Mosquitto MQTT, and the topic that I am subscribing to is the name "topic", so whenever someone publishes some message with the topic name as "topic", we see the message.

Similarly the following is the command to publish the message.

mosquitto_pub -h "localhost" -p 1883 -t "topic" -m "Hello World from Raspberry Pi 5"
mosquitto_pub -h "localhost" -p 1883 -t "topic" -m "Once Again, Hello World from Raspberry Pi 5"

Here I am publishing the two messages, the following screenshot illustrates this whole transaction.

Publish and Subscription

Enabling Remote Access

As mentioned above also, with the above setup other devices can't publish to the broker running on the Raspberry Pi unless we update the "MQTT Configuration" file. In this tutorial, we will cover only the "No Authentication" configuration, in the future I will either update or create a new tutorial with "User Based Authentication" which is a more practical scenario.

So as mentioned above we need to update the Mosquitto Broker "MQTT Configuration" file, which is usually present under the following path.

/etc/mosquitto/mosquitto.conf

To open this file we will use the "nano" in-build text editor, and the command will look as follows.

sudo nano /etc/mosquitto/mosquitto.conf

After opening the file, the content looks like this.

MQTT Global Configuration File
As you can see in this file, it is advised to create a local configuration file in the folder "conf.d", so I will close this file by using "Ctrl+X" and then create a new MQTT configuration file using the following command.

sudo nano /etc/mosquitto/conf.d/mosquitto.conf

And this command will create a new file with the name "mosquitto.conf" inside the folder "conf.d", inside this file we will specify the MQTT port number as 1883, and we will set the "allow_anonymous" setting to true, and after setting this, all devices shall be able to publish and subscribe to Mosquitto broker running on the Raspberry Pi.

listener 1883
allow_anonymous true

Creating Mosquitto Configuration File
Mosquitto Configuration File Creation Completed

Once this file is created, we need to restart the "Mosquitto" so that it takes the latest configuration settings available from this newly created configuration file, and this can be done using the following command.
sudo systemctl restart mosquitto.service

Ideally, this command should work out of the box, but for me, it doesn't work, as you can see in the following image.
Error while Restarting Mosquitto Service

To fix this problem there are multiple ways, the first is to restart the Raspberry Pi, after booting up it should work automatically, but sometimes it is not a good choice to restart the Raspberry Pi, in that case, I did is to first stop the service, and then disable the service, and then enable the service and then started the Mosquitto again, and for this, I have to use the following commands back to back.
sudo systemctl stop mosquitto.service
sudo systemctl disable mosquitto.service
sudo systemctl enable mosquitto.service
mosquitto -v

Stopping and Starting Mosquitto

Once this is done the Mosquitto will start again, as shown below.
Mosquitto Running Again
And now it is possible to access the Mosquitto Broker running on Raspberry Pi, from other devices on the same network, but if this is still not working, then I would suggest you guys restart the Raspberry Pi. In my case, this also doesn't work, but after restarting the Raspberry Pi 5, it started working.
Now to test, I will test using my Windows 11 laptop, which also has Mosquitto installed and running, I will use my Windows 11 command prompt to send a message over a topic named "topic", to see if this is working.
Publishing Using Windows 11 to Raspberry Pi 5

I used the same Publish and Subscribe commands, but instead of using the "localhost" name I used the IP address assigned to the Raspberry Pi, to find the IP address assigned you can check your router setting or type "hostname -I" to find this out. As you can see both publish and subscription works fine.

Conclusion

We learned how to install the Mosquitto broker on the Raspberry Pi and also learned how to Publish and Subscribe topics on the local host and also from external devices such as Windows.
We will use this knowledge to build home automation projects for our needs in the future.

No comments:

Post a Comment