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.
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
MQTT Topics
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
- Raspberry Pi Board
- Micro SD Card
- Raspberry Pi Power Supply
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.
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.
Once the installation is completed, to make the Mosquitto auto starts when the Raspberry Pi boots, you need to run the following command.
Now type the following command to start the Mosquitto broker.
Mosquitto Broker Running |
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.
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.
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.
To open this file we will use the "nano" in-build text editor, and the command will look as follows.
After opening the file, the content looks like this.
MQTT Global Configuration File |
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.
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.
Stopping and Starting Mosquitto |
Mosquitto Running Again |
No comments:
Post a Comment