-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.py
123 lines (95 loc) · 2.59 KB
/
monitor.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import os
import glob
import time
import lcddriver
import datetime
from vesync.api import VesyncApi
from time import strftime, localtime
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
lcd = lcddriver.lcd()
lcd.lcd_clear()
api = VesyncApi("EMAIL", "PASSWORD")
fridge_id = "ID"
heater_id = "ID"
min_temp = 68
max_temp = 72
mid_temp = ((max_temp - min_temp) / 2) + min_temp
refresh_time = 30
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f
def turn_on_fridge():
api.turn_on(fridge_id)
def turn_on_heater():
api.turn_on(heater_id)
def turn_off_fridge():
api.turn_off(fridge_id)
def turn_off_heater():
api.turn_off(heater_id)
turn_off_fridge()
turn_off_heater()
while True:
#Get devices
devices = api.get_devices()
fridge_status = "off"
heater_status = "off"
#Get heater and fridge status
for x in range(0, len(devices)):
if devices[x]["cid"] == fridge_id:
fridge_status = devices[x]["deviceStatus"]
elif devices[x]["cid"] == heater_id:
heater_status = devices[x]["deviceStatus"]
#Get temperature
temp = read_temp()[1]
#Get current time
current_time = strftime("%H:%M:%S", localtime())
#Print status of system
print '------------------------'
print 'Time:\t', current_time
print 'Temp:\t', temp
print 'Fridge:\t', fridge_status
print 'Heater:\t', heater_status
#Show temp on screen
lcd.lcd_clear()
lcd.lcd_display_string("{0} (F)".format(temp), 1)
#Show status and time on screen
if(fridge_status == "on" and heater_status == "on"):
lcd.lcd_display_string("ERROR", 2)
turn_off_fridge()
turn_off_heater()
elif(heater_status == "on"):
lcd.lcd_display_string("HEATING {0}".format(current_time), 2)
elif(heater_status == "on"):
lcd.lcd_display_string("COOLING {0}".format(current_time), 2)
else:
lcd.lcd_display_string("STANDBY {0}".format(current_time), 2)
#Turn fridge on if higher than max temp
if temp > max_temp:
turn_on_fridge()
turn_off_heater()
#Turn heater on if lower than min temp
elif temp < min_temp:
turn_on_heater()
turn_off_fridge()
#Turn off heater if higher than mid temp
elif temp > mid_temp:
turn_off_heater()
#turn off fridge if lower than mid temp
elif temp < mid_temp:
turn_off_fridge()
#Sleep
time.sleep(refresh_time)