Skip to content

Instantly share code, notes, and snippets.

@gretel
Created March 13, 2018 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gretel/22b7b042ef0317d779e5d114710d1e17 to your computer and use it in GitHub Desktop.
Save gretel/22b7b042ef0317d779e5d114710d1e17 to your computer and use it in GitHub Desktop.
openbsd has native support for 'ugold' temperature/humidity sensors - this does publish the measured values via mqtt
#!/usr/bin/env python3
#
# https://gist.github.com/gretel/22b7b042ef0317d779e5d114710d1e17
MQTT_HOST='mqtt.jitter.local'
import subprocess
from time import sleep
import paho.mqtt.client as mqtt
def publish():
r = str(subprocess.check_output(["sysctl", "-n", "hw.sensors.ugold0.temp0"]), 'utf-8')
t = float(r.split(' ')[0])
print(t)
mqttc.publish("home/sensor/studio/temperature", t)
r = str(subprocess.check_output(["sysctl", "-n", "hw.sensors.ugold0.humidity0"]), 'utf-8')
h = float(r.split('%')[0])
print(h)
mqttc.publish("home/sensor/studio/humidity", h)
r = str(subprocess.check_output(["sysctl", "-n", "hw.sensors.km0.temp0"]), 'utf-8')
c = float(r.split(' ')[0])
print(c)
mqttc.publish("sys/sensor/svaha/cpu", c)
mqttc = mqtt.Client()
mqttc.connect(MQTT_HOST)
print('connected')
mqttc.loop_start()
while True:
publish()
sleep(15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment