Wednesday, 19 August 2020

Measuring Temperature with Raspberri Pi and DHT22, and Publishing with MQTT

I'm interested in setting up MQTT to see if I can monitor the traffic between endpoints.  This is both for work and out of interest as I'm trying to do some more with my Raspberry Pi.  The plan is to use my test server as an MQTT Subscriber to read temperate coming from my Raspberry Pi, which is acting as an MQTT Publisher.
A lot of different are used when talking about MQTT but I'm sticking with following definitions:
  • Publisher - The thing that sends the data
  • Broker - The central point for lots of different data sources
  • Subscriber - The thing that wants to read the data from the Publisher
Depending on the size/complexity of the system the Publisher and Broker may be the same system, and it is in my case i.e. the Raspberry Pi is both Publisher and Broker.

The following diagram shows my setup:


  • Raspberry Pi - MQTT Publisher/Brooker – Sends (publishes) the MQTT messages
  • Lenovo – MQTT Subscriber – Subscribes to the MQTT messages
The first thing to do is to get the RPi to generate some data.  I'm using the DHT22 sensor to read temperature and humidity.  I've the directions from to setup the RPi, with the following diagram showing the wiring:















The physical setup worked well until I started to do the coding for the DHT22, I had numerous problems getting the Adafruit libraries working.  I found that instead of point 2 in PiMyLifeUp I had better results using  Around halfway down the page the author talks about using the GPIO Zero library, which was more successful for me.

So the following code produced the desired results:

sudo python3 ~/Adafruit_Python_DHT/examples/ 22 4

Fitting into a bash script to run the query in a loop looks the following:
# This generates a file every 10 seconds

while true; do
temp=$(sudo python3 ~/Adafruit_Python_DHT/examples/ 22 4)
sleep 10

So the next part is getting MQTT setup.  I spent ages on this but it turns out that it shouldn't have been that difficult.
For the MQTT setup I used Mosquitto MQTT.  While Mosquitto works really well, the documentation is only really useful when you know what you're doing.  Mosquitto has distributions for Windows, Mac and Linux (including RPi).  I found the following site useful when I was getting started:  It seems that the distributions support using Mosquitto as either an MQTT Publisher or Subscriber, Broker is also supported but I never used that.

My setup for Mosquitto was installing on my RPi and Windows test server as follows:

RPi (Publisher)

Installation on the RPi was easy, there's plenty of resources out there, for example:

Assuming your installation is successful then the following commands Publishes data from the RPi: 

mosquitto_pub -d -t "testTopic" -m "Hello world!"


  •  -d is the debug option to see what's happening
  • -t is the Topic (think of it a subject heading)
  • -m is the message you want to send 


Note however there's nothing listening yet...


Test server (Subscriber)

The Windows install is easy to follow as well, just install the download from the Mosquitto site.  All you need to do is run the following command once installed (from the Mosquitto directory):

mosquitto_sub -d -h -t "testTopic"


  • -d  is the debug option to see what's happening
  • -h is the host i.e. the thing that's sending the data, in my case my RPi
  • -t is the Topic i.e. the subject you want to listen for



Once you have MQTT setup you can then substitute the MQTT commands into your bash script that generates the temperature.  So my script on the RPi is as follows:

# This generates a file every 10 seconds

while true; do
temp=$(sudo python3 ~/Adafruit_Python_DHT/examples/ 22 4)
mosquitto_pub -d -t "temperature" -m " $temp "
sleep 10

So I now have...



Test server

My next step is a brief run through of what you can see using Wireshark, which is the interesting bit for me.  I'll put that in a post shortly.  Simply put, using this MQTT approach all your traffic is in clear text, which is what I'll show.  There is an option to encrypt your traffic but I don't have that working yet, and it'll be another post once I figure it out.  MQTT does have the option to use username and password, I want to see if I can see this detail with Wireshark as well.