Install MQTT Server on Ubuntu Mate 16.04—Raspberry Pi 2018 Tutorial

Install MQTT Server on Ubuntu Mate 16.04 on Raspberry Pi 3. MQTT is a publish and subscribe system that makes communication between IoT devices easy. It is very simple and easy to use. In this tutorial, you will learn how to install MQTT server on Ubuntu Mate system running on a Raspberry Pi.

Before you start, you should have done the following:

  • Raspberry Pi should have Ubuntu Mate 16.04 installed.
  • It should have static IP address set.

Login to Raspberry PI using Putty.

How to setup MQTT server on Raspberry Pi 3 Ubuntu Mate

First, install Repository:

sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa

Update the system

sudo apt-get update

Install Mosquitto

sudo apt-get install mosquitto mosquitto-clients

After the installation, check the service if it is running.

systemctl status mosquitto.service

Let’s start configuring Mosquitto

sudo /etc/init.d/mosquitto stop
sudo nano /etc/mosquitto/mosquitto.conf

This will show:

# 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

We have to delete the lines after the long_dest file /var…
and paste the following code

allow_anonymous false
password_file /etc/mosquitto/pwfile
listener 1883

This should be your configuration

# 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

allow_anonymous false
password_file /etc/mosquitto/pwfile
listener 1883

Now, let’s add mosquitto username mqtt_user:

sudo mosquitto_passwd -c /etc/mosquitto/pwfile mqtt_user

then set the password. This is the details of MQTT user:
User: mqtt_user
Password: password

Just to show MQTT is running on boot up, let’s reboot the Raspberry Pi

sudo reboot

After the login, check the MQTT service:

systemctl status mosquitto.service

Let’s test the MQTT server. Set the MQTT to subscribe in a topic named: hello/world

mosquitto_sub -u mqtt_user -P password -d -t hello/world

-u mqtt_user = the username we set
-P password = is the password of mqtt_user

Open a new Putty window and log in. This second Putty will be used as a publisher to the topic hello/world. Type the following command to send a message.

mosquitto_pub -u mqtt_user -P password -d -t hello/world -m "Hello from Terminal window 2!"

We will see a message “Hello from Terminal window 2!” on the first Putty window. It means MQTT received the publisher’s message.

This is the way IoT devices communicate. Later I will post some example about how to control ESP devices using MQTT combined with OpenHAB.