How to install and configure Mosquitto on Raspberry Pi.
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install mosquitto mosquitto-clients
Once installation is completed, we need to create the mosquitto service (unless it is automatically built , while using Dietpi for example).
cd /etc/systemd/system
nano mosquitto.service
Paste the following:
[Unit]
Description=Mosquitto MQTT v3.1/v3.1.1 Broker
Documentation=man:mosquitto.conf(5) man:mosquitto(8)
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
NotifyAccess=main
ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Restart=on-failure
[Install]
WantedBy=multi-user.target
and close the file with CTRL+x – Y.
Then enable the service (to start upon reboot)
systemctl enable mosquitto.service
Configuration file. Located in /etc/mosquitto or /etc/mosquitto/conf.d
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
Simple configuration file
# Config file for mosquitto
#
# See mosquitto.conf(5) for more information.
user mosquitto
max_queued_messages 200
message_size_limit 0
allow_zero_length_clientid true
allow_duplicate_messages false
listener 1883
autosave_interval 900
autosave_on_changes false
persistence true
persistence_file /var/lib/mosquitto/mosquitto.db
allow_anonymous true
password_file /etc/mosquitto/passwd
The password_file mentioned above is generated when adding/creating user for mosquitto. Add a username and password to a new password file (/etc/mosquitto/passwd):
mosquitto_passwd -c /etc/mosquitto/passwd username
This command will request to enter a password for the specified username, and place the results in /etc/mosquitto/passwd
To add a user and password to an existing password file, just run the following command:
mosquitto_passwd /etc/mosquitto/passwd username