-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
226 lines (169 loc) · 6.04 KB
/
main.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
# to redirect output to the console
import streams
streams.serial()
#########################################
# libraries to import #
#########################################
import adc
import json
from mqtt import mqtt
from meas.htu21d import htu21d
import sources.wifi_connection as wifi
import sources.led as myLed
import sources.fan as myFan
import sources.sunshield as mySunshield
#########################################
# variables #
#########################################
# if the temperature exceeds this value the cooling fan will be turned on
MAX_TEMP = 37.00
# when the fan is on and temperature reaches this value, the fan turns off
MIN_TEMP = 32.00
# time (in seconds) the device can remain uncovered with a high value of brightness
UNCOVER_TIME = 10
# time (in seconds) the device has to remain covered
COVER_TIME = 8
# time (in seconds) between each iteration of the main loop
SLEEP_TIME = 2
BROKER = "test.mosquitto.org"
client = mqtt.Client("iot-marco-esp32", True)
# temperature and humidity sensor
htu = htu21d.HTU21D(I2C0)
# cooling fan controlled by a relay on pin D23
fan = myFan.Fan(D23, client)
# cardboard panel attached to a servo motor controlled by pin D15
sunshield = mySunshield.Sunshield(D15.PWM, client)
# led to activate if the room is too dark
led = myLed.Led(D16, client)
#########################################
# mqtt communication functions #
#########################################
def confirm_subscribe(data):
print("succesfully subscribed")
# creates a message containing all the control variables and shares them via mqtt
def send_variables():
data = {
"max temp": MAX_TEMP,
"min temp": MIN_TEMP,
"cover time": COVER_TIME,
"uncover time": UNCOVER_TIME
}
# the message is a JSON representation of the dictionary
message = json.dumps(data)
client.publish("iot-marco/data/variables", str(message))
# sends a message containing the 3 parameters read by the sensors via mqtt
def send_sensor_values(t, h, b):
data = {
"temperature": t,
"humidity": h,
"brightness": b
}
message = json.dumps(data)
client.publish("iot-marco/data/sensors", str(message))
# sends a message containing the state of the devices via mqtt
def send_devices_data():
data = {
"fan": fan.state(),
"sunshield": sunshield.state(),
"led": led.state()
}
message = json.dumps(data)
client.publish("iot-marco/data/devices", str(message))
# takes the new values from the received data and updates the global variables
def set_variables(data):
global MAX_TEMP, MIN_TEMP, COVER_TIME, UNCOVER_TIME
values = json.loads(data)
MAX_TEMP = float(values["max temp"])
MIN_TEMP = float(values["min temp"])
COVER_TIME = int(values["cover time"])
UNCOVER_TIME = int(values["uncover time"])
# sends the values back to the user
send_variables()
# called whenever a message is received
def read_data(client, data):
message = data['message']
topic = str(message.topic)
payload = str(message.payload)
if topic == "iot-marco/commands/set-variables":
set_variables(payload)
elif topic == "iot-marco/commands/get-variables":
send_variables()
elif topic == "iot-marco/commands/get-devices":
send_devices_data()
elif topic == "iot-marco/commands/fan":
fan.control(payload)
elif topic == "iot-marco/commands/sunshield":
sunshield.control(payload)
elif topic == "iot-marco/commands/led":
led.control(payload)
#########################################
# temperature control functions #
#########################################
def monitor_temperature(value):
if value > MAX_TEMP:
fan.start()
elif value < MIN_TEMP and fan.is_running():
fan.stop()
#########################################
# brightness control functions #
#########################################
# the value read from the photoresistor is an integer within the range [0, 4095]
# the higher the value, the brighter the room is.
# I use this function to normalize said value in the range [0, 100]
def normalize(value):
return int(value / 4095 * 100)
def is_bright(value):
return value > 70
def is_dark(value):
return value < 40
time_passed = 0
def monitor_brightness(value):
global time_passed
# turns on the control led when the lighting is insufficient
if is_dark(value):
led.on()
else:
led.off()
# if the room is too bright it starts counting the passing of time.
# When time_passed reaches the UNCOVER_TIME threshold the sunshield
# will cover the device for a period of time equal to COVER_TIME
if is_bright(value) and not sunshield.is_covering():
time_passed += SLEEP_TIME
if time_passed >= UNCOVER_TIME:
time_passed = 0
sunshield.cover()
elif sunshield.is_covering():
time_passed += SLEEP_TIME
if time_passed >= COVER_TIME:
time_passed = 0
sunshield.uncover()
else:
time_passed = 0
#########################################
# setup #
#########################################
pinMode(A1, INPUT_ANALOG)
htu.start()
htu.init()
wifi.connect()
client.connect(BROKER, keepalive=0)
print("connected to broker:", BROKER)
# defines the callback function for when it receives subscription's ACK
client.on(mqtt.SUBACK, confirm_subscribe)
client.subscribe([["iot-marco/commands/#", 0]])
# starts a new thread to read incoming messages with the given function
client.loop(read_data)
#########################################
# program starts here #
#########################################
while True:
# reads the values
temperature = htu.get_temp()
humidity = htu.get_humid()
brightness = normalize(analogRead(A1))
# monitors them
monitor_temperature(temperature)
monitor_brightness(brightness)
# sends them
send_sensor_values(temperature, humidity, brightness)
sleep(SLEEP_TIME * 1000)