DHT11 + Arduino + RasPi + OpenHAB2

Hello everybody, I’m able to start with OpenHAB2 and follow the official tutorials, but…

I’m trying to do the following, and I’m not sure where to start or is it even possible…

I have a DHT11 sensor connected to an Arduino and I can get readings on the Raspberry Pi through USB pySerial connection, OpenHAB2 is configured and everything seems to work even the myopenhab account,

now, is it possible to add the sensor as a “thing” and get its readings remotely through myopenhab? and how?

Thank you all…

Probably easier to get your python code to push the dht11 values to mqtt and then use the mqtt binding to read it. Something I’ve done a lot on my OH v1 system.

But the next step of getting mqtt going I’m, also only just coming to grips with: Openhab2 - starting out - really don’t get it

2 Likes

Ok, so I followed your suggestion and now I have temperature and humidity readings being published to a MQTT topic every two seconds and I can read them from MQTT.fx

Also I have “MQTT Binding” installed in openHAB2,

But not sure where to go from here, How can I add a “thing” that displays those readings in openHAB2?

Well, I’ve done that by creating an item as of this page

Also, I’ve added a YahooWeather “thing” and linked it to the above item (I guess any “thing” would do just ok).

But is there a better way than this??!!

So you added the mqtt binding, then went and made an item (in a config file file) and then made a entry in a sitemap?

Or did you get access to the mqtt item from somewhere else (I might learn off you here).
ie: items:
Number bm2_humidity “Humidity [%.1f%%]” (g_bm,g_bm2,g_bm2_humidity,g_humidity) {mqtt="<[frodo:3a7c72e125cc/humidity:state:default]"}

frodo being the name of my mosquitto broker.
then in the home.sitemap I have:
Text item=bm2_humidity icon=“water”

But this means if I go to BasicUI I have to add sitemap=home - or set the default to my home sitemap

What I’ve done was:
0- Installed MQTT Binding from paperUI (as well as bunch of other bindings like Yahoo weather and the cloud connector)

1- used “uugear” to be able to send sensors readings from Arduino to Pi (USB connection), then “paho.mqtt” to be able to publish the readings to a MQTT topic from within a single python script

2- I made sure mosquitto is running, and I can publish and subscribe successfully (used MQTT.fx for this), I configured mqtt.cfg from /etc/openhab2/services by adding the line
aNameForTheBroker.url=tcp://mosquittoServerIPaddress:1883

3- then I added an .item file in /etc/openhab2/items with the following lines

Number temperature “Temperature [%.1f]” {mqtt="<[aNameForTheBroker:temp_mqtt:state:default]"}
Number humidity “Humidity [%.1f]” {mqtt="<[aNameForTheBroker:hum_mqtt:state:default]"}
Number dist “Distance [%.1f]” {mqtt="<[aNameForTheBroker:dis_mqtt:state:default]"}

4- Tried to add things using config files but couldn’t do that, so I used the Yahoo Weather thing instead, and linked its channels to the items I made earlier,

5- ran the python script, and voila… control page started to update accordingly…

I restarted the Pi and now openHAB2 doesn’t seem to be able to pick the published data like it did before.
bad luck I guess, not sure what’ve done wrong…

Never missed with sitemaps as I don’t really understand them yet…

How do you run you collection script (that connects to the sensor and publishes the temp?) - cron, internal loop, is it set to start on reboot?

It’s actually a hacked code from a dozen of websites, not sure how to describe it but it does the job I guess, anyway, here it is…

from time import sleep
from UUGear import *
from time import gmtime, strftime
import paho.mqtt.client as paho

broker = "192.168.137.54"
port = 1883

def on_publish(client, userdata, result):
    print("data published\n")
    pass

client1 = paho.Client("control1")
client1.on_publish = on_publish
client1.connect(broker, port)

UUGearDevice.setShowLogs(0)

device = UUGearDevice('UUGear-Arduino-9695-7515')

if device.isValid():
	for i in range(500):
		data = device.readDHT(4)
		humidity = (data >> 16) / 10
		if humidity > 100:
			humidity = (data >> 24)
		if humidity == -1:
			continue
		temperature = (data & 32767) / 10
		if temperature > 125:
			temperature = ((data & 32512) >> 8)
		if (data & 32768):
			temperature = -temperature
		distance = device.readSR04(5, 6)##trig, echo		
		client1.publish("hum_mqtt", humidity)
		client1.publish("tem_mqtt", temperature)
		client1.publish("dis_mqtt", distance)
		sleep(2)
	device.detach()
	device.stopDaemon()
else:
	print 'UUGear device is not correctly initialized.'

This is where you’ve started to go off track. The MQTT is an OH1.x binding, therefore it doesn’t work with the OH2 concept of Things (and therefore you can’t use PaperUI for it).

After step 3 you should be able to see the updates come through in the log, you will need to create a sitemap if you wish to have a UI to see these values.

1 Like

I found the problem preventing openHAB2 from reading the published values after a restart of the Raspberry pi (by examining the log thanks to “danielwalters86”)…

It was just that openHAB was starting before mosquitto and therefore wasn’t able to open the MQTT connections, simply by restarting openHAB while mosquitto is running solves the problem…