-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol_ventilation.py
189 lines (160 loc) · 6.66 KB
/
control_ventilation.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
import datetime
import json
from giessomat import Relais, DHT22
def read_json(path_json):
"""
Takes a path to json file and returns json data as dictionary.
Arguments:
path_json (str): Path to json file.
"""
with open(path_json) as s:
settings = json.load(s)
return(settings)
def vent_at_times(times, GPIO=24):
"""
Ventilation is executed at specific times.
Arguments:
times (array): Each time in times is a 2D Array
time (array): [start_hour (int), start_min (int)]
GPIO (int): GPIO pin used to switch Relais for ventilation control.
"""
ventilation = Relais.Relais(GPIO)
# Get current time
current_time = datetime.datetime.now().hour*60 + datetime.datetime.now().minute
# Compare time with current time
for time in times:
tmp = time[0]*60 + time[1]
time_delta = tmp - current_time
if time_delta <= 0 and time_delta > -10:
ventilation.on()
break
else:
ventilation.off()
def vent_TaRH(Ta, RH, Ta_max, RH_max, GPIO=24):
"""
Compares Ta with Ta_max and RH with RH_max. If statement is true/false
ventilation starts/stops.
Arguments:
Ta (float): Actual air temperature measured with DHT22.
RH (float): Actual humidity measured with DHT22.
Ta_max (int): Maximum air temperature accepted before ventialtion starts.
RH_max (int): Maximum humidity (in percent) accepted before ventilation starts.
GPIO (int): GPIO pin used to switch Relais for ventilation control.
"""
ventilation = Relais.Relais(GPIO)
if Ta >= Ta_max:
ventilation.on()
elif RH >= RH_max:
ventilation.on()
else:
ventilation.off()
def vent_Ta(Ta, Ta_max, GPIO=24):
"""
Compares Ta with Ta_max. If statement is true/false ventilation starts/stops.
Arguments:
Ta (float): Actual air temperature measured with DHT22.
Ta_max (int): Maximum air temperature accepted before ventialtion starts.
GPIO (int): GPIO pin used to switch Relais for ventilation control.
"""
ventilation = Relais.Relais(GPIO)
if Ta >= Ta_max:
ventilation.on()
else:
ventilation.off()
def vent_RH(RH, RH_max, GPIO=24):
"""
Compares RH with RH_max. If statement is true/false ventilation starts/stops.
Arguments:
Ta (float): Actual air temperature measured with DHT22.
RH (float): Actual humidity measured with DHT22.
Ta_max (int): Maximum air temperature accepted before ventialtion starts.
RH_max (int): Maximum humidity (in percent) accepted before ventilation starts.
GPIO (int): GPIO pin used to switch Relais for ventilation control.
"""
ventilation = Relais.Relais(GPIO)
if RH >= RH_max:
ventilation.on()
else:
ventilation.off()
def vent_off(GPIO=24):
"""
Stops ventilation.
Arguments:
GPIO (int): GPIO pin used to switch Relais for ventilation control.
"""
ventilation = Relais.Relais(GPIO)
ventilation.off()
if __name__ == '__main__':
# Open JSON seetings file
json_path = '/home/pi/Giess-o-mat-Webserver/ventilation_settings.json'
ventilation_settings = read_json(json_path)
if ventilation_settings['auto'] == True:
if ventilation_settings['mode_ventilation_stop'] == True:
print("No ventilation in timespan")
# Get start and end time
start_hour = int(ventilation_settings['start_time'][0:2])
start_minute = int(ventilation_settings['start_time'][3:5])
end_hour = int(ventilation_settings['end_time'][0:2])
end_minute = int(ventilation_settings['end_time'][3:5])
# Calculate start and end time in minutes
start_time = start_hour*60 + start_minute
end_time = end_hour*60 + end_minute
# Get current time
current_time = datetime.datetime.now().hour*60 + datetime.datetime.now().minute
# Check whether actual time is within start and end time
if current_time > start_time and current_time < end_time:
if ventilation_settings['mode'] == 'Lufttemperatursteuerung':
# Get Ta from senor
dht22 = DHT22.DHT22()
Ta = dht22.get_temperature()
# Get Ta threshold
Ta_max = ventilation_settings['Ta_max']
# Call function
vent_Ta(Ta, Ta_max)
elif ventilation_settings['mode'] == 'Luftfeuchtesteuerung':
# Get RH from senor
dht22 = DHT22.DHT22()
RH = dht22.get_humidity()
# Get RH threshold
RH_max = ventilation_settings['RH_max']
# Call function
vent_RH(RH, RH_max)
elif ventilation_settings['mode'] == 'Lufttemperatur- und Luftfeuchtesteuerung':
# Get sensor values
dht22 = DHT22.DHT22()
Ta = dht22.get_temperature()
RH = dht22.get_humidity()
# Get thresholds
Ta_max = ventilation_settings['Ta_max']
RH_max = ventilation_settings['RH_max']
# Call function
vent_TaRH(Ta, RH, Ta_max, RH_max)
else:
vent_off()
elif ventilation_settings['mode_ventilation_stop'] == False:
if ventilation_settings['mode'] == 'Lufttemperatursteuerung':
# Get Ta from senor
dht22 = DHT22.DHT22()
Ta = dht22.get_temperature()
# Get Ta threshold
Ta_max = ventilation_settings['Ta_max']
# Call function
vent_Ta(Ta, Ta_max)
elif ventilation_settings['mode'] == 'Luftfeuchtesteuerung':
# Get RH from senor
dht22 = DHT22.DHT22()
RH = dht22.get_humidity()
# Get RH threshold
RH_max = ventilation_settings['RH_max']
# Call function
vent_RH(RH, RH_max)
elif ventilation_settings['mode'] == 'Lufttemperatur- und Luftfeuchtesteuerung':
# Get sensor values
dht22 = DHT22.DHT22()
Ta = dht22.get_temperature()
RH = dht22.get_humidity()
# Get thresholds
Ta_max = ventilation_settings['Ta_max']
RH_max = ventilation_settings['RH_max']
# Call function
vent_TaRH(Ta, RH, Ta_max, RH_max)