Skip to content

Instantly share code, notes, and snippets.

@robshep
Created March 27, 2018 15:52
Show Gist options
  • Save robshep/4281f5b3de5c3bcea5c1c3ef61ccba49 to your computer and use it in GitHub Desktop.
Save robshep/4281f5b3de5c3bcea5c1c3ef61ccba49 to your computer and use it in GitHub Desktop.
Temp from USB Temper to MQTT
#!/usr/bin/python3
# EASY-INSTALL-ENTRY-SCRIPT: 'temperusb==1.5.3','console_scripts','temper-poll'
__requires__ = 'temperusb==1.5.3'
from temperusb.temper import TemperHandler
from paho.mqtt.publish import single as mqtt_single
def getTempReading():
th = TemperHandler()
devs = th.get_devices()
readings = []
output = ''
for dev in devs:
sensors = [0] # range(dev.get_sensor_count())
temperatures = dev.get_temperatures(sensors=sensors)
humidities = dev.get_humidity(sensors=sensors)
combinations = {}
for k, v in temperatures.items():
c = v.copy()
try:
c.update(humidities[k])
except:
pass
combinations[k] = c
readings.append(combinations)
for i, reading in enumerate(readings):
dict_key = 'temperature_c'
for sensor in sorted(reading):
output += '%0.1f; ' % reading[sensor][dict_key]
output = output[0:len(output) - 2]
return output
t = getTempReading()
mqtt_single(topic=TOPIC, payload=t, qos=0, retain=False, hostname=HOST,
port=PORT, client_id="CLIENT", keepalive=60, will=None, auth=AUTH, tls=None,
protocol=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment