-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp_controller_run.py
More file actions
423 lines (350 loc) · 11.8 KB
/
temp_controller_run.py
File metadata and controls
423 lines (350 loc) · 11.8 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/python3
#-*- coding: utf-8 -*-
import os
from concurrent.futures import ProcessPoolExecutor
from time import sleep
def read_write_room_temp():
room_temp = 78.0
while True:
# tempfile = open('/sys/bus/w1/devices/28-000009b55d17/w1_slave')
# thetext = tempfile.read()
# tempfile.close()
# tempdata = thetext.split("\n")[1].split(" ")[9]
# room_temp = ((float(tempdata[2:])/1000) *1.8) + 32
# room_temp = '{0:.1f}'.format((room_temp))
f = open('room_temp.txt', 'a', os.O_NONBLOCK)
f.seek(0)
f.truncate()
f.write(room_temp + '\n')
f.flush()
sleep(2)
def read_write_cond_temp():
cond_temp = 67.0
while True:
## tempfile = open('/sys/bus/w1/devices/28-000009b7715e/w1_slave')
## thetext = tempfile.read()
## tempfile.close()
## tempdata = thetext.split("\n")[1].split(" ")[9]
## cond_temp = ((float(tempdata[2:])/1000) *1.8) + 32
## cond_temp = '{0:.1f}'.format((cond_temp))
cond_temp = cond_temp - 1.0
f = open('cond_temp.txt', 'a', os.O_NONBLOCK)
f.seek(0)
f.truncate()
f.write(str(cond_temp) + '\n')
f.flush()
sleep(2)
process_pool = ProcessPoolExecutor(2)
process_1 = process_pool.submit(read_write_room_temp)
process_2 = process_pool.submit(read_write_cond_temp)
import eventlet
eventlet.monkey_patch()
from concurrent.futures import ThreadPoolExecutor
from flask import Flask, render_template, url_for, redirect, session
from flask_socketio import SocketIO, emit, disconnect
from threading import Lock, Event, Timer, Thread
from datetime import datetime
import atexit
import RPi.GPIO as GPIO
COMPRESSOR_PIN = 11
FAN_PIN = 13
HEAT_COIL_PIN = 15
HEATER_1_PIN = 29
HEATER_2_PIN = 31
GPIO.setmode(GPIO.BOARD)
GPIO.setup(COMPRESSOR_PIN, GPIO.OUT)
GPIO.setup(FAN_PIN, GPIO.OUT)
GPIO.setup(HEAT_COIL_PIN, GPIO.OUT)
GPIO.setup(HEATER_1_PIN, GPIO.OUT)
GPIO.setup(HEATER_2_PIN, GPIO.OUT)
async_mode = None
app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode=async_mode)
stop_event = Event()
pool = ThreadPoolExecutor(5)
thread_1 = None
thread_2 = None
future_1 = None
future_2 = None
future_3 = None
future_4 = None
future_5 = None
condenser_heating_cycle_flag = False
compressor_cut_off_temp = 35.0
compressor_cut_in_temp = 38.0
temp_differential = 1.5
compressor_state = 0
fan_state = 0
heating_coil_state = 0
heater_1_state = 0
heater_2_state = 0
stop_event.set()
set_temperature = 75
timer_run_flag = False
room_temperature = 75
condenser_temperature = 75
def cleanup_routine():
print('cleanup started')
global compressor_state
global fan_state
global heating_coil_state
global heater_1_state
global heater_2_state
GPIO.output(COMPRESSOR_PIN, False)
GPIO.output(FAN_PIN, False)
GPIO.output(HEAT_COIL_PIN, False)
GPIO.output(HEATER_1_PIN, False)
GPIO.output(HEATER_1_PIN, False)
compressor_state = 0
fan_state = 0
heating_coil_state = 0
heater_1_state = 0
heater_2_state = 0
GPIO.cleanup()
print('cleanup finished')
def kill_all_outputs():
global compressor_state
global fan_state
global heating_coil_state
global heater_1_state
global heater_2_state
GPIO.output(COMPRESSOR_PIN, False)
GPIO.output(FAN_PIN, False)
GPIO.output(HEAT_COIL_PIN, False)
GPIO.output(HEATER_1_PIN, False)
GPIO.output(HEATER_1_PIN, False)
compressor_state = 0
fan_state = 0
heating_coil_state = 0
heater_1_state = 0
heater_2_state = 0
def output_control():
global condenser_temperature
if float(set_temperature) < 75.0:
cooling_control()
elif float(set_temperature) >= 75.0:
heating_control()
def cooling_control():
global compressor_state
global fan_state
global heating_coil_state
global heater_1_state
global heater_2_state
global condenser_temperature
GPIO.output(HEATER_1_PIN, False)
GPIO.output(HEATER_2_PIN, False)
heater_1_state = 0
heater_2_state = 0
if float(condenser_temperature) <= compressor_cut_off_temp:
while float(condenser_temperature) <= compressor_cut_in_temp:
GPIO.output(COMPRESSOR_PIN, False)
GPIO.output(FAN_PIN, False)
GPIO.output(HEAT_COIL_PIN, True)
compressor_state = 0
fan_state = 0
heating_coil_state = 1
socketio.emit('timeChange', {'data': 'Condenser Heating Cycle'}, broadcast=True, namespace='/test')
eventlet.sleep(1)
if float(room_temperature) <= (float(set_temperature) - temp_differential):
GPIO.output(COMPRESSOR_PIN, False)
GPIO.output(FAN_PIN, True)
GPIO.output(HEAT_COIL_PIN, False)
compressor_state = 0
fan_state = 1
heating_coil_state = 0
elif float(room_temperature) >= (float(set_temperature) + temp_differential):
GPIO.output(COMPRESSOR_PIN, True)
GPIO.output(FAN_PIN, True)
GPIO.output(HEAT_COIL_PIN, False)
compressor_state = 1
fan_state = 1
heating_coil_state = 0
def heating_control():
global compressor_state
global fan_state
global heating_coil_state
global heater_1_state
global heater_2_state
GPIO.output(COMPRESSOR_PIN, False)
GPIO.output(FAN_PIN, False)
GPIO.output(HEAT_COIL_PIN, False)
compressor_state = 0
fan_state = 0
heating_coil_state = 0
if float(room_temperature) >= (float(set_temperature) + temp_differential):
GPIO.output(HEATER_1_PIN, False)
GPIO.output(HEATER_2_PIN, False)
heater_1_state = 0
heater_2_state = 0
elif float(room_temperature) <= (float(set_temperature) - temp_differential):
GPIO.output(HEATER_1_PIN, True)
GPIO.output(HEATER_2_PIN, True)
heater_1_state = 1
heater_2_state = 1
def outputs_task():
on = 'On'
off = 'Off'
while True:
if compressor_state == 0:
socketio.emit('compressorStateIO', {'comp': off}, namespace='/test', broadcast=True)
else:
socketio.emit('compressorStateIO', {'comp': on}, namespace='/test', broadcast=True)
if fan_state == 0:
socketio.emit('fanStateIO', {'fan': 'Off'}, namespace='/test', broadcast=True)
else:
socketio.emit('fanStateIO', {'fan': 'On'}, namespace='/test', broadcast=True)
if heating_coil_state == 0:
socketio.emit('heatCoilStateIO', {'heatCoil': 'Off'}, namespace='/test', broadcast=True)
else:
socketio.emit('heatCoilStateIO', {'heatCoil': 'On'}, namespace='/test', broadcast=True)
if heater_1_state == 0:
socketio.emit('heater1StateIO', {'heater1': 'Off'}, namespace='/test', broadcast=True)
else:
socketio.emit('heater1StateIO', {'heater1': 'On'}, namespace='/test', broadcast=True)
if heater_2_state == 0:
socketio.emit('heater2StateIO', {'heater2': 'Off'}, namespace='/test', broadcast=True)
else:
socketio.emit('heater2StateIO', {'heater2': 'On'}, namespace='/test', broadcast=True)
eventlet.sleep(1)
def temp_1_task():
global room_temperature
while True:
tempfile = open('room_temp.txt', 'r')
room_temperature = tempfile.read()
tempfile.close()
eventlet.sleep(2)
def temp_2_task():
global condenser_temperature
while True:
tempfile = open('cond_temp.txt', 'r')
condenser_temperature = tempfile.read()
tempfile.close()
eventlet.sleep(2)
@app.route('/')
def tempURL():
return redirect(url_for('temp'))
@app.route('/temperature')
def temp():
return render_template('temperature.html')
@app.route('/IO_monitor')
def condTemp():
return render_template('IOmonitor.html')
def update():
return redirect(url_for('temp'))
def continuous_timer_task():
global timer_run_flag
timer_run_flag = True
while (not stop_event.is_set()):
eventlet.sleep(1)
output_control()
socketio.emit('timeChange', {'data': 'Running in continuous mode...'}, broadcast=True, namespace='/test')
kill_all_outputs()
stop_event.clear()
timer_run_flag = False
socketio.emit('timeChange', {'data': ""}, broadcast=True, namespace='/test')
def timer_task(setup_time):
setup_time = setup_time * 60
global timer_run_flag
timer_run_flag = True
while (not stop_event.is_set() and setup_time >= 0):
output_control()
hours = int(setup_time/3600)
minutes = int(setup_time/60) % 60
seconds = int(setup_time) % 60
if minutes < 10:
minutes = '0' + str(minutes)
if seconds < 10:
seconds = '0' + str(seconds)
hours_minutes_seconds = str(hours) + ':' + str(minutes) + ':' + str(seconds)
socketio.emit('timeChange', {'data': hours_minutes_seconds}, broadcast=True, namespace='/test')
setup_time = setup_time - 1
eventlet.sleep(1)
kill_all_outputs()
stop_event.clear()
timer_run_flag = False
socketio.emit('timeChange', {'data': ""}, broadcast=True, namespace='/test')
def temp_out_task():
global room_temperature
global condenser_temperature
while True:
eventlet.sleep(2)
socketio.emit('currentTemp', {'temp1': room_temperature}, namespace='/test', broadcast=True)
socketio.emit('currentTempIO', {'temp1': room_temperature}, namespace='/test', broadcast=True)
socketio.emit('condenserTempIO', {'temp2': condenser_temperature}, namespace='/test', broadcast=True)
@socketio.on('connect_event', namespace='/test')
def connect_start():
global timer_run_flag
timer_run_flag = False
global future_1
global future_2
global future_3
global future_5
socketio.emit('tempChange', {'data': set_temperature}, broadcast=True, namespace='/test')
pool = ThreadPoolExecutor(5)
future_1 = pool.submit(temp_1_task)
future_2 = pool.submit(temp_2_task)
future_3 = pool.submit(temp_out_task)
future_5 = pool.submit(outputs_task)
@socketio.on('inc_temp_event', namespace='/test')
def increment_temp(message):
temp = str(message['data'])
temp = temp.strip('F')
temp = temp.strip(u'\u00B0')
temp = int(temp)
if temp >= 150:
temp = 150
else:
temp = temp + 5
global set_temperature
set_temperature = temp
socketio.emit('tempChange', {'data': temp}, broadcast=True, namespace='/test')
@socketio.on('dec_temp_event', namespace='/test')
def decrement_temp(message):
temp = str(message['data'])
temp = temp.strip('F')
temp = temp.strip(u'\u00B0')
temp =int(temp)
if temp <= 35:
temp = 35
else:
temp = temp - 5
global set_temperature
set_temperature = temp
socketio.emit('tempChange', {'data': temp}, broadcast=True, namespace='/test')
@socketio.on('start_event', namespace='/test')
def start_timer(message):
global future_4
global timer_run_flag
if timer_run_flag is False:
if future_4 != None:
future_4.cancel()
future_4 = None
stop_event.clear()
hours = (message['data1'])
minutes = (message['data2'])
if hours == '':
hours = 0
if minutes == '':
minutes = 0
setup_time = (int(hours) * 60) + int(minutes)
if setup_time == 0:
future_4 = pool.submit(continuous_timer_task)
else:
future_4 = pool.submit(timer_task, (setup_time))
else:
pass
@socketio.on('stop_event', namespace='/test')
def stop_timer():
kill_all_outputs()
global timer_run_flag
timer_run_flag = False
global future_4
if future_4 != None:
future_4.cancel()
future_4 = None
stop_event.set()
atexit.register(cleanup_routine)
if __name__ == '__main__':
socketio.run(app, host='0.0.0.0')