- 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
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 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 https://www.carnivorousplants.co.uk/resources/raspberry-pi-terrarium-controller/. 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/AdafruitDHT.py 22 4
# This generates a file every 10 seconds
while true; do
temp=$(sudo python3 ~/Adafruit_Python_DHT/examples/AdafruitDHT.py 22 4)
sleep 10
done
RPi (Publisher)
Installation on the RPi was easy, there's plenty of resources out there, for example: https://randomnerdtutorials.com/how-to-install-mosquitto-broker-on-raspberry-pi/
Assuming your installation is successful then the following commands Publishes data from the RPi:
mosquitto_pub -d -t "testTopic" -m "Hello world!"
Where...
- -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
Test server (Subscriber)
mosquitto_sub -d -h 192.168.0.10 -t "testTopic"
Where...
- -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:
#!/bin/bash
# This generates a file every 10 seconds
while true; do
temp=$(sudo python3 ~/Adafruit_Python_DHT/examples/AdafruitDHT.py 22 4)
mosquitto_pub -d -t "temperature" -m " $temp "
sleep 10
done
RPi
Test server
No comments:
Post a comment