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

How can I get data of DHT22 that connected to GPIO on Raspberry running Openhab. I a new in Openhab. Please help me

There isn’t any particular Binding to that in openHAB, instead you can use a python script on then use MQTT to load it into openHAB, that would be the easiest way to go.

1 Like

Could you write a demo for me (scripts, items, sitemaps)?!. Because I don’t understand how to use a Python script and MQTT. I a new in Openhab, I only control mode of LED via GPIO. Please help me @mr_eskildsen

Unfortunately writing demo scripts is a minor part of getting the suggested setup up and running OUTSIDE openHAB.
The DHT 22 sensor in’t straightforward, and I guess that knowledge about scripting (Python) would be helpfull.

First of all you will need a running instance of a MQTT server. That could be installed on you Raspberry,there is a lot of step by step guides around, here is one of them:


I haven’t tried that link, so I am not sure if it works, but in that case, Google is your best friend…

When MQTT is running you should decdie how you will read values from DHT22, a lot of samples for raspberry Pi is laying around, not just Python, but also others. Pick your favourite and test that it reads the sensor as expected. Then add MQTT publishing to the script.

Now you are ready to read the sensor values from MQTT. In openHAB, you find good examples under the Wiki for each Binding (MQTT Binding).

Hope that helps in pointing out a direction, but unfortunately it will involve a lot of work…

1 Like

Thank you for your time, I’m going to try it. See you again in other topic :slight_smile:

If you are having Openhab on the same raspberry pi where you have your sensor connected, why don’t you use the gpio binding? It’s the best solution for your setup!

1 Like

As far as i knowhow the DHT22 sensor can’t just jeg real by one reading. It need some processing , Which isn’t (IMHO) worth trying from a openhab rule, As far as I know, there isn’t any support in the GPIO Binding for reading the DHT22 sensor directly???

1 Like

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!!!