How to get data from a RaspberryPi?

Hello,

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?

Thanks a lot for any advice.
HouCh.

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 posted an overview of the project here

What kind of sensors are you using?

Another option is to set up a Network File Sharing (NFS), and do the scripting on the machine hosting OH.

Hi, I am using temperature sensors DS18B20.

Are you using Python? If so which package are you using to read values?

Yes I am using Python. Here is the Python code, it is very small!

import from GPIO Zero library

from gpiozero import LED

import the sleep function from the time module

from time import sleep

import os
import glob
import time

os.system(‘modprobe w1-gpio’)
os.system(‘modprobe w1-therm’)

base_dir = ‘/sys/bus/w1/devices/’
device_folder = glob.glob(base_dir + ‘28*’)[0]
device_file = device_folder + ‘/w1_slave’

defining the GPIO-pin of the red LED

LED_red = LED(17)

defining sleep duration

sleep_duration = 1

def read_temp_raw():
f = open(device_file, ‘r’)
lines = f.readlines()
f.close()
return lines

def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != ‘YES’:
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find(‘t=’)
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f

while True:
LED_red.on()
deg_c, deg_f = read_temp()
print(deg_c,"°C ",deg_f,“°F”)
time.sleep(1)
LED_red.off()
time.sleep(3)

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. :wink:

1 Like

You can do this easily using MQTT and the Homie convention.

You will need to setup a MQTT Broker and the MQTT binding in OH

Download the Homie4 library.

pip3 install homie4

Example below is using an AM2303. Easy to change to the DS18B20. Once you start the example code, the device will appear in the OH In Box.

Use MQTT Explorer to troubleshoot.

import Adafruit_DHT
import time

from homie.device_temperature import Device_Temperature

mqtt_settings = {
    'MQTT_BROKER' : 'OpenHAB',
    'MQTT_PORT' : 1883,
}


try:

    temperature_device = Device_Temperature(device_id="temperature-sensor-1",name = "Temperature_Sensor 1",mqtt_settings=mqtt_settings)
    sensor = Adafruit_DHT.AM2302
    pin = 4

    
    while True:
        humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
        print(temperature)
        temperature_device.update_temperature(temperature)
        time.sleep(5)

except (KeyboardInterrupt, SystemExit):
    print("Quitting.")   
1 Like

Ok thank you. I will try this in the next few days, and keep you updated!!

Hello. I tried out some configurations and have some things running and some not!!
On the side of openHab I have the following configuration:

Capture d'écran 2020-04-07 16.49.27

Capture d'écran 2020-04-07 16.49.41

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)


I get this error:

??

In MQTT Explorer I see:

Thanks for your help… :wink:

Remove the username/password from the mqtt broker or add the following to the MQTT settings

    "MQTT_USERNAME": None,
    "MQTT_PASSWORD": None,

Note: I don’t use authentication and haven’t tested this code thoroughly.

if I remove the username/password, the Broker goes immediately offline…

you have to remove it from the broker, are you using Mosquito?

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.
Capture d'écran 2020-04-12 21.05.07

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.")   

here the error output:
Capture d'écran 2020-04-12 21.05.24

so the program stops immediately.

Sorry a development version get released. Reinstall Homie 4 using

pip3 install homie4==0.3.0