Eclipse Mosquitto™ is an open source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 3.1 and 3.1.1. MQTT provides a lightweight method of carrying out messaging using a publish/subscribe model. This makes it suitable for “Internet of Things” messaging such as with low power sensors or mobile devices such as phones, embedded computers or microcontrollers like the Arduino.
This is enough for the introduction. In this tutorial we will learn “How to install mosquitto broker on Raspberry Pi. So Let’s get started!! 😎
Open up your favourite terminal program such as putty and connect to Raspberry Pi through ssh. Type the following commands one by one for successful installation of Mosquitto broker in your Raspberry Pi.
For Raspbian Jessie:
curl -O http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key sudo apt-key add mosquitto-repo.gpg.key rm mosquitto-repo.gpg.key cd /etc/apt/sources.list.d/ sudo curl -O http://repo.mosquitto.org/debian/mosquitto-jessie.list sudo apt-get update sudo apt-get install mosquitto
For Raspbian Stretch:
Due to possible dependencies issue, there is no user friendly way to install latest mosquitto version on Raspbian Stretch. However, one can install the aptitude version as following;
sudo apt-get install mosquitto
Using systemd start up for Mosquitto:
Mosquitto broker does not using modern system of systemd for start up. In order to move old init system, following the procedure below.
sudo systemctl stop mosquitto sudo update-rc.d mosquitto remove sudo rm /etc/init.d/mosquitto
After removing old init.d script for Mosquitto broker, Create systemd unit file using Nano editor;
sudo nano /etc/systemd/system/mosquitto.service
Paste the following contents in the Nano editor using Right mouse key.
[Unit] Description=MQTT v3.1 message broker After=network.target Requires=network.target [Service] Type=simple ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf Restart=always [Install] WantedBy=multi-user.target
Save the file by pressing “CTRL” and “X” key. Editor will ask for the confirmation to save the file. Press “Y” key and hit “Enter”.
Now reload systemd configuration using the following command.
sudo systemctl daemon-reload
Enable Mosquitto service to start at boot;
sudo systemctl enable mosquitto
Starting Mosquitto service;
sudo systemctl start mosquitto.service
Password protecting MQTT Server:
In order to protect our Mosquitto MQTT Server, we need to create a user by issuing following command.
sudo mosquitto_passwd -c /etc/mosquitto/accesslist.txt pi
“pi” is the name of user which will be used in authentication process in future. By issuing the command above, system will ask to enter a password for MQTT broker. Enter the password and press Enter key, confirm it by retyping again and you have successfully completed securing your MQTT server with a password.
We also have to make certain adjustment further. Create a new configuration file with the help of nano editor by using the command below.
sudo nano /etc/mosquitto/conf.d/mosquitto.conf
Copy the contents of mosquitto configuration file and paste the contents into the nano editor the by clicking Right mouse button and save the file using “CTRL” and “X” key followed by “Y” and then hit enter (“Return Key”).
After adding configuration file, restart mosquitto service by issuing following commands;
sudo systemctl stop mosquitto sudo systemctl start mosquitto
Thats all, We have successfully configured our Mosquitto broker. Have fun 😎 .