Sponsors

STATS

Todays date:
Friday 19th of April 2024 06:36:24 PM

You are visitor

48119


Your IP address is 3.145.94.251


Page last updated
25 May 2017

MQTT

Raspberry Pi     After experimenting with our automation, solar and robotics projects, we decided to try communicating between our projects using MQTT.

We will not go into detail about what MQTT is or what it does. There is already plenty of information around t'net describing this already.

Here we will explain the steps we needed to take to install MQTT on our devices.


Update our repository

We chose to install the Mosquitto MQTT software. Mosquitto is not part of the apt-get repository. We need to add it manually, then install it. We need to do this on each raspberry Pi on our system. If you are using just one RPi, this needs to be done only once.

Log into the RPi console as user pi. Then enter the following commands. Allow each command to complete before entering the next one.

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

the broker

The broker can be considered the centre of our system, the hub if you like.

Since our web server (Raspberry Pi) is on 24/7, it would make sense to install our MQTT broker on this, so that it will be available to all clients, all of the time.

Log into the RPi console as user pi. Then enter the following command:


sudo apt-get install mosquitto

The Mosquitto Broker should now be installed and running on our Raspberry Pi.

The clients

The Mosquitto Client software is used to connect to our Broker. If you are using the client software on the same Raspberry Pi as your Broker, simply enter the following command:

sudo apt-get install mosquitto-clients

You can then move on to the next section "TESTING MQTT".

In our case, we are using a second RPi to act as our client.
Remember we had to manually add Mosquitto to our apt-get repository on our first Rpi? We now need to do that on our second machine.

Log into the second RPi as user pi. Enter the following commands. Allow each command to complete before entering the next one.


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

We can now install the client software to our second Rpi by entering:

sudo apt-get install mosquitto-clients

We can now move onto testing our MQTT operation.

Testing MQTT

In order to test that our MQTT system is working, all we need to do is subscribe to a topic.

Open a console window and login as user pi. Then type the following command:


mosquitto_sub -h 192.168.1.5 -t testmqtt
`
You will need to substitute the ip address of the server which is running your MQTT broker.
Note. If you are running the broker and clients on the same Raspberry Pi, The IP address will be 127.0.0.1

This will create a client, which is listening to the topic 'testmqtt'.

Next we will publish some data to the same topic.
Open another console window and log in.
This time, type:


mosquitto_pub -h 192.168.1.5 -t testmqtt -m "It works."

This command will publish some text to the topic 'testmqtt'.
The message "It works." should appear in the client window.


>>next>>