-
Notifications
You must be signed in to change notification settings - Fork 7
/
rubustat_daemon.py
executable file
·295 lines (251 loc) · 12.8 KB
/
rubustat_daemon.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#! /usr/bin/python
import sys
import subprocess
import os
import time
import RPi.GPIO as GPIO
import datetime
import ConfigParser
from daemon import Daemon
from getIndoorTemp import getIndoorTemp
#set working directory to where "rubustat_daemon.py" is
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
#read values from the config file
config = ConfigParser.ConfigParser()
config.read("config.txt")
DEBUG = int(config.get('main','DEBUG'))
active_hysteresis = float(config.get('main','active_hysteresis'))
inactive_hysteresis = float(config.get('main','inactive_hysteresis'))
HEATER_PIN = int(config.get('main','HEATER_PIN'))
AC_PIN = int(config.get('main','AC_PIN'))
FAN_PIN = int(config.get('main','FAN_PIN'))
sqliteEnabled = config.getboolean('sqlite','enabled')
if sqliteEnabled == True:
import sqlite3
#mail config
mailEnabled = config.getboolean('mail', 'enabled')
if mailEnabled == True:
import smtplib
config.read("mailconf.txt")
SMTP_SERVER = config.get('mailconf','SMTP_SERVER')
SMTP_PORT = int(config.get('mailconf','SMTP_PORT'))
username = config.get('mailconf','username')
password = config.get('mailconf','password')
sender = config.get('mailconf','sender')
recipient = config.get('mailconf','recipient')
subject = config.get('mailconf','subject')
body = config.get('mailconf','body')
errorThreshold = float(config.get('mail','errorThreshold'))
class rubustatDaemon(Daemon):
def configureGPIO(self):
GPIO.setmode(GPIO.BCM)
GPIO.setup(HEATER_PIN, GPIO.OUT)
GPIO.setup(AC_PIN, GPIO.OUT)
GPIO.setup(FAN_PIN, GPIO.OUT)
subprocess.Popen("echo " + str(HEATER_PIN) + " > /sys/class/gpio/export", shell=True)
subprocess.Popen("echo " + str(AC_PIN) + " > /sys/class/gpio/export", shell=True)
subprocess.Popen("echo " + str(FAN_PIN) + " > /sys/class/gpio/export", shell=True)
def getHVACState(self):
heatStatus = int(subprocess.Popen("cat /sys/class/gpio/gpio" + str(HEATER_PIN) + "/value", shell=True, stdout=subprocess.PIPE).stdout.read().strip())
coolStatus = int(subprocess.Popen("cat /sys/class/gpio/gpio" + str(AC_PIN) + "/value", shell=True, stdout=subprocess.PIPE).stdout.read().strip())
fanStatus = int(subprocess.Popen("cat /sys/class/gpio/gpio" + str(FAN_PIN) + "/value", shell=True, stdout=subprocess.PIPE).stdout.read().strip())
if heatStatus == 1 and fanStatus == 1:
#heating
return 1
elif coolStatus == 1 and fanStatus == 1:
#cooling
return -1
elif heatStatus == 0 and coolStatus == 0 and fanStatus == 0:
#idle
return 0
else:
#broken
return 2
def cool(self):
GPIO.output(HEATER_PIN, False)
GPIO.output(AC_PIN, True)
GPIO.output(FAN_PIN, True)
return -1
def heat(self):
GPIO.output(HEATER_PIN, True)
GPIO.output(AC_PIN, False)
GPIO.output(FAN_PIN, True)
return 1
def fan_to_idle(self):
#to blow the rest of the heated / cooled air out of the system
GPIO.output(HEATER_PIN, False)
GPIO.output(AC_PIN, False)
GPIO.output(FAN_PIN, True)
def idle(self):
GPIO.output(HEATER_PIN, False)
GPIO.output(AC_PIN, False)
GPIO.output(FAN_PIN, False)
#delay to preserve compressor
time.sleep(360)
return 0
if mailEnabled == True:
def sendErrorMail(self):
headers = ["From: " + sender,
"Subject: " + subject,
"To: " + recipient,
"MIME-Version: 1.0",
"Content-Type: text/html"]
headers = "\r\n".join(headers)
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.ehlo()
#you may need to comment this line out if you're a crazy person
#and use non-tls SMTP servers
session.starttls()
session.ehlo
session.login(username, password)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
session.quit()
def run(self):
lastLog = datetime.datetime.now()
lastMail = datetime.datetime.now()
self.configureGPIO()
while True:
#change cwd to wherever rubustat_daemon is
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
indoorTemp = float(getIndoorTemp())
hvacState = int(self.getHVACState())
file = open("status", "r")
targetTemp = float(file.readline())
mode = file.readline()
file.close()
now = datetime.datetime.now()
logElapsed = now - lastLog
mailElapsed = now - lastMail
### check if we need to send error mail
#cooling
#it's 78, we want it to be 72, and the error threshold is 5 = this triggers
if mailEnabled == True and (mailElapsed > datetime.timedelta(minutes=20)) and (indoorTemp - float(targetTemp) ) > errorThreshold:
self.sendErrorMail()
lastMail = datetime.datetime.now()
if DEBUG == 1:
log = open("logs/debug_" + datetime.datetime.now().strftime('%Y%m%d') + ".log", "a")
log.write("MAIL: Sent mail to " + recipient + " at " + time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) + "\n")
log.close()
#heat
#it's 72, we want it to be 78, and the error threshold is 5 = this triggers
if mailEnabled == True and (mailElapsed > datetime.timedelta(minutes=20)) and (float(targetTemp) - indoorTemp ) > errorThreshold:
self.sendErrorMail()
lastMail = datetime.datetime.now()
if DEBUG == 1:
log = open("logs/debug_" + datetime.datetime.now().strftime('%Y%m%d') + ".log", "a")
log.write("MAIL: Sent mail to " + recipient + " at " + time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) + "\n")
log.close()
#logging actual temp and indoor temp to sqlite database.
#you can do fun things with this data, like make charts!
if logElapsed > datetime.timedelta(minutes=6) and sqliteEnabled:
c.execute('INSERT INTO logging VALUES(?, ?, ?)', (now, indoorTemp, targetTemp))
conn.commit()
lastLog = datetime.datetime.now()
# heater mode
if mode == "heat":
if hvacState == 0: #idle
if indoorTemp < targetTemp - inactive_hysteresis:
if DEBUG == 1:
log = open("logs/debug_" + datetime.datetime.now().strftime('%Y%m%d') + ".log", "a")
log.write("STATE: Switching to heat at " + time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) + "\n")
log.close()
hvacState = self.heat()
elif hvacState == 1: #heating
if indoorTemp > targetTemp + active_hysteresis:
if DEBUG == 1:
log = open("logs/debug_" + datetime.datetime.now().strftime('%Y%m%d') + ".log", "a")
log.write("STATE: Switching to fan_to_idle at " + time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) + "\n")
log.close()
self.fan_to_idle()
time.sleep(30)
if DEBUG == 1:
log = open("logs/debug_" + datetime.datetime.now().strftime('%Y%m%d') + ".log", "a")
log.write("STATE: Switching to idle at " + time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) + "\n")
log.close()
hvacState = self.idle()
elif hvacState == -1: # it's cold out, why is the AC running?
if DEBUG == 1:
log = open("logs/debug_" + datetime.datetime.now().strftime('%Y%m%d') + ".log", "a")
log.write("STATE: Switching to idle at " + time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) + "\n")
log.close()
hvacState = self.idle()
# ac mode
elif mode == "cool":
if hvacState == 0: #idle
if indoorTemp > targetTemp + inactive_hysteresis:
if DEBUG == 1:
log = open("logs/debug_" + datetime.datetime.now().strftime('%Y%m%d') + ".log", "a")
log.write("STATE: Switching to cool at " + time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) + "\n")
log.close()
hvacState = self.cool()
elif hvacState == -1: #cooling
if indoorTemp < targetTemp - active_hysteresis:
if DEBUG == 1:
log = open("logs/debug_" + datetime.datetime.now().strftime('%Y%m%d') + ".log", "a")
log.write("STATE: Switching to fan_to_idle at " + time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) + "\n")
log.close()
self.fan_to_idle()
time.sleep(30)
if DEBUG == 1:
log = open("logs/debug_" + datetime.datetime.now().strftime('%Y%m%d') + ".log", "a")
log.write("STATE: Switching to idle at " + time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) + "\n")
log.close()
hvacState = self.idle()
elif hvacState == 1: # it's hot out, why is the heater on?
if DEBUG == 1:
log = open("logs/debug_" + datetime.datetime.now().strftime('%Y%m%d') + ".log", "a")
log.write("STATE: Switching to fan_to_idle at " + time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) + "\n")
log.close()
hvacState = self.idle()
else:
print "It broke."
#loggin'stuff
if DEBUG == 1:
heatStatus = int(subprocess.Popen("cat /sys/class/gpio/gpio" + str(HEATER_PIN) + "/value", shell=True, stdout=subprocess.PIPE).stdout.read().strip())
coolStatus = int(subprocess.Popen("cat /sys/class/gpio/gpio" + str(AC_PIN) + "/value", shell=True, stdout=subprocess.PIPE).stdout.read().strip())
fanStatus = int(subprocess.Popen("cat /sys/class/gpio/gpio" + str(FAN_PIN) + "/value", shell=True, stdout=subprocess.PIPE).stdout.read().strip())
log = open("logs/debug_" + datetime.datetime.now().strftime('%Y%m%d') + ".log", "a")
log.write("Report at " + time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) + ":\n")
log.write("hvacState = " + str(hvacState)+ "\n")
log.write("indoorTemp = " + str(indoorTemp)+ "\n")
log.write("targetTemp = " + str(targetTemp)+ "\n")
log.write("heatStatus = " + str(heatStatus) + "\n")
log.write("coolStatus = " + str(coolStatus)+ "\n")
log.write("fanStatus = " + str(fanStatus)+ "\n")
log.close()
time.sleep(5)
if __name__ == "__main__":
daemon = rubustatDaemon('rubustatDaemon.pid')
#Setting up logs
if not os.path.exists("logs"):
subprocess.Popen("mkdir logs", shell=True)
if sqliteEnabled == True:
conn = sqlite3.connect("temperatureLogs.db")
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS logging (datetime TIMESTAMP, actualTemp FLOAT, targetTemp INT)')
if len(sys.argv) == 2:
if 'start' == sys.argv[1]:
daemon.start()
elif 'stop' == sys.argv[1]:
#stop all HVAC activity when daemon stops
GPIO.setmode(GPIO.BCM)
GPIO.setup(HEATER_PIN, GPIO.OUT)
GPIO.setup(AC_PIN, GPIO.OUT)
GPIO.setup(FAN_PIN, GPIO.OUT)
GPIO.output(HEATER_PIN, False)
GPIO.output(AC_PIN, False)
GPIO.output(FAN_PIN, False)
daemon.stop()
elif 'restart' == sys.argv[1]:
daemon.restart()
else:
print "Unknown command"
sys.exit(2)
sys.exit(0)
else:
print "usage: %s start|stop|restart" % sys.argv[0]
sys.exit(2)