Do it your self Hobby

How to install MQTT server on Ubuntu14 and Raspberry Pi 3

MQTT stands for MQ Telemetry transport. It’s a simple messaging protocol designed to constrained devices and low bandwidth. It’s a lightweight publish and subscribe system that makes communication between multiple devices easy.
This tutorial will use Ubuntu14 server.

How to install MQTT server on Ubuntu14

sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update
sudo apt-get install mosquitto mosquitto-clients

Configuring Mosquitto:

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

Delete the lines after the long_dest……mosquitto.log
We will now add authentication on the Mosquitto for additional security
Add the following code:

allow_anonymous false
password_file /etc/mosquitto/pwfile
listener 1883

Press ctrl+o to save and ctrl+x to exit.
Let’s add a password. In the following code, I used admin as the user. You change it as per your preferences

sudo mosquitto_passwd -c /etc/mosquitto/pwfile admin

You will have to enter the password twice.
Reboot the server

sudo reboot

Testing Mosquitto

Let’s publish a topic named “hellow/world”.

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

This command uses the authentication we just configured. With admin -P password, Mosquitto will deny the request. After the command, your current connection will listen to the topic. Do not close this (first) connection.
Now, open another connection so you will have two (putty) connections running. Enter the command below on the second (putty) connection:

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

Check the First connection, you will see a message “Hello from Terminal window 2!”. This message is from your second connection.

How to install MQTT server on Raspberry Pi 3

Update Raspberry Pi first

sudo apt-get update

Download the key

sudo wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
sudo apt-get update

Install Mosquitto on Raspberry Pi:

sudo apt-get install mosquitto
sudo apt-get install mosquitto mosquitto-clients python-mosquitto

Configuring Mosquitto:

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

Delete the lines after the long_dest……mosquitto.log
We will now add authentication on the Mosquitto for additional security
Add the following code:

allow_anonymous false
password_file /etc/mosquitto/pwfile
listener 1883

Press ctrl+o to save and ctrl+x to exit.
Let’s add password. In the following code, I used admin as the user. You change it as per your preferences

sudo mosquitto_passwd -c /etc/mosquitto/pwfile admin

You will have to enter the password twice.
Reboot the server

sudo reboot

Testing Mosquitto

Let’s publish a topic named “hellow/world”.

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

This command uses the authentication we just configured. With admin -P password, Mosquitto will deny the request. After the command, your current connection will listen to the topic. Do not close this (first) connection.
Now, open another connection so you will have two (putty) connections running. Enter the command below on the second (putty) connection:

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

Check the First connection, you will see a message “Hello from Terminal window 2!”. This message is from your second connection.

1 thought on “How to install MQTT server on Ubuntu14 and Raspberry Pi 3”

  1. Hans Einar Steinsland

    Thank you. Great. On a early generation of Raspberry the mosquitto does not start by default. Have to run “mosquitto” from ssh prompt(putty).
    Can you give me a hint for autostart.. that work 🙂

Comments are closed.