Summary: Basic methods to install MQTT broker service on Raspberry Pi server
Previously, we introduced the basic knowledge and principles of MQTT communication. We have also covered various commands in Linux. Today we will try to install MQTT broker service on Raspberry Pi.
Typically, the MQTT broker is installed on Raspberry Pi using Mosquitto software. Mosquitto is an open-source MQTT (Message Queuing Telemetry Transport) broker implementation, and one of the reference implementations of the MQTT protocol.Mosquitto provides a high-performance, lightweight, scalable, and easy-to-deploy MQTT broker for establishing communication connections in Internet of Things (IoT) applications.
Here are some of the features and functionalities of Mosquitto:
1.Support for MQTT protocol: Mosquitto is a broker based on MQTT protocol, supporting MQTT v5.0, v3.1.1, and v3.1 versions, providing complete MQTT message publishing and subscription capabilities.
2.Client connection management: Mosquitto can handle thousands of client connections simultaneously, providing reliable connection management for communication between devices and applications.
3.Security authentication and authorization: Mosquitto supports username and password-based authentication and can be configured with TLS/SSL for secure connections. It also supports access control lists (ACL) for subscription and publishing to control client access to topics.
4.Persistence and last will messages: Mosquitto can store client subscriptions and last will messages, ensuring recovery upon client reconnection. Last will messages can be automatically published to a specified topic when a client disconnects to notify other client devices.
5.Integration and extensibility: Mosquitto provides a rich set of APIs and interfaces for easy integration with other applications and languages. It also supports a plugin mechanism for customizing and extending functionality as needed.
6.Cross-platform support: Mosquitto can run on various operating system platforms, including Linux, Windows, macOS, and Raspberry Pi.
7.Command line tools: Mosquitto comes with a set of convenient command line tools for testing and debugging MQTT connections, such as mosquitto_pub and mosquitto_sub.
In summary, Mosquitto is an easy-to-deploy and use MQTT broker that provides a reliable messaging mechanism for IoT applications. Whether in IoT projects or other real-time communication applications, Mosquitto is a powerful and flexible choice.
To install the MQTT broker service on Raspberry Pi, follow these steps:
1. Update the Raspberry Pi operating system
Updating the operating system requires two commands. As shown below:
-
sudo apt-get update
-
sudo apt-get upgrade
The output after executing these two commands is as follows:
cth@raspberrypi:~ $ sudo apt-get updateHit:1 http://deb.debian.org/debian bookworm InReleaseHit:2 http://archive.raspberrypi.com/debian bookworm InReleaseHit:3 http://deb.debian.org/debian-security bookworm-security InReleaseHit:4 http://deb.debian.org/debian bookworm-updates InReleaseReading package lists... Donecth@raspberrypi:~ $ sudo apt-get upgradeReading package lists... DoneBuilding dependency tree... DoneReading state information... DoneCalculating upgrade... DoneThe following packages have been kept back: linux-headers-rpi-2712 linux-headers-rpi-v8 linux-image-rpi-2712 linux-image-rpi-v8 raspberrypi-ui-mods raspi-utils rpi-eeprom0 upgraded, 0 newly installed, 0 to remove and 7 not upgraded.
The last two items displayed are 0, indicating that there are no packages to update or install.
2. Install the MQTT broker Mosquitto
As previously mentioned, installing software on Raspberry Pi requires the apt-get install command, followed by the name of the software package you want to install. Here, we need to install mosquitto and mosquitto-clients, with the former being the Mosquitto service and the latter being the MQTT client for accessing Mosquitto service. Thus, the installation command is as follows:
sudo apt-get install -y mosquitto mosquitto-clients
If no errors occur during the program’s operation, the installation is complete. Next, we will test and check if the Mosquitto service is running normally after installation.
3. Check the running status of Mosquitto service
After installation, the Mosquitto service will start automatically. You can check its status using the following command:
sudo systemctl status mosquitto
If the service is running, the status information will be displayed as follows:
4. Test whether the MQTT service is functioning normally
Next, we will use the MQTT client we just installed to test whether the Mosquitto service is working properly.
1. Subscribe to a topic
To subscribe to a topic, use the mosquitto_sub command. Here we will subscribe to a topic called test/topic. The command used is as follows:
mosquitto_sub -h localhost -t test/topic
After executing this command, it will be in a blocking state, waiting for the MQTT broker to send information on the subscribed topic. When a message is received for the test/topic topic, it will be displayed in the terminal.
2. Publish a message
At this point, we need to keep the previous subscription terminal state unchanged, then open another terminal and use the mosquitto_pub command to publish a message to the test/topic topic, with the message content being “Hello, MQTT!”. The command used is as follows:
mosquitto_pub -h localhost -t test/topic -m “Hello, MQTT!”
If everything is normal, the previously opened subscription terminal should display the recently published message. As shown in the following image:
So far, we have introduced the basic methods for installing the MQTT broker service on Raspberry Pi and how to perform simple message testing. Next, we will discuss the configuration related to the MQTT broker service.