Month: May 2018

In an earlier tutorial I showed how to read the popular DHTxx sensor on a Raspberry Pi and then to MQTT that into Openhab.
However, there are more ways to skin a cat, so what I like to do this time is:

1) Read the sensor with a device tree overlay on a Raspi and then
2) Send the data to OpenHab with the REST-API

I am not saying one method is better than the other, but suppose you dont want to install an MQTT server because all your other channels don’t need MQTT, then the REST API comes in handy.

1) Reading the DHT sensor
First we need to load the dht11 overlay at boot-up
Dosudo nano /boot/config.txt
and add:

dtoverlay=dht11,gpiopin=4

obviously one can pick another pin here, or declare a different sensor such as the DHT22 or DHT21 (AM2301)

Once you have loaded the overlay and connected your sensor, a simple:cat /sys/devices/platform/dht11@0/iio:device0/in_temp_input,
would show you the temperature.
whereas:cat /sys/devices/platform/dht11@0/iio:device0/in_humidityrelative_input, would show you the humidity

Upon doing that in a python program, I noticed there can be quite a number of I/O errors. This seems to be a frequent problem with this overlay, so in order not to interrupt the program, I had to do a bit of Error catching.

2) Sending values to OpenHab
The REST API is fairly simple to use. We only need 1 line for every Item we want to update. Such a line looks like this:requests.put('http://192.168.xxx.yyy:800/rest/items/<YOUR ITEM>/state',value).
The ‘value’-variable needs to be a string.

Beware that Python programs rely on proper indenting. As this sometimes can be corrupted by publication on a website, I will leave an image of the properly indented program below:

As I am not the world’ s best python coder, I am sure improvements can be made, but at least this code will get you up and running fast.
Since the OpenHAB items are updated through the REST API, they do not need a channel in the itemsfile, that could look as simple as this:

This will just be a short piece, more like a tip to take into consideration.
While playing around with a Raspberry Pi Zero W, I had inserted a USB OTG ‘hub’ that could take a micro SD card and a USB stick and I had mounted the medium that was in there.
My goal was to use the USB stick to write picture files from a security camera to, in order to spare the SD card.
As also my ordered headers arrived, I shut down the Raspberry, soldered the headers and started up again.
To my horror, it wouldn’t start. Fearing that my soldering somehow had damaged the raspberry Pi, I exchanged cards with a Raspberry Pi B and restarted both. Thank god, the ‘start problem’ was related to the card, not the raspberry.
But how was that possible. It was a new card, and I always had properly shut down the raspberry it was in.
Then it dawned on me, could it be… no it couldn’t, could it??
I realised that after soldering, I had not reinserted the OTG hub with USB stick anymore. Could that be the problem??
After re-inserting the OTG hub with USB stick, my Zero started immediately again.
Obviously that is not the behaviour I want. I will see if I can do something about it, but for now I removed the mount.