Mosquitto is an open-source message broker that implements the MQTT protocol. In this tutorial, TechSparks will provide a comprehensive guide on installing the Mosquitto MQTT service on Raspberry Pi, making the entire process easier for you.
MQTT and Mosquitto
MQTT, short for Message Queuing Telemetry Transport, is a lightweight, open-source message publish/subscribe protocol designed to address data exchange between devices in conditions of low bandwidth, high latency, or unstable network connections. By adopting the publish/subscribe model, devices can freely publish and subscribe to messages, supporting features such as retained messages and last will messages when a device disconnects. This design enhances the flexibility of managing device online and offline states. MQTT offers three levels of message delivery services, ranging from the lowest level of at most once delivery to the highest level of exactly once delivery.
Mosquitto, on the other hand, is an open-source MQTT message broker supporting both MQTT protocol versions 3.1 and 3.1.1. Despite the availability of other brokers, Mosquitto is designed with a focus on lightweightness, making it highly suitable for resource-constrained devices and network environments. Additionally, Mosquitto provides support for TLS/SSL-based encrypted communication, adding an extra layer of security to the communication. Its straightforward installation process and flexible configuration options allow users to customize it based on specific requirements.
First, you need to log in to your Raspberry Pi and ensure that it is connected to the network using the following command:
pi@raspberrypi:~ $ ping www.baidu.com
In this command, “ping” is used to test the network connection to the target host, and here I’m using Baidu, but you can replace it with another host.
If you see the following interface during runtime, it means the network is not accessible:
You need to refresh and reconfirm in the WLAN interface. After retesting, if you see the following interface, it means the network communication is normal:
To ensure that the package information you are using is up-to-date, you can use the following command to update the apt package resources:
sudo apt-get update
Then, you need to enter the following command to search for installable MQTT packages:
apt search mqtt
In this Raspberry Pi project, we are using Mosquitto. MQTT has both server and client components, so execute the following commands to install both parts:
sudo apt-get install mosquitto mosquitto-clients
Finally, execute the following command to check if the installed Mosquitto package is correct:
dpkg -l mosquitto
Before using Mosquitto, it’s necessary to configure the file for personalized settings. The configuration file is located at /etc/mosquitto.
First, you can use the following command to view the configuration of the Mosquitto service and understand its parameter settings and operation:
In the comments, it mentions placing local configurations in the /etc/mosquitto/conf.d/ directory.
Check the /etc/mosquitto/conf.d/ directory:
There is a README file in the directory. Open it to see more details:
Any files with the .conf extension placed in this directory will be loaded by the broker as local configuration files.
The complete configuration file documentation is available at: /usr/share/doc/mosquitto/examples/
Copy the mosquitto.conf.gz configuration file to the /etc/mosquitto/conf.d/ directory:
sudo cp /usr/share/doc/mosquitto/examples/mosquitto.conf.gz /etc/mosquitto/conf.d/
Navigate to the /etc/mosquitto/conf.d/ directory:
Decompress the mosquitto.conf.gz file:
sudo gzip -d mosquitto.conf.gz
Edit the mosquitto.conf file:
sudo nano /etc/mosquitto/conf.d/mosquitto.conf
This is a relatively large file with over 800 lines, and all lines are commented out with # symbols.
Make the following modifications by removing the comment symbols and adjusting the default values:
Save and exit the editor.
Return to the home directory:
When testing Mosquitto, we need to open three terminals to simulate the scenarios of Mosquitto service, publisher, and subscriber in MQTT communication.
Start the Mosquitto MQTT broker:
mosquitto -c /etc/mosquitto/mosquitto.conf -v
Use the -v option to log all types of messages, which is useful for debugging. When using Mosquitto in a production environment, it’s generally not necessary to log all messages to avoid consuming storage space.
If you encounter a permission issue, check the permissions of the mosquitto.log file:
ls -l /var/log/mosquitto/mosquitto.log
If you need to modify the log file’s permissions:
sudo chmod 666 /var/log/mosquitto/mosquitto.log
Then execute the command again:
mosquitto -c /etc/mosquitto/mosquitto.conf -v
The service is running, but the terminal window is occupied.
Test Publish and Subscribe
Open two additional terminals:
In one terminal, execute the Subscribe command:
mosquitto_sub -p 1883 -u ct -P xxxxxx -t "test"
This subscribes to the “test” topic, waiting to receive messages.
In another terminal, execute the Publish command:
mosquitto_pub -p 1883 -u ct -P xxxxxx -t test -m "Hello!"
This publishes a message with the topic “test” and the content “Hello!”.
The Subscribe window should display the received message.
Check the log:
sudo cat /var/log/mosquitto/mosquitto.log
Finally, restart the Mosquitto service to ensure the configuration takes effect:
Check the running Mosquitto processes:
ps -ef | grep mosquitto
Now, Mosquitto is running in the background, ready to receive and process MQTT messages.
You Might Be Interested
This tutorial guides precision drone landing with Raspberry Pi, covering ROS Kinetic, MAVROS, and PX4 Autopilot setup. It explains Gazebo config, showcasing landmark recognition via
This collaborative project seamlessly integrates the Raspberry Pi 4B and STM32 to create a sophisticated Intelligent Access Control System. Leveraging the strengths of each component,
This tutorial streamlines Raspberry Pi’s auto-connect feature by editing the wpa_supplicant.conf file, allowing users to specify preferred Wi-Fi networks. By customizing network configurations, it ensures
This tutorial provides a concise guide to establishing SPI communication between a Raspberry Pi and an SSD1306 OLED display. It covers SPI principles, Raspberry Pi
Automating program startup on the Raspberry Pi can be achieved through various methods. Editing the “/etc/rc.local” file or using desktop applications, while simpler, may not
Raspberry Pi, a revolutionary single-board computer introduced by the Raspberry Pi Foundation, has become a global sensation, initially designed for educational purposes. With its integrated