I have openHAB 2.5.3 running on a Raspberry Pi. It works fine.
I have several other Raspberrys which take temperature and humidity in several rooms of the house.
How can I get the data from the several Raspberrys in the openHAB to display the data?
I used python and mqtt to send sensor data to a pi running oh. Th oh pi also has mosquito mqtt broker installed and the mqtt binding. If you have openhabian the mosquitto can be installed from the oh config tool easilly.
The harder part was writing the python on the pi with the sensor and dealing with network connections failures automatically without crashing the sensor pi. I didn’t know anything when I started but was successful after some reading. It took me 1-2 weeks starting from 0 knowledge. So, with the will it can be done even if you’re a beginner.
I am not sure if what I did was the best way but it has been very reliable. The sensor pi just runs and runs and never needs any attention.
I gather temp and humidity with a similar sensor but use ESP8266 devices to communicate the info to OH. You can get the ESP8266 development boards cheap, flash them with EspEasy or Tasmota firmware and they use very little power (not that an RPI uses much).
Just a thought if you happen to need more sensor reading and wanna do it cheaper.
For the Raspberry who should send the data I use a SenseHat and made following setup:
pip3 install homie4
If I use this program on the Raspberry Pi:
# GOAL: send data with MQTT to openHAB
# last update: 20200407
# import what is needed
from sense_hat import SenseHat
from time import *
import time
from homie.device_temperature import Device_Temperature
# MQTT settings
mqtt_settings = {
'MQTT_BROKER' : '192.168.1.33',
'MQTT_PORT' : 1883
}
# definition of a device
temp_device = Device_Temperature(device_id="temp-sensor-1",name="Temp_Sensor_1",mqtt_settings=mqtt_settings)
# definition of the colours
red= (255,0,0)
black=(0,0,0)
# SenseHat from the Raspberry Pi
s = SenseHat()
while True:
# get the data needed
temp = round(s.get_temperature(),1)
pressure = round(s.get_pressure(),1)
humidity =round (s.get_humidity(),0)
ti= time.strftime("%H:%M:%S")
date = time.strftime("%Y%m%d")
# assemble one line of data
# data = date+";"+ti+";"+str(temp)+";"+str(pressure)+";"+str(humidity)
data = ti+" -- "+str(temp)
temp_device.update_temperature(s.get_temperature())
print("02 "+data)
s.show_message(data,scroll_speed=0.2, text_colour=red, back_colour=black)
sleep(20)
Hello,
on the openHab-side I restarted from scratch. I installed the MQTT-Binding and created a broker called MyBroker, without username and password. It goes online! So far ok.
On the Raspberry who should send the data I have exactly your program and when I start it I immediately get an error message.
# GOAL: send data with MQTT to openHAB
# last update: 20200411
# import what is needed
from sense_hat import SenseHat
from time import *
import time
import homie
from homie.device_temperature import Device_Temperature
# MQTT settings
mqtt_settings = {
'MQTT_BROKER' : '192.168.1.33',
'MQTT_PORT' : 1883,
'MQTT_USERNAME': None,
'MQTT_PASSWORD': None
}
# definition of the colours
red= (255,0,0)
black=(0,0,0)
# SenseHat from the Raspberry Pi
s = SenseHat()
try:
temperature_device = Device_Temperature(device_id="temperature-sensor-1",name = "Temperature_Sensor 1",mqtt_settings=mqtt_settings)
while True:
temperature = round(s.get_temperature(),1)
print(temperature)
temperature_device.update_temperature(temperature)
time.sleep(5)
except (KeyboardInterrupt, SystemExit):
print("Quitting.")