Initial commit
This commit is contained in:
commit
2afa8027d8
6 changed files with 289 additions and 0 deletions
93
post2mqtt.py
Executable file
93
post2mqtt.py
Executable file
|
|
@ -0,0 +1,93 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
import sys, string, time, atexit
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
# ---- Configuration START ----
|
||||
|
||||
topic = 'neues-haus/keller/tele/werkstatt/'
|
||||
mqttBroker = '192.168.0.11'
|
||||
mqttPort = 1883
|
||||
mqttuser = 'werkstatt-pi'
|
||||
mqttpasswort = 'h#st0$HYWq1Z2*'
|
||||
valuesFile = '/opt/1wire/output'
|
||||
maxDataAge = 300 # [s]
|
||||
minTemp = -10.0 # [°C], for simple sanity check
|
||||
maxTemp = 35.0 # [°C], for simple sanity check
|
||||
debug = True
|
||||
|
||||
# ---- Configuration END ----
|
||||
|
||||
# MQTT init
|
||||
def on_connect(client, userdata, flags, rc, properties):
|
||||
if rc != 0:
|
||||
print("Connection to MQTT broker failed with rc = ", rc)
|
||||
|
||||
try:
|
||||
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, 'werkstatt-pi')
|
||||
client.username_pw_set(mqttuser, mqttpasswort)
|
||||
client.on_connect = on_connect
|
||||
client.connect(mqttBroker, port=mqttPort)
|
||||
client.loop_start()
|
||||
except:
|
||||
print('Connection to MQTT broker failed')
|
||||
sys.exit()
|
||||
|
||||
# Function definitions
|
||||
|
||||
def exit_handler():
|
||||
client.publish(topic + 'LWT', 'Offline', 0, True)
|
||||
|
||||
def getValues():
|
||||
values = {}
|
||||
with open(valuesFile,"r") as f:
|
||||
for line in f:
|
||||
if debug:
|
||||
print('line = ' + line)
|
||||
|
||||
splitted = line.split('=')
|
||||
if len(splitted) == 2:
|
||||
values[splitted[0].strip()] = splitted[1].strip()
|
||||
|
||||
return values
|
||||
|
||||
def publishMetrics():
|
||||
payload = '{ "timestamp": "' + values['Timestamp'] + '"'
|
||||
for key in ['LuftTemp', 'WasserTemp', 'BodenTemp']:
|
||||
if key in values and float(values[key]) >= minTemp and float(values[key]) <= maxTemp:
|
||||
payload = payload + ', "' + key + '": "' + values[key] + '"'
|
||||
|
||||
if debug:
|
||||
print(key + ' = ' + values[key] + ' °C')
|
||||
|
||||
payload = payload + ' }'
|
||||
|
||||
if debug:
|
||||
print('payload = ' + payload)
|
||||
|
||||
client.publish(topic + 'SENSOR', payload)
|
||||
|
||||
|
||||
# Main
|
||||
|
||||
client.publish(topic + 'LWT', 'Online', 0, True)
|
||||
atexit.register(exit_handler)
|
||||
|
||||
while(1):
|
||||
values = getValues()
|
||||
|
||||
if not 'Timestamp' in values:
|
||||
continue
|
||||
|
||||
dataAge = int(time.time() - int(values['Timestamp']))
|
||||
|
||||
if debug:
|
||||
print('Data age = ' + str(dataAge) + ' seconds')
|
||||
|
||||
if dataAge > maxDataAge:
|
||||
continue
|
||||
|
||||
publishMetrics()
|
||||
|
||||
time.sleep(60)
|
||||
Loading…
Add table
Add a link
Reference in a new issue