PI and MQTT.

Setting Up your Raspberry Pi

The following is based on Raspberry Pi OS with desktop (April 2022). We recommend you follow the instructions on https://www.raspberrypi.com/documentation/computers/getting-started.html for setting up the SD Card with a fresh version of the OS.

Screen Discussion

Enabling SSH

Installing MQTT

MQTT stands for Message Queuing Telemetry Transport (Right or wrong, for ease I pronounce it MUTT with a q-like emphasis on the U). We use this protocol to send messages between our nodes and base station. In this instance we are going to use the popular Mosquitto server software.

The Mosquitto acts as a broker or middle-man between publishers (our sensor nodes) and the subscriber (our base station).

The following guide has been completed on Raspberry Pi 2 and 3 but should be applicable to any machine running Linux (e.g. Ubuntu) or Raspbian. You will need to open a terminal window or connect to the Pi via SSH.

INSTALLATION

Step 1 – Ensure your OS is up to date.

The following step can often avoid unnecessary problems and issues later on.

sudo apt update
sudo apt upgrade

Step 2 – Install Mosquitto.

Because Mosquitto is part of the OS repository it can be installed with this one liner.

sudo apt install mosquitto mosquitto-clients

Step 4 – Testing Mosquitto.

At this point you should have Mosquitto setup and running. The install script will have configured the server to start at boot up.

sudo systemctl status mosquitto

Step 5 – Finishing Up.

There are two finishing up steps that will ensure happy MQTTing. First if are using a firewall such as UFW, make sure that you open port 1883. Second, a fixed IP address or hostname will ensure uninterrupted coms.

CONFIGURATION

In order to ensure this step goes cleanly its worth stopping the MQTT server

sudo systemctl stop mosquitto

Step 1 – Default Conf.

Use nano to open/create the default.conf from the /etc/mosquitto/conf.d directory

sudo nano /etc/mosquitto/conf.d/default.conf

and add/check the following.

listener 1883
allow_anonymous false
password_file /etc/mosquitto/passwd

Step 2 – Passwords

We will use mosquitto_passwd to create our username/password pairs. Type the following, changing <username> for your username of choice. You will then be asked for your chosen password twice. As this is not a main login and will be added to the config file of the station I would choose a new one specific to Hive Scientia.

sudo mosquitto_passwd -c /etc/mosquitto/passwd <username>

If you exclude the -c you can add additional username/password pairs as the -c means create this password file and overwrite an existing file. I like to create username/password pairs for each hub.

sudo mosquitto_passwd /etc/mosquitto/passwd <username>

Step 3 – Restart the Server

You can now restart the server with:

sudo systemctl start mosquitto 

VERIFICATION

Now we can verify that MQTT is working.

First we will set up a subscriber to listen for messages.

mosquitto_sub -h localhost -p 1883 -t myTestTopic -u <user_name> -P <password>

The second command will not exit unless there is a problem. Note the uppercase P – this is case sensitive. It should sit and wait for a message.

Now open another SSH window or terminal and type the following command.

mosquitto_pub -h localhost -p 1883 -t myTestTopic -u <user_name> -P <password> -m "Hello"

As soon as you press enter the message ‘Hello’ should appear in the original SSH window.

You can now close one of the windows. To exit the subscriber press CTRL-C

The nest step is to prepare the Python installation.

Leave a Reply

Your email address will not be published.