-
Notifications
You must be signed in to change notification settings - Fork 0
/
thermostat.py
237 lines (193 loc) · 6.27 KB
/
thermostat.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/python
import subprocess
import sys
import os
import json
from datetime import datetime
import thermostat_config as config
import RPi.GPIO as GPIO
import Adafruit_DHT
os.environ['PYTHON_EGG_CACHE'] = '__pycache__'
APP_PATH = os.path.dirname(os.path.abspath(__file__))
class thermostat:
__db = {}
def __init__(self, **kwargs):
# Setup GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup([config.HEAT_PIN, config.COOL_PIN], GPIO.OUT)
if (config.HEAT_TWO_STAGE):
GPIO.setup([config.HEAT_STAGE_2_PIN], GPIO.OUT)
if (config.COOL_TWO_STAGE):
GPIO.setup([config.COOL_STAGE_2_PIN], GPIO.OUT)
self.__load_db()
return
''' Read-only attribute'''
@property
def Target_Temperature(self):
return self.__db["target_temperature"]
@property
def Mode(self):
return self.__db["mode"]
@property
def Schedule(self):
return self.__db["schedule"]
@property
def Current_Temperature(self):
sensor = Adafruit_DHT.DHT22
humidity, temp = Adafruit_DHT.read_retry(sensor, config.SENSOR_PIN)
temp = round(temp * 9/5.0 + 32, 2) + config.CALIBRATION
return temp
@property
def Current_Temperature_Humidity(self):
sensor = Adafruit_DHT.DHT22
humidity, temp = Adafruit_DHT.read_retry(sensor, config.SENSOR_PIN)
temp = round(temp * 9/5.0 + 32, 2) + config.CALIBRATION
humidity = round(humidity, 2)
return temp, humidity
@property
def Status(self):
if config.HEAT_TWO_STAGE and GPIO.input(config.HEAT_STAGE_2_PIN) == GPIO.LOW:
return "HEAT", "2"
elif config.COOL_TWO_STAGE and GPIO.input(config.COOL_STAGE_2_PIN) == GPIO.LOW:
return "COOL", "2"
elif GPIO.input(config.HEAT_PIN) == GPIO.LOW:
return "HEAT", "1" if config.HEAT_TWO_STAGE else ""
elif GPIO.input(config.COOL_PIN) == GPIO.LOW:
return "COOL", "1" if config.COOL_TWO_STAGE else ""
else:
return "OFF", ""
def Set(self, targetTemp, Mode, schedule=None):
if (schedule==None):
schedule = datetime.now().strftime('%Y-%m-%d %H:%M')
self.__db["mode"]=Mode
self.__db["target_temperature"]=int(targetTemp)
self.__db["schedule"]=schedule
self.__save_db()
if config.PERFORM_IMIDIATE_ACTION or Mode == "OFF":
self.Process()
def Process(self):
temp = self.Current_Temperature
mode = self.__db["mode"]
target_temperature = self.__db["target_temperature"]
schedule = self.__db["schedule"]
schedule = datetime.strptime(schedule, '%Y-%m-%d %H:%M')
# LOW = ON
# HIGH = OFF
# Perfrom process only if current date and time aftre scheduled one except "OFF". "OFF" performed imideately
if (datetime.now() > schedule or mode=="OFF"):
if target_temperature - temp > 3 and (mode == "HEAT" or mode == "AUTO"):
# Set HEAT_2=ON, COOL=OFF
GPIO.output([config.HEAT_PIN, config.COOL_PIN],(GPIO.LOW, GPIO.HIGH))
if (config.HEAT_TWO_STAGE):
GPIO.output([config.HEAT_STAGE_2_PIN],GPIO.LOW)
if (config.COOL_TWO_STAGE):
GPIO.output([config.COOL_STAGE_2_PIN],GPIO.HIGH)
return "ON"
elif target_temperature - temp > 1 and (mode == "HEAT" or mode == "AUTO"):
# Set HEAT_1=ON, COOL=OFF
GPIO.output([config.HEAT_PIN, config.COOL_PIN],(GPIO.LOW, GPIO.HIGH))
if (config.HEAT_TWO_STAGE):
GPIO.output([config.HEAT_STAGE_2_PIN],GPIO.HIGH)
if (config.COOL_TWO_STAGE):
GPIO.output([config.COOL_STAGE_2_PIN],GPIO.HIGH)
return "ON"
elif temp - target_temperature > 3 and (mode == "COOL" or mode == "AUTO"):
# Set HEAT=OFF, COOL=ON"
GPIO.output([config.HEAT_PIN, config.COOL_PIN],(GPIO.HIGH, GPIO.LOW))
if (config.HEAT_TWO_STAGE):
GPIO.output([config.HEAT_STAGE_2_PIN],GPIO.HIGH)
if (config.COOL_TWO_STAGE):
GPIO.output([config.COOL_STAGE_2_PIN],GPIO.LOW)
return "ON"
elif temp - target_temperature > 1 and (mode == "COOL" or mode == "AUTO"):
# Set HEAT = OFF, COOL=ON
GPIO.output([config.HEAT_PIN, config.COOL_PIN],(GPIO.HIGH, GPIO.LOW))
if (config.HEAT_TWO_STAGE):
GPIO.output([config.HEAT_STAGE_2_PIN],GPIO.HIGH)
if (config.COOL_TWO_STAGE):
GPIO.output([config.COOL_STAGE_2_PIN],GPIO.HIGH)
return "ON"
else:
GPIO.output([config.HEAT_PIN, config.COOL_PIN],(GPIO.HIGH, GPIO.HIGH))
if (config.HEAT_TWO_STAGE):
GPIO.output([config.HEAT_STAGE_2_PIN],GPIO.HIGH)
if (config.COOL_TWO_STAGE):
GPIO.output([config.COOL_STAGE_2_PIN],GPIO.HIGH)
return "OFF"
GPIO.cleanup()
def Initialize(self):
#Initialize GPIO Hardware
GPIO.output([config.HEAT_PIN, config.COOL_PIN], GPIO.HIGH)
if (config.HEAT_TWO_STAGE):
GPIO.output([config.HEAT_STAGE_2_PIN], GPIO.HIGH)
if (config.COOL_TWO_STAGE):
GPIO.output([config.COOL_STAGE_2_PIN], GPIO.HIGH)
print ("GPIO Initialized")
def __load_db(self):
if (os.path.exists(APP_PATH + "/db.json")):
with open(APP_PATH + '/db.json', 'r') as db_file:
self.__db = json.load(db_file)
else:
self.__db = {"mode": "OFF",
"target_temperature": 72,
"schedule": datetime.now().isoformat()}
self.__save_db()
return
def __save_db(self):
with open(APP_PATH + "/db.json", "w+") as db_file:
db_file.write(json.dumps(self.__db, indent=4))
return
def get():
t = thermostat()
tp,h=t.Current_Temperature_Humidity
s,st=t.Status
print(" ")
print("Temperature: " +str(tp) + "F")
print("Humidity: " +str(h) + "%")
print("Status: " + s + " " + str(st))
print(" ")
print ("Mode: " + str(t.Mode))
print ("Target Temperature: " + str(t.Target_Temperature) + "F")
print ("Schedule: " + str(t.Schedule))
print(" ")
def set():
t=thermostat()
targetTemp=t.Target_Temperature
mode=t.Mode
schedule = None
a=sys.argv
i=2
while i < len(a):
if a[i]=="-t" and a[i+1]!='':
targetTemp=a[i+1]
i+=2
continue
if a[i]=="-m" and a[i+1]!='':
mode=a[i+1]
i+=2
continue
if a[i]=="-s" and a[i+1]!='':
schedule=a[i+1]
i+=2
continue
i+=1
t.Set(targetTemp,mode,schedule)
get()
def proc():
t=thermostat()
t.Process()
if (__name__=="__main__"):
if (len(sys.argv)<=1):
get()
elif sys.argv[1]=="get":
get()
elif sys.argv[1]=="set":
set()
elif sys.argv[1]=="proc":
proc()
elif sys.argv[1]=="init":
t=thermostat()
t.Initialize()
else:
get()