Thursday, March 15, 2018

RaspberryPi as sensor hub - Read DHT11 with custom tool.

To enable some "smart home" functionality I wanted to introduce sensor values into the mix and create a way to control devices based on the sensor readings.

RPI with sensor backpack
Connecting a light, sound, temperature and motion detector to a RaspberryPi is easy enough. A breadboard with some connectors and the setup is finished.  You see a
connected to the board. (There is also a 433Mhz receiver and transmitter module visible, but these unrelated to this post) So the HW setup was pretty straight forward and reading the on the command line was also soon established.
Temperature readings in smartVISU interface
However for reading the values and reporting them to be available for other smart components via MQTT messages took some more effort.

The first idea was to write a small tool to read the DHT11 sensor values and send them out to the MQTT broker so the readings can be picked up by FHEM and be traced and reported in the web interface.

You can find the sources to my ReadDHT11 tool at GitHub. Use it as starting point if you like to experiment yourself.

It uses the wiringPI library to access the GPIO pins. The Mosquitto library is used to communicate to the MQTT broker.  All the configuration is done in the source code. Please adjust them to your needs.

Room temperature changes slowly so reading the sensor at long intervals is acceptable and a cron job performing this task every five minutes works well as you can see in the smartVISU temperature plot.

To read the other sensors a cron job is not suitable. The values may change quickly and you don't want to wait for the light to turn on once motion was detected. It shall happen without noticeable delay.

The solution is a simple daemon reading the sensors in a infinite loop and send out MQTT messages whenever a value change has been detected.

This will be the topic my next post. So stay tuned!

No comments:

Post a Comment