LEDs connected to mypi2. mypi1 sending simulated temperature values.

Sensor and actuator communication via Mainflux MQTT broker

Ulrich Tehrani


In this short tutorial I will demonstrate the communication between a temperature sensor and two LEDs. The LEDs will reflect the current status of the measured temperature. Depending on the temperature either the green or the red LED will be switched on. The temperature sensor will be simulated by a random function in python generating integers higher and lower than a defined threshold. We will publish/subscribe messages to MQTT topics/Mainflux channels. In the example two Raspberry Pis are used. You can do this setup also with one device. Instead of the simulated temperature sensor you could also use a real temperature sensor, e.g. the DHT22.

We will do the following steps:

  • Connect the LEDs to the second Raspberry Pi. We call it mypi2.
  • Install paho-mqtt on both Raspberry Pi
  • Define things and channels in Mainflux
  • Connect things to channels
  • Write/download a python script on the Raspberry Pi simulating the temperature sensor. We call it mypi1.
  • Write a few lines of python code on mypi2. This code will handle the on/off status of the LEDs

We need:

  • A mainflux installation running on a Linux Laptop with e.g. Centos 7.6,
  • 2 Raspberry Pi (in the exmple we use different models),
  • 1 breadboard,
  • a red and a green LED,
  • 4 cables (female/male),
  • 2 330 Ω resistors.

Connecting the LEDs

Raspberry PI GPIO Layout

All current Raspberry Pi have a 40-ping GPIO header. The image above shows the GPIO layout. First of all we put the two LEDs on our breadboard. Keep in mind the long pin is the anode and the short pin is cathode. We connect GND on Pin #6 to the cathode of the red LED. We connect the anode to GPIO17 which is Pin #11. Don’t forget to put a 330 Ω resistor anywhere in the circuit. We do the same for the green LED. Here we connect GND on Pin #14 to the cathode and GPIO27 to the anode of the green LED. We also put a 330 Ω resistor in the circuit.

Install paho-mqtt on Raspberry Pi

The installation of paho-mqtt is already described in this arcticle.

Create things and channels

Two things either with the mainflux-cli or the the web interface have to be created. In this example the things are called:

  • myled
  • mysensor

Then we create two channels:

  • datachannel
  • alarmchannel

The alarmchannel will be used for publishing messages which are used for communication between the two devices. Keys and ids will be needed it in the python scripts. Furthermore we have to connect the two things to the alarmchannel.

The datachannel will be used to publish data in SenML format. We just connect the mysensor to the datachannel. The datachannel wil be used to create dashboards in grafana. Zivorad Cosovic has written a good article about that. You can find it here. For this reason I will skip it. The focus will be kept on the communication of the 2 devices.

Publish/subscribe scripts

It is assumed that you have a user account pi on your Raspberry Pis and that the devices and the mainflux MQTT-broker can talk to each other. If you have internet access on your Raspberries you can directly download the scripts from github.

First you connect with your user pi to mypi1 and download the publish-temp.py to a place where you like, e.g. $HOME/bin

ssh pi@mypi1 
mkdir bin
cd ~/bin
wget https://raw.githubusercontent.com/hadoopch/mainflux-sensor-actuator/master/publish-temp.py
chmod 700 publish-temp.py

Afterward replace the following variables with your own values:

  • broker_address: IP of the docker host where the MQTT-Port is published
  • broker_port: Listen Port of your MQTT-Broker
  • thing_id: ID of the myled thing
  • thing_key : Key of the myled thing
  • datachannel: ID of the datachannel
  • alarmchannel : ID of the datachannel

Then start the publis-temp.py in a terminal:

mypi1:~bin $ ./publish-temp.py

The script is generating random integer between 21 and 24. If a value is greater than 22 the message red will send to the alarmchannel. Otherwise the message green is send. The generated temperature values are sent to the datachannel.

Then we go ahead with mypi2. You connect with your pi user and run the following commands

ssh pi@mypi2
mkdir bin
cd ~/bin
wget https://raw.githubusercontent.com/hadoopch/mainflux-sensor-actuator/master/led-alarm.py
chmod 700 led-alarm.py

Then replace the following variables with your values:

  • broker_address
  • broker_port
  • thing_id: ID of the mysensor thing
  • thing_key : Key of the mysensor thing
  • alarmchannel

Then start the python script led-alarm.py in a terminal of mypi2

pi@mypi2:~bin $ ./led-alarm.py
Temperature is OK
Temperature is too high
Temperature is OK
Temperature is OK

According to the published messages of mypi1 the connected LEDs change their status. Their is always only one LED on. The switch of the LEDs is related with an appropriate output in the mypi2 terminal.


Ulrich Tehrani, T-Systems Schweiz AG, Basel

Further infos