-
Notifications
You must be signed in to change notification settings - Fork 0
/
heater_control.py
61 lines (55 loc) · 1.74 KB
/
heater_control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#! /usr/bin/env python3
import json
import paho.mqtt.client as mqtt
import time
import datetime
client = mqtt.Client()
last_message = None
rooms = {}
def on_message(client, userdata, message):
global last_message
global rooms
result = str(message.payload.decode("utf-8"))
topic = message.topic.split('/')
room = topic[2]
data = json.loads(result)
if topic[1] == 'shellyht-6BABF1':
room = 'shop'
temp = data['tmp']['value']
hum = data['hum']['value']
elif topic[1] == 'sensors':
temp = data['temp']
hum = data['hum']
else:
temp = data['state']['temperature']
hum = data['state']['humidity']
rooms[room] = {
"temp":temp, "hum": hum
}
myFile = open("sensor_states.txt", "w")
for rm in rooms:
myFile.write(rm+ ': ' + str(round(rooms[rm]['temp'],2)) + ' ' + str(round(rooms[rm]['hum'],2)) + '\n')
myFile.close()
last_message = result
def failure_detected():
print('Detected MQTT failure. Sending power cycle command and waiting two minutes.')
time.sleep(120)
if __name__ == "__main__":
client.on_message = on_message
client.connect('192.168.2.200')
client.subscribe('smarter_circuits/sensors/#')
client.subscribe('smarter_circuits/thermostats/#')
client.subscribe('shellies/shellyht-6BABF1/info')
client.loop_start()
running = True
while running is True:
# pass
# now = datetime.datetime.now().strftime("%m-%d-%Y %H:%M:%S")
# print("Sending: "+now)
# client.publish(topic,now)
# time.sleep(2)
# if last_message is not None and last_message != now:
# failure_detected()
time.sleep(1)
client.loop_stop()
client.disconnect()