How to add DHT11 senzor

Hello,
i’m using openhabian on raspberry pi. I have 2 dht11 sensors and i want one to monitor temperature and one humidity. I have also installed GPIO binding and now i don’t know what i’m suppose to write and define sensor in files(ssh connected) such as sitemaps, things, items,…
Thanks jaka

There are several examples on the forum, just search GPIO binding. Here’s one example:

so i added
items:

String Temp “Temperature [%.2f C]” {channel=“exec:command:temp:output”}
String Hum “Humidity [%d %%]” {channel=“exec:command:hum:output”}

scripts:
temp.py

#!/usr/bin/env python2.7

import sys

import Adafruit_DHT

sensor = Adafruit_DHT.DHT22

pin = 14

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)

hum.py

#!/usr/bin/env python2.7
import sys

import Adafruit_DHT

sensor = Adafruit_DHT.DHT22

pin = 14

humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

if humidity is not None and temperature is not None:

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

else:

 print('Failed to get reading. Try again!')

 sys.exit(1)

Things:
Thing exec:command:hum [command="/usr/bin/python2 /opt/openhab2/conf/scripts/hum.py", interval=15, autorun=true]

Thing exec:command:temp [command="/usr/bin/python2 /opt/openhab2/conf/scripts/temp.py", interval=15, autorun=true]

and nothing showed up in app

What app are you referring to?

You may want to create a sitemap with your items and check the logs (front tail, you can open this from the same web page where you select PapreUI) for error messages that will help identify the problem.

i mean the paper ui or basic ui, because when i added the relay same way, it has automatically showed up in there.

Jaka,
Did you manage to get this to work? I’m trying to do something similar and am struggling bad. If you got this to work, can you post what you did to make it work?

Thanks!

Just seeing this thread, but his py code above cannot work as is. He specified a DHT11 sensor in OP then that code calls for a DHT22. Granted, would just need to change sensor = Adafruit_DHT.DHT22 to sensor = Adafruit_DHT.DHT11 since it is included in that Adafruit_DHT library.

However, beyond that, I cannot help as python is not my strong suit. I have dealt with these DHT11 and 22 sensors before and connecting them to OH, but was connecting the sensors to an ESP8266 that posted updates to MQTT using Homie convention, then the Homie MQTT item is in OH.

If you are interested this is the script for DHT22(for a DHT11 is very similar)

import Adafruit_DHT

sensor = Adafruit_DHT.DHT22

pin = 14

humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

if humidity is not None and temperature is not None:
print(‘Temp={0:0.1f}*C Humidity={1:0.1f}%’.format(temperature, humidity))
else:
print(‘Failed to get reading. Try again!’)

and this is the output of the script
image

@mountbaldy I’ve been working on the temperature sensors for atleast 3 months now and i still haven’t found the issue. So far I made it to the point that the script is working and it’s posting temperature and humidity in the bash but not in the OH app.

@Jaka_Smode
There are probably many ways to skin this cat as far as getting your updates into OH.

Do you use a MQTT server with your OH install? If yes, you could modify your script to post the temp and humidity updates via MQTT.

Then you can just configure an MQTT item in OH for each.

I have a few esp8266 sensors that I created around my house that communicate their updates (temp, humidity and motion) to OH in that way. I coded the esp8266 chips to use Homie 3.0.1 convention in regards to MQTT. One of nice features of using that version of Homie, the updated MQTT binding (from OH 2.5 I believe) will auto discover the items in Paper UI.

@JMacIV can I ask you which tutorial did you use or how did you made it work with the esp8266(because I have nodeMCU and I want to give it a shoot)?

Flash your nodeMCU with either Tasmota or ESPEasy, install MQTT on openHAB.

For ESPEasy:
https://www.letscontrolit.com/wiki/index.php?title=DHT11_DHT22
https://www.letscontrolit.com/wiki/index.php?title=Tutorial_OpenHAB_Switch

1 Like

Thank you @sihui,
I’ve installed MQTT on the openhab, flashed nodeMCU and made it working with the DHT22(picure below)


and also set up the protocol

Maybe do you know how can I now link the sensor with the openhab in sitemap and item’s folder?
P.S. I’ve also checked the MQTT.fx and it seems to be working

If you already have your MQTT bridge configured with OH, then you will need to create a MQTT thing in OH for this. Check the docs here: https://www.openhab.org/addons/bindings/mqtt.generic/

If you do not have MQTT bridge configured, configure it before the Thing. Check here: https://www.openhab.org/addons/bindings/mqtt/

If you are still interested in my method, I just threw the code onto GitHub: https://github.com/JMacIV/ESP8266-homie-PIR-DHT

1 Like

Create a Generic MQTT Thing manually, add a number channel, fill in the topic you have chosen in your ESPEasy as a state topic, for example:

Create a number item and link the channel to it

then display the number item on your sitemap

https://www.openhab.org/docs/configuration/sitemaps.html#element-type-text

Text item=Temperature label="Livingroom [%.1f °C]" icon="temperature"
1 Like

so far I made MQTT broker


and created new generic thing sensor and linked it with bridge


and I hit the wall at the channel configuration because I don’t know how is MQTT state topic is named as.

Where can I find this information? MQTT.fx or in the espeasy software?


The first part on the ESPEasy config page (unit name), the second part on the ESPEasy device page:
grafik

In this case the state topic for the temperature would be:

/esp8266one/dht22/temperature

1 Like

I’m so sorry for all these question I’m totally new to the mqtt stuff.
Now I configured this channel and it shows offline status(sensor is online) is this a bad thing?

You haven’t yet configured the broker thing.