-
Notifications
You must be signed in to change notification settings - Fork 0
/
fan_ctrl.py
188 lines (159 loc) · 5.38 KB
/
fan_ctrl.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
import board
import pandas as pd
import numpy as np
import time
import argparse
from lib.relay import Pump as Fan
from lib.meas_lib_new import parse_args, DHTTemp, Decorators
# from lib.influx.influx_lib import InfluxClient
import adafruit_dht as dht_lib
import board
import RPi.GPIO as GPIO
from lib.cpu_lib import CoolingSystem, wait
class FanCooling(CoolingSystem):
def __init__(self, fan_channel=19, setpoint=35):
super().__init__(setpoint=setpoint)
self.fan = Fan(channel=fan_channel)
@wait
def make_measurement(self):
temp_arr = np.zeros(self.interval, dtype=float)
for i in range(self.interval):
temp_arr[i] = self.measure()
time.sleep(0.9)
return temp_arr[~np.isnan(temp_arr)].mean()
def run(self):
while True:
try:
temp = self.make_measurement()
if temp >= self.setpoint:
self.fan.turn_on()
while temp >= self.setpoint - self.hyster:
print('Current temperature = {:.1f}'.format(temp))
temp = self.make_measurement()
else:
self.fan.turn_off()
print('Current temperature = {:.1f}'.format(temp))
time.sleep(1)
self.clean()
except KeyboardInterrupt:
self.clean()
print('Interrupted by user.')
break
def parse_args():
"""Parse the args from main."""
parser = argparse.ArgumentParser(
description='set setoint temp and hum')
parser.add_argument('--temp', type=float, required=False,
default=25,
help='setpoint temperature')
parser.add_argument('--hum', type=float, required=False,
default=55,
help='setpoint humidity')
return parser.parse_args()
def print_res(res_dict):
print(f"temp = {res_dict.get('temp'):.2f} C | hum = {res_dict.get('hum'):.2f} %")
class DHTTempModified(DHTTemp):
def __init__(self, board_ch):
super().__init__(board_ch=board_ch)
@Decorators.wait
def make_measurement(self):
df = pd.DataFrame(
np.zeros((self.interval, 2), dtype=float),
columns=['temp', 'hum'], index=range(self.interval))
for i, (ind, _) in zip(range(self.interval), df.iterrows()):
df.loc[ind, :] = self.measure()
time.sleep(0.9)
return df.mean().round(2).to_dict()
def test_temp():
m = DHTTempModified(board_ch=board.D5)
#
while True:
try:
res = m.make_measurement()
print(res)
# m.write_to_db(res)
except KeyboardInterrupt:
print('Interrupted by user.')
break
def dht_test():
# GPIO.setmode(GPIO.BCM)
# GPIO.cleanup()
dht = dht_lib.DHT22(board.D20)
print(dht.temperature)
def main(temp_set, hum_set):
temp_hyster, hum_hyster = 5, 5
# init temp sensor & fan
m = DHTTempModified(board_ch=board.D20)
big_fan = Fan(channel=13) # big fan channel
# run ctrl
while True:
try:
res = m.make_measurement()
temp, hum = res.values()
print_res(res)
if temp >= temp_set or hum >= hum_set:
# start cooling
big_fan.turn_on()
while (temp >= temp_set - temp_hyster) and (hum >= hum_set - hum_hyster):
print_res(res)
res = m.make_measurement()
temp, hum = res.values()
else:
# stop cooling
big_fan.turn_off()
print_res(res)
time.sleep(1)
except KeyboardInterrupt:
print('Interrupted by user.')
big_fan.turn_off()
big_fan.gpio_clean()
break
def run_cpu_fan():
cpu_fan = FanCooling(fan_channel=19, setpoint=10)
cpu_fan.run()
def main_hum_fan(temp_set=25, hum_set=50):
temp_hyster, hum_hyster = 5, 5
# init temp sensor & fan
m = DHTTempModified(board_ch=board.D20)
big_fan = Fan(channel=13) # big fan channel
# run ctrl
while True:
try:
res = m.make_measurement()
temp, hum = res.values()
print_res(res)
if temp >= temp_set or hum >= hum_set:
# start cooling
big_fan.turn_on()
while (temp >= temp_set - temp_hyster) and (hum >= hum_set - hum_hyster):
print_res(res)
res = m.make_measurement()
temp, hum = res.values()
else:
# stop cooling
big_fan.turn_off()
print_res(res)
time.sleep(1)
except KeyboardInterrupt:
print('Interrupted by user.')
big_fan.turn_off()
big_fan.gpio_clean()
break
def stop_fan():
big_fan = Fan(channel=13) # big fan channel
big_fan.turn_off()
big_fan.gpio_clean()
if __name__ == '__main__':
# args = parse_args()
# main(temp_set=args.temp, hum_set=args.hum)
# run_cpu_fan()
# main_hum_fan(args.temp, args.hum)
# test_temp()
# dht_test()
# stop_fan()
# main()
# fan = Fan(channel=19)
# fan = Fan(channel=13) # big fan channel
# # fan.turn_on()
# fan.turn_off()
test_temp()