Hi Hacker ,
Looking at my relays I unfortunately got a batch of active low . They change state fine and also on shutdown or loss of power the relays are off . I am using a generic pi hat for a breakout board
For setting of gpio I am using a bash script saved in usr/bin/pin_12 on or off which has
#!/bin/bash
/usr/bin/gpioset gpiochip0 12=1
the exec script can control the pin to do what I need it to do . I only need to call the scipt to turn the output on or off
In openhab paper I am using command to call the bash script for pin 12 on and pin 12 off
this is linked with a item I created control heat relay on and another one for off
Next was rule I created a trigger based off the heating pin state
and then send a command to the relay item I created relay on to on relay off to off
The same type of rule is created for heating pin = off to change the state of the relays to send the comand to launch exec script to pin12_off
I repeated this same process for pin 18
I am also using a bme680 sensor instead of the bme 280 to utilize the air quality portion of the chip . To do this I rewrote ( with the help of ai) the bme280.py file
to utilize this you must install adafruit bme680 libary and the paho-mqtt to get airquality to openhab along with items to store air quality in openhab
#!/usr/bin/env python3
import time
import requests
import board
import busio
import adafruit_bme680
import paho.mqtt.client as mqtt
----------------------------
OpenHAB REST API settings
----------------------------
OH_HOST = “http://localhost:8080”
#ITEM_TEMP_C = “MyTemp”
ITEM_TEMP_F = “MyTemp”
ITEM_HUMI = “MyHumi”
ITEM_PRESS = “MyPressure”
ITEM_AIR_QUALITY = “MyAirQuality”
ITEM_AIR_QUALITY_CAT = “MyAirQualityCategory”
----------------------------
MQTT settings
----------------------------
MQTT_BROKER = “localhost”
MQTT_PORT = 1883
TOPIC_IAQ = “hestia/local/iaq”
TOPIC_IAQ_CAT = “hestia/local/iaq_category”
----------------------------
Initialize I2C and BME680
----------------------------
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_bme680.Adafruit_BME680_I2C(i2c, address=0x76)
Optional sensor settings
sensor.sea_level_pressure = 1013.25
sensor.temperature_oversample = 8
sensor.humidity_oversample = 2
sensor.pressure_oversample = 4
sensor.gas_oversample = 1
sensor.set_gas_heater_temperature = 320 # °C
sensor.set_gas_heater_duration = 150 # ms
sensor.gas_heater_profile = 0
----------------------------
Helper function to update OpenHAB item
----------------------------
def update_item(item, value):
url = f"{OH_HOST}/rest/items/{item}/state"
headers = {“Content-Type”: “text/plain”}
try:
response = requests.put(url, data=str(value), headers=headers)
if response.status_code in [200, 202]:
print(f"Updated {item}: {value}“)
else:
print(f"Failed to update {item}: {response.status_code} {response.text}”)
except Exception as e:
print(f"Error updating {item}: {e}")
def readIAQ():
“”“Compute IAQ index 0-500 from gas resistance”“”
gas = sensor.gas
if gas is None:
return 0
GAS_MIN = 5000.0
GAS_MAX = 500000.0
gas = max(GAS_MIN, min(GAS_MAX, gas))
iaq = int((gas - GAS_MIN) / (GAS_MAX - GAS_MIN) * 500)
return iaq
def readIAQCategory(iaq):
“”“Return EPA-style IAQ category”“”
if iaq <= 50:
return “Good”
elif iaq <= 100:
return “Moderate”
elif iaq <= 150:
return “Unhealthy for Sensitive Groups”
elif iaq <= 200:
return “Unhealthy”
elif iaq <= 300:
return “Very Unhealthy”
else:
return “Hazardous”
----------------------------
Initialize MQTT client
----------------------------
mqtt_client = mqtt.Client()
mqtt_client.connect(MQTT_BROKER, MQTT_PORT, 60)
----------------------------
Main loop: read sensor and push values
----------------------------
while True:
temp_c = round(sensor.temperature, 1)
temp_f = round((temp_c * 9 / 5) + 32, 1)
humidity = round(sensor.humidity, 1)
pressure = round(sensor.pressure*0.029529980,2)
iaq = readIAQ()
iaq_category = readIAQCategory(iaq)
print(f"Temp C: {temp_c}, Temp F: {temp_f}, Humidity: {humidity}, Pressure: {pressure}, IAQ: {iaq} ({iaq_category})")
# Update OpenHAB items
# update_item(ITEM_TEMP_C, temp_c)
update_item(ITEM_TEMP_F, temp_f)
update_item(ITEM_HUMI, humidity)
update_item(ITEM_PRESS, pressure)
update_item(ITEM_AIR_QUALITY, iaq)
update_item(ITEM_AIR_QUALITY_CAT, iaq_category)
# Publish to MQTT
mqtt_client.publish(TOPIC_IAQ, iaq)
mqtt_client.publish(TOPIC_IAQ_CAT, iaq_category)
time.sleep(10) # adjust the interval as needed
After getting script running I changed default.sitemap in /etc/openhab2/sitemaps and added
Frame label=“Environment” {
// Text item=MyPressure label =“Pressure [%s]”
Text item=MyAirQuality label=“Air Quality Index [%d]”
Text item=MyAirQualityCategory label=“Air Quality Category [%s]”
to get
}
I am positive that this is not the cleanest way to get up and running but I am seeing relays respond and airquality,temp and humidity change .
Still working on jsnode and how to display on screen . I was able to add IAQ to info page with number replacing where disk space was but thats it