How can I get data of DHT22 that connected to GPIO on Raspberry running Openhab

Seriously? Why don’t you trying a little legwork for yourself. From this very forum: Read Temperature with Python and forward to MQTT. No you can’t use the GPIO binding because, to the best of my knowledge, that only detects high and low. Not enough to read a DHT22.

2 Likes

I have read DHT11 (or DHT22) successly.
This is my way that I have referred in other topic.
In items:

   Number Temp "Temperature [%.2f   C]" <temperature> {exec="<[/usr/bin/python /opt/openhab/configurations/scripts/AdafruitDHT.py:10000:REGEX((.*?))]"}

In AdafruitDHT.py:

import sys
import Adafruit_DHT

sensor = Adafruit_DHT.DHT11
pin = 4
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
     print('{0:0.2f}'.format(temperature))
else:
     print('Failed to get reading. Try again!')
     sys.exit(1)

I have installed AdafruitDHT library for Raspberry and used Exec binding

Thank you so much, I will try to read data of DHT with MQTT.

3 Likes

Nguyen_Dang_Nhut_Tan

Do you have puthon script to read Humidity?

Hi @daskalov_daskalov,
Above AdafruitDHT.py, it will return both humidity and temperature. But I only use temperature
If you want to get humidity. you must replace this line

print('{0:0.2f}'.format(humidity))

instead of

print('{0:0.2f}'.format(temperature))

or you can get both by:

print('Temp={0:0.1f}*  Humidity={1:0.1f}%'.format(temperature, humidity))
1 Like

@Nguyen_Dang_Nhut_Tan Nguyen can you post a “complete” step by step tutorial?
i follow your instructions but nothing happens in oh2.
if i start the pythonscript manually via ssh/terminal on my win-computer, i can see temp/humidity …but nothing in oh2 :frowning:

You likely need to change the regex expression in the items file. Try just reading the temperature rather than both and see if that works. If it does then that is likely your problem. If you return both as demonstrated in the code the regex for temp will need to select the first number and the regex for humidity will need to select the second number.

sry im a absolutely openhab2 newbie :frowning:
all what i have done are the steps what referred from nguyen.

item
Number Temp "Temperature [%.2f C]" <temperature> {exec="<[/usr/bin/python /opt/openhab/configurations/scripts/AdafruitDHT.py:10000:REGEX((.*?))]"}
python script
import sys
import Adafruit_DHT

sensor = Adafruit_DHT.DHT11
pin = 4
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
     print('{0:0.2f}'.format(temperature))
else:
     print('Failed to get reading. Try again!')
     sys.exit(1)

all what i see in openhab logs is "item temp removed…"
maybe you can help me

I have a similar approach and read the sensor values via SSH in a shell script. I paired the OpenHAB Server and the Raspberry with SSH-Keys and run something like that in a rule:

val TEMP = executeCommandLine("/x/x/openhab/runtime2/conf/scripts/rasp_keller.sh@@TEMP", 5000)
if (TEMP.toString().length <= 5) tmp_keller.postUpdate(TEMP)

where the shell script is something like:

#!/bin/bash
case $1 in
        TEMP) raw=$(ssh pi@192.168.x.x "sudo ./DHT22.py" | awk '{print $1}')
            ;;
        HUMID) raw=$(ssh pi@192.168.x.x "sudo ./DHT22.py" | awk '{print $2}')
            ;;
esac
echo "$raw"

The python script on the Raspberry is the example from Adafruit. Very easy and works perfectly, maybe not the most professional way, but okay for me.

Greetings

can you read local sensors with your ssh script?
maybe you can post a tutorial from your ssh setup

no, local on the raspberry, which has the DHTxx to his GPIO connected, i read the data with this example from adafruit: https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/software-install-updated

then i pair the OpenHAB Server with the raspberry via ssh keypairs (German: https://wiki.ubuntuusers.de/SSH/)

then i can execute that adafruit example remotely with my shell script from the OpenHAB Server and work with the answer and put them into my items via rules…

EDIT:

sorry, i didn’t see that you have the DHT connected on your openhab server…
Then you only need to take the adafruit example, put your sensor and sensorpin in there and execute it from within a rule!

you can build for example 2 *.py files, one for the temperature and one for the humidity and then get your values like i do within a rule

thats what i trying…but it doesnt works.
can you give me a rule and item example?
i can read all data via ssh and the example adafruit script, no problem.
but there are no data in oh2.

Try this:

Items:

Number	tmp_keller					"Keller [%.1f °C]"	
Number	humidity_keller		  			"Keller [%.1f %%]"

Rule:

rule "Keller"
when
	Time cron "0 */5 * * * ?"
then
	val TEMP = executeCommandLine("/pathToPythonScriptWithOnlyTemperature.py", 5000)
	if (TEMP.toString().length <= 5) tmp_keller.postUpdate(TEMP)
	Thread::sleep(5000)
	val HUMID = executeCommandLine("/pathToPythonScriptWithOnlyHumidity.py", 5000)
	if (HUMID.toString().length <= 5) humidity_keller.postUpdate(HUMID)
	logInfo("Keller", "Temperatur: " + TEMP.toString() + "°C, Feuchte: " + HUMID.toString() + "%")
end
3 Likes

wtf…it works now :scream:
1000 x thx fo your help.
next step: some graphs, but not today anymore :slight_smile:

sry but these are my first steps with openhab.
i understand the item, sitemap things but i should work with rules-wiki.

also dann danke nochmals stefan!!!

Cool! Glad i could help! Feel free to ask if there are questions within that small code!

Gerne, dafür ist das Ganze hier :wink:

Today i test the script, and i have strange reading from DHT11 sensor, the value of temperature and humidity from the sensors are floating. If temperature is 9 degree Celsius one reading is 9 next reading is 4. and humidity is the same if humidity is 35% one reading is 35% some readings after is 140%. I use DHT11 reading every 5 seconds, power sensors with 3.3V and i have resistor 4K8 to VCC and Signal. Trying also to read sensor every 60sek… the same think, down on the link is picture of the chart. Is anybody have idea whats wrong? what have to check?

https://www.dropbox.com/s/4vucx1en23tnzwv/Screenshot%202017-02-19%2012.29.40.png

Hi,
I have a DTH11, its working ok when I run it from the command line using horschte instructions, but the SItemap shows no values. The text and the icons are there but ther are no values for temp or the Humidity.

I should be missing something. There are no errors in the logs.

I also tried spy0r aproach and the same thing happens.

Any idea what I’m doing wrong ?

Thanks for the help

If you don’t provide any information that is almost impossible …
Post your items and sitemap entries and tell us on which topic you are receiving your temp and hum values.

1 Like

Hi,
Sorry for beeing short on the information. Here goes:

Sitemap:
sitemap default label=“Sitemap Vila Alice”{
Frame label=“Demo” {
Text item=Temp1 label="Temperatura1 [%.2f ºC]"
Switch item=Wifi_ZAP label="ZAP 192.168.1.1"
Text item=temp_k
Text item=humidity_keller
Text item=TempA
}}

Items:
Switch Wifi_ZAP “ZAP 192.168.1.1” {channel=“network:device:192_168_1_1:online”}
Number Temp1 “Temperatura1 [%.2f C]” {exec="<[python /etc/openhab2/scripts/temperatura.py:10000:REGEX((.?))]"}
Number temp_k “Keller [%.1f °C]”
Number humidity_keller “Keller[%.1f %%]”
Number TempA “TemperaturaA [%.2f C]” {exec="<[sudo /usr/bin/python /etc/openhab2/scripts/AdafruitDHT.py:10000:REGEX((.
?))]"}

Rules:
Keller.rules
rule "Keller"
when
Time cron "0 */5 * * * ?"
then
val TEMP = executeCommandLine(“python /etc/openhab2/scripts/temperatura.py”, 5000)
if (TEMP.toString().length <= 5) tmp_keller.postUpdate(TEMP)
Thread::sleep(5000)
val HUMID = executeCommandLine(“python /etc/openhab2/scripts/humidade.py”, 5000)
if (HUMID.toString().length <= 5) humidity_keller.postUpdate(HUMID)
logInfo(“Keller”, "Temperatur: " + TEMP.toString() + "°C, Feuchte: " + HUMID.toString() + “%”)
end

As I mentioned before temperture.py and AdafruitDHT.py both run ok on the command line.
There’s no error on the log
The sitemap apears Ok, but no values are dispalyed.
Thanks
Paulo

This is the display of the Sitemap (copy /paste):
Demo
Temperatura1

  • ºC
    ZAP 192.168.1.1
    Keller
  • °C
    Keller
  • %
    TemperaturaA
  • C

Unfortunately I have never used any Phyton scripts with the exec binding and therefore am not able to help.
The only hint I can give you: is openHAB able to read the *.py scripts?
When you execute them from the command line you normally have a different user (or even root) as whether you want openHAB (user:group openhab:openhab) to read the file …

1 Like