Raspberry Pi OpenHAB2 GPIO DHT11 temperature and humidity sensor exec bindings and logging

Hi Everyone

I have recently installed openHABian on my Pi 3 and have the demo house all up and running. I have also got it nicely connected to my.openhab and im able to modify the sitemap and item files and have been using Paper UI to add bindings (my.openHAB and the exec binding).

I have attached a DHT11 module to GPIO pin 4 on the Pi and I have successfully read back the temperature & humidity via a python script.

I am now tripping over being able to implement this sensor inside openHAB2 and get it to appear in the UI correctly. I cannot figure out how to use the exec binding (if indeed this is this is the best method?) and furthermore I have tried to use the logging (Karaf Console) also with little success after reading loads on forums. There is very little on how to use the exec binding on the forums and what is available gets confusing if its for OH1 or OH2 there appears to be a difference!

I have been trying to follow this thread

And my attempt at an exec binding is as follows, created a python script:

     #!/usr/bin/python
     import sys
     import Adafruit_DHT
     humidity, temperature = Adafruit_DHT.read_retry(11, 4)
     print '{0:0.1f}'.format(temperature)
     #print 'Temp: {0:0.1f} C  Humidity: {1:0.1f} %'.format(temperature, humidity)

And on the Pi console I get this (if I run as sudo):

 [17:23:39] pi@openHABianPi:/etc/openhab2/scripts$ sudo ./DHT11-read-temp-humd.py
 [sudo] password for pi:*******
 2.0

I thought the sudo might be affecting things so I created a simpler script to just return a number:

 #!/usr/bin/python
 print '10'

 [17:23:54] pi@openHABianPi:/etc/openhab2/scripts$ ./DHT11-read-temp-humd_test.py
 10

Permissions are all 777 for both. I have then added this line to my demo.items:

Number Temperature_FF_Workshop 	"Temperature [%.1f °C]" <temperature> 	(Temperature, FF_Workshop) {exec="<[/usr/bin/python /etc/openhab2/scripts/DHT11-read-temp-humd_test.py:10000:REGEX((.*?))]"}

And I get this is the UI (and this is the same as I got with the DHT11 read python script so it looks like 20 is some default value!)

So following the thread I have tried to implement this in rules so I can see logging info but unfortunately im very confused by the Karaf logging in OH2. I added this line to my demo.rules:

val tempres executeCommandLine("/etc/openhab2/scripts/DHT11-read-temp-humd_test.py", 5000) logInfo("TestTempRes", tempres)

And I have the following log set

openhab> log:get all
Logger                                                      | Level
-------------------------------------------------------------------
ROOT                                                        | ERROR
javax.jmdns                                                 | ERROR
org.eclipse.smarthome                                       | DEBUG
org.jupnp                                                   | ERROR
org.openhab                                                 | INFO
org.ops4j.pax.web.service.jetty.internal.JettyServerWrapper | ERROR
smarthome.event                                             | DEBUG
smarthome.event.InboxUpdatedEvent                           | ERROR
smarthome.event.ItemStateEvent                              | ERROR
smarthome.event.ThingStatusInfoEvent                        | ERROR

But I see nothing in the log:tail at all.

I have double checked the exec binding is installed :

Any help would be much appreciated.

Thanks In advance

The broken image link above that I cant seem to fix in an edit is this :

You may not have picked the easiest starter project.
The exec binding seems to create quite a bit of problems and you can find extensive threads on this forum. You may have noticed that there are two versions available (differentiated by the version numbers 1.9 and 2.0 respectively). You seem to use version 2.0 that appears not trivial to use. In addition, I have not seen you talk about your thing definition
not sure why that is, but if you are not familiar you will have to read up on this (docs.openhab.org)
For example, I have finally managed to run a script (exec ver2.0) with this manual thing definition:

Thing exec:command:iphonecheck [command="/home/pi/scripts/iPhoneDetect.sh]

and corresponding item definition that reads like that:

String phone2 "At Home [%s]" (All) { channel="exec:command:iphonecheck:output" }

The script itself returns a string as output.

One aspect to keep in mind are to give your shell files the right permissions and ownerships. A lot of discussions can be found on this forum, but again, this may not be the best start into OH2.
You may want to consider using sensors that can connect through MQTT.
Lastly, and I believe you noticed that already, it will be critical for you to keep in mind that OH1.8 is very different from OH2, and in particular the ver1.9 and ver2.0 of the exec binding acts very differently. In addition if you search the forum older threads may make reference to even more obsolete exec versions.
Hope this helps somewhat.

Just reading the forum, I got reminded that there is also a GPIO binding that should allow you to access GPIO pins directly (without a python script and exec binding); personally I have no experience, but it may answer some of your needs. If memory serves me well, DHT11 may be readable that way, if you need any processing of signals before they are human readable, that may still not be the way to go and mysensors.org or similar platforms may be a better way.

Thanks for the reply, you have confirmed some of the things I have been reading and thinking.

I didnt think I needed a thing def as the exec can be done in the item direct? I will read up some more on things, thanks for the pointer.

I have seen a few thing on this, but I cant see how it can operate the protocol for the DHT11, I think the GPIO binding can do the more popular 1-wire and that is something I also need to implement/use for my project.

Any ideas or advise on the logging?

Thanks for your help

Not really, sorry. I guess you already figured out that Karaf has package specific log-level options, so aside from setting debug level of the exec binding, I am fresh out of of ideas.

Beware of the versions. As far as I understand, OH2 and 2.0bindings are running over things and you can link items to channels of your things. 1.9bindings need to be accessed differently (you would need to consult the wiki for OH1.8 for that).

hello, i’m curious if you succesfuly connected dht11 to gpio?

Yes I did, but I ended up moving over to hassio.

I can barely remember the details, but here are a few file entries I can see from my backup 


demo.items:
String dht11Value0 “Loft Temp [%s°C]” (gHeating, gHeatingCharts) {channel=“exec:command:dht11temp:output”}
String dht11Value1 “Loft Humd [%d%%]” (gHeating, gHeatingCharts, gWeatherHumidityChart) {channel=“exec:command:dht11humd:output”}
String onewiretemp7Value “Boiler Flow(7) [%s°C]” (gHeating, gHeatingCharts, gHeatingChartBoiler) {channel=“exec:command:onewiretemp7:output”}
String onewiretemp9Value “Boiler Return(9) [%s°C]” (gHeating, gHeatingCharts, gHeatingChartBoiler) {channel=“exec:command:onewiretemp9:output”}
String BoilerFlowTempTodayMinMax “Boiler Flow Today Min/Max [%s]” (gHeating)

demo.things:
exec:command:dht11temp [command=“sudo python /etc/openhab2/scripts/DHT11-read-temp.py”]
exec:command:dht11humd [command=“sudo python /etc/openhab2/scripts/DHT11-read-humd.py”]
exec:command:onewiretemp0 [command=“bash /etc/openhab2/scripts/ds18b20_read.bash 28-0000063fd1c0”]
exec:command:onewiretemp1 [command=“bash /etc/openhab2/scripts/ds18b20_read.bash 28-0000063da1ac”]
exec:command:onewiretemp2 [command=“bash /etc/openhab2/scripts/ds18b20_read.bash 28-0000063da262”]
exec:command:onewiretemp3 [command=“bash /etc/openhab2/scripts/ds18b20_read.bash 28-0000063fcf4a”]
exec:command:onewiretemp4 [command=“bash /etc/openhab2/scripts/ds18b20_read.bash 28-0000063da496”]
exec:command:onewiretemp5 [command=“bash /etc/openhab2/scripts/ds18b20_read.bash 28-0000063f3141”]
exec:command:onewiretemp7 [command=“bash /etc/openhab2/scripts/ds18b20_read.bash 28-0000063f7dfd”]
exec:command:onewiretemp9 [command=“bash /etc/openhab2/scripts/ds18b20_read.bash 28-0000063ef6bf”]

scripts/DHT11-read-temp.py
#!/usr/bin/python
import sys
import Adafruit_DHT
humidity, temperature = Adafruit_DHT.read_retry(11, 14)
#print ‘{0:0.1f}’.format(temperature)
print ‘{0:0.1f}’.format(humidity)
#print ‘Temp: {0:0.1f} C Humidity: {1:0.1f} %’.format(temperature, humidity)

Hope that helps

1 Like