-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp_controller_run_gpio.py
242 lines (180 loc) · 7 KB
/
temp_controller_run_gpio.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
#-*- coding: utf-8 -*-
from concurrent.futures import ProcessPoolExecutor
from concurrent.futures import ThreadPoolExecutor
from time import sleep
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)
COMPRESSOR_CUT_OFF_TEMP = 31.0
COMPRESSOR_CUT_IN_TEMP = 33.0
HEAT_TEMP_LOW = 0.5
HEAT_TEMP_HIGH = 1.5
COOL_TEMP_LOW = 1.5
COOL_TEMP_HIGH = 0.5
process_pool = ProcessPoolExecutor(1)
thread_pool = ThreadPoolExecutor(2)
def init_outputs():
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_2_PIN, False)
init_outputs()
def write_gpio_status():
try:
compressor_state = GPIO.input(COMPRESSOR_PIN)
fan_state = GPIO.input(FAN_PIN)
heat_coil_state = GPIO.input(HEAT_COIL_PIN)
heater_1_state = GPIO.input(HEATER_1_PIN)
heater_2_state = GPIO.input(HEATER_2_PIN)
f = open('outputs.txt', 'a')
f.seek(0)
f.truncate()
f.write(str(compressor_state) + '\n')
f.write(str(fan_state) + '\n')
f.write(str(heat_coil_state) + '\n')
f.write(str(heater_1_state) + '\n')
f.write(str(heater_2_state) + '\n')
f.flush()
f.close()
except:
print('Error in write_gpio_status()')
write_gpio_status()
def read_write_temps():
while True:
try:
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')
f.seek(0)
f.truncate()
f.write(str(room_temp) + '\n')
f.flush()
f.close()
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))
f = open('cond_temp.txt', 'a')
f.seek(0)
f.truncate()
f.write(str(cond_temp) + '\n')
f.flush()
f.close()
except:
print('Error in read_write_temps()')
sleep(2)
def kill_all_outputs():
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_2_PIN, False)
def gpio_cooling_control(set_temperature, room_temperature, condenser_temperature):
GPIO.output(HEATER_1_PIN, False)
GPIO.output(HEATER_2_PIN, False)
if float(condenser_temperature) <= COMPRESSOR_CUT_OFF_TEMP:
while float(condenser_temperature) <= COMPRESSOR_CUT_IN_TEMP:
try:
tempfile = open('cond_temp.txt', 'r')
condenser_temperature = tempfile.read()
tempfile.close()
GPIO.output(COMPRESSOR_PIN, False)
GPIO.output(FAN_PIN, False)
GPIO.output(HEAT_COIL_PIN, True)
except:
print('Error in condenser heater loop()')
sleep(1)
if float(room_temperature) <= (float(set_temperature) - COOL_TEMP_LOW):
GPIO.output(COMPRESSOR_PIN, False)
GPIO.output(FAN_PIN, True)
GPIO.output(HEAT_COIL_PIN, False)
elif float(room_temperature) >= (float(set_temperature) + COOL_TEMP_HIGH):
GPIO.output(COMPRESSOR_PIN, True)
GPIO.output(FAN_PIN, True)
GPIO.output(HEAT_COIL_PIN, False)
def gpio_heating_control(set_temperature, room_temperature, condenser_temperature):
GPIO.output(COMPRESSOR_PIN, False)
GPIO.output(FAN_PIN, False)
GPIO.output(HEAT_COIL_PIN, False)
if float(room_temperature) >= (float(set_temperature) + HEAT_TEMP_HIGH):
GPIO.output(HEATER_1_PIN, False)
GPIO.output(HEATER_2_PIN, False)
elif float(room_temperature) <= (float(set_temperature) - HEAT_TEMP_LOW):
GPIO.output(HEATER_1_PIN, True)
GPIO.output(HEATER_2_PIN, True)
def gpio_output_control():
while True:
try:
tempfile = open('run_flag.txt', 'r')
run_flag = tempfile.read()
tempfile.close()
if int(run_flag) == 1:
tempfile = open('set_temp.txt', 'r')
set_temperature = tempfile.read()
tempfile.close()
tempfile = open('room_temp.txt', 'r')
room_temperature = tempfile.read()
tempfile.close()
tempfile = open('cond_temp.txt', 'r')
condenser_temperature = tempfile.read()
tempfile.close()
if float(set_temperature) < 75.0:
gpio_cooling_control(set_temperature, room_temperature, condenser_temperature)
elif float(set_temperature) >= 75.0:
gpio_heating_control(set_temperature, room_temperature, condenser_temperature)
else:
kill_all_outputs()
except:
print('Error in gpio_output_control()')
sleep(2)
def gpio_status():
while True:
try:
write_gpio_status()
tempfile = open('set_temp.txt', 'r')
set_temperature = tempfile.read()
tempfile.close()
tempfile = open('room_temp.txt', 'r')
room_temperature = tempfile.read()
tempfile.close()
tempfile = open('cond_temp.txt', 'r')
condenser_temperature = tempfile.read()
tempfile.close()
compressor_state = GPIO.input(COMPRESSOR_PIN)
fan_state = GPIO.input(FAN_PIN)
heat_coil_state = GPIO.input(HEAT_COIL_PIN)
heater_1_state = GPIO.input(HEATER_1_PIN)
heater_2_state = GPIO.input(HEATER_2_PIN)
print('Set temperature: ' + str(set_temperature))
print('Room temperature: ' + str(room_temperature))
#print('Condenser temperature: ' + str(condenser_temperature))
print('Compressor state: ' + str(compressor_state))
print('Fan state: ' + str(fan_state))
print('Heat coil state: ' + str(heat_coil_state))
print('Heater 1 state: ' + str(heater_1_state))
print('Heater 2 state: ' + str(heater_2_state))
except:
print('Error in gpio_status()')
sleep(1)
if __name__ == '__main__':
process_1 = process_pool.submit(read_write_temps)
thread_1 = thread_pool.submit(gpio_output_control)
thread_2 = thread_pool.submit(gpio_status)