-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
339 lines (266 loc) · 24.7 KB
/
main.py
File metadata and controls
339 lines (266 loc) · 24.7 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
from machine import Pin, PWM
import network
import rp2
import socket
import time
import uasyncio as asyncio
# This is the IP assigned to your router by your ISP
OUTSIDE_IP = "<<YOUR_IP_HERE>>"
# This is the list of local IP addresses assigned to your Pico's by the router
# The port numbers (e.g. 29420) are nearly arbitrary and should match the NAT port-forwarded
# configuration in the router. Ex: port 29420 should forward to 192.168.50.18 on port 80
PHYSICAL_LOCATIONS = {
'<<PICO 1 IP HERE>>': ("<<PICO 1 LOCATION>>",29420),
'<<PICO 2 IP HERE>>': ("<<PICO 2 LOCATION>>",29421),
}
# Your Wifi name and password. Wifi must be on 2.4 GHz (the only one Pico W supports)
WIFI_SSID='<<YOUR WIFI SSID>>'
WIFI_PASSWORD='<<YOUR WIFI PASSWORD>>'
WIFI_COUNTRY='<<YOUR WIFI COUNTRY HERE, e.g. US>>'
# Visible Menu - a list of A/C commands you want to see (see options below in class ACControl
VISIBLE_MENU = ["OFF","HEAT_66","HEAT_72","COOL_70","COOL_74"]
class ACControl:
COMMANDS = {
'OFF': [4,1,12,2,4,1],
'HEAT_60': [9,6,2,2,1,2,2],
'HEAT_62': [9,5,3,2,1,2,1,1],
'HEAT_64': [9,5,1,2,2,1,1,3],
'HEAT_66': [9,4,4,2,1,1,2,1],
'HEAT_68': [9,4,2,2,2,1,1,1,2],
'HEAT_70': [9,4,1,3,2,1,1,1,1,1],
'HEAT_72': [9,4,1,1,2,2,5],
'HEAT_74': [9,3,5,2,4,1],
'HEAT_76': [9,3,3,3,3,2,1,1,1,1],
'HEAT_78': [9,3,2,1,2,2,2,3],
'HEAT_80': [9,3,1,4,2,2,2,1],
'HEAT_82': [9,3,1,2,2,2,1,2],
'HEAT_84': [9,3,1,1,3,2,2,1,1,1],
'HEAT_86': [9,3,1,1,1,2,2,1,4],
'COOL_64': [14,1,2,4,1,1,1],
'COOL_66': [13,4,3,4],
'COOL_68': [13,2,2,3,3,1],
'COOL_70': [13,1,3,3,2,2],
'COOL_72': [13,1,1,2,3,2,1,1],
'COOL_74': [12,5,3,1,3],
'COOL_76': [12,3,2,3,1,2,1],
'COOL_78': [12,2,1,2,3,1,1,1,1],
'COOL_80': [12,1,4,7],
'COOL_82': [12,1,2,2,6,1],
'COOL_84': [12,1,1,3,5,2],
'COOL_86': [12,1,1,1,2,5,1,1],
}
def __init__(self, pin=3):
self.pwm = PWM(Pin(pin))
self.pwm.freq(37000)
print("freq = ",self.pwm.freq())
def __del__(self):
self.stop()
def stop(self):
self.pwm.deinit()
def pulseIR(self, time_us):
self.pwm.duty_u16(32767)
time.sleep_us(time_us)
def spacer(self, time_us):
self.pwm.duty_u16(0)
time.sleep_us(time_us)
def LGCode(self, num):
ontime = 450
offtime = 750
longofftime = 1500
for i in range(num-1):
self.pulseIR(ontime)
self.spacer(offtime)
self.pulseIR(ontime); self.spacer(longofftime);
def LGHeader(self):
self.pulseIR(9300)
self.spacer(4500)
self.LGCode(1)
self.LGCode(4)
def run_command_pulses(self, v):
self.LGHeader()
for n in v:
self.LGCode(n)
def run_command(self, s):
if s in self.COMMANDS:
self.run_command_pulses(self.COMMANDS[s])
return True
return False
class LEDBlinker:
def __init__(self, pin="LED"):
self.led = Pin(pin, Pin.OUT)
async def blink(self, n, time_ms=500):
for i in range(n):
self.led.on()
await asyncio.sleep_ms(time_ms)
self.led.off()
if i < n - 1:
await asyncio.sleep_ms(time_ms)
class WifiConnection:
def __init__(self, blinker, ssid=WIFI_SSID, password=WIFI_PASSWORD):
rp2.country(WIFI_COUNTRY) # allow all US WiFi channels
self.wlan = network.WLAN(network.STA_IF)
self.wlan.active(True)
self.wlan.config(pm = 0xa11140) # Disable power-save mode
self.ssid, self.password = ssid, password
self.blinker = blinker
async def __aenter__(self):
await self.connectToWifi()
return self
async def __aexit__(self, exc_type, exc, tb):
pass
async def connectToWifi(self):
self.wlan.connect(self.ssid, self.password)
attempts_left = 10
while attempts_left and self.wlan.status() == network.STAT_CONNECTING:
print("connecting...")
await self.blinker.blink(1)
attempts_left -= 1
status = self.wlan.status()
print(f"status = {status}")
if status != network.STAT_GOT_IP:
reasoning = {
network.STAT_CONNECTING: 'timed out without error. try resetting power',
network.STAT_WRONG_PASSWORD: 'wrong password',
network.STAT_NO_AP_FOUND: 'could not discover access point',
}
raise RuntimeError(f"wifi connection failed - {reasoning.get(status,'unknown')}")
print("connected")
# Based on the IP given by the router, determine which Pico we are - and, by extension, which room we're in
config = self.wlan.ifconfig()
self.ip = config[0]
self.physical_location = PHYSICAL_LOCATIONS.get(self.ip, (self.ip,29420))[0]
outside_port = PHYSICAL_LOCATIONS.get(self.ip, (self.ip,29420))[1]
print("Physical Location: ",self.physical_location)
print(f"IP/Port: http://{OUTSIDE_IP}:{outside_port}/")
class ConnectionHandler:
PATH = '/lgcontrol'
COMMANDS = VISIBLE_MENU
AC_FAVICON = b"\x00\x00\x01\x00\x02\x00\x1f\x1b\x00\x00\x01\x00 \x00\xa8\r\x00\x00&\x00\x00\x00 \x02\x00\x01\x00\x01\x000\x01\x00\x00\xce\r\x00\x00(\x00\x00\x00\x1f\x00\x00\x006\x00\x00\x00\x01\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xba\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xf9\x00\x00\x00\x0e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x8b\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x001\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00j\x00\x00\x00\xb5\x00\x00\x00\xe5\x00\x00\x00\xf8\x00\x00\x00\xf3\x00\x00\x00\xdd\x00\x00\x00\xb9\x00\x00\x00t\x00\x00\x00\x1b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00^\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00W\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xda\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xd5\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00G\x00\x00\x00\xe9\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00h\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\xf5\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xa7\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00)\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00y\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00L\x00\x00\x00\xfd\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xe5\x00\x00\x00\\\x00\x00\x00\x12\x00\x00\x00\t\x00\x00\x00$\x00\x00\x00K\x00\x00\x00\x9d\x00\x00\x00^\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa5\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xfe\x00\x00\x00\x1e\x00\x00\x00\x00\x00\x00\x00\x11\x00\x00\x00\xef\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xee\x00\x00\x00!\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00I\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xc1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00z\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00u\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\xe8\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xe6\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00l\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc4\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\x1f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x91\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x009\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xbd\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xf8\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xeb\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xf3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x004\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\x8a\x00\x00\x00\x00\x00\x00\x00\x12\x00\x00\x00\xfb\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xad\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xfb\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xe7\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xd7\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xdc\x00\x00\x00\x00\x00\x00\x00_\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00Q\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xeb\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xf4\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00|\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00-\x00\x00\x00\xaf\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xee\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc4\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\x1f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\xfe\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\x88\x00\x00\x00\xf6\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\x99\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00z\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00u\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc3\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xf8\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00=\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11\x00\x00\x00\xef\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xef\x00\x00\x00!\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00g\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xe0\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00K\x00\x00\x00\xfd\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xe5\x00\x00\x00]\x00\x00\x00\x14\x00\x00\x00\n\x00\x00\x00'\x00\x00\x00O\x00\x00\x00\x9f\x00\x00\x00_\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11\x00\x00\x00\xf9\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\x85\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00F\x00\x00\x00\xe8\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00h\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xaf\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00)\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00j\x00\x00\x00\xb4\x00\x00\x00\xe4\x00\x00\x00\xf7\x00\x00\x00\xf2\x00\x00\x00\xdc\x00\x00\x00\xb8\x00\x00\x00s\x00\x00\x00\x1a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xfe\xff\xff\xff\xfe\xff\xff\xff\xfe\xff\xff\xff\xfe\xff\xff\xff\xfe\xff\xff\xff\xfe\xff\xff\xff\xfe\xff\xff\xff\xfe\x07\xe0\xe0\x06\x07\xe1\xc0\x06\x07\xc1\x80\x06\x80\x01\x03\xfe\x80\x03\x07\xfe\x81\x83\x07\xfe\xc1\x83\x0f\xfe\xc1\x07\x0f\xfe\xe1\x07\x0f\xfe\xe0\x07\x07\xfe\xe0\x0f\x07\xfe\xf0\x0f\x03\xfe\xf0\x0f\x80\x06\xf0\x1f\xc0\x06\xf8\x1f\xe0\x06\xff\xff\xff\xfe\xff\xff\xff\xfe\xff\xff\xff\xfe\xff\xff\xff\xfe(\x00\x00\x00 \x00\x00\x00@\x00\x00\x00\x01\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
def __init__(self, ac, blinker, physical_location):
self.ac = ac
self.blinker = blinker
self.physical_location = physical_location
def parse_params(self, r):
EOH = '\r\n\r\n' # End-of-header
res = {'GET': {}, 'POST': {}}
def parse_args(d,p):
for kv in params.split('&'):
if '=' in kv:
k,v = kv.split('=')
d[k] = v
# Parse POST, if it exists
if r[:4]=="POST":
params = r.split(EOH)[-1]
parse_args(res['POST'],params)
# Parse GET
try:
params = r.split('\n')[0].split(' ')[1].split('?')[-1]
parse_args(res['GET'],params)
except:
pass
return res
def request_contains(self, r, s):
rsp = r.split(' ')
if len(rsp) < 2:
return False
print(f"Looking for {s} in {rsp[1]}")
return rsp[1].find(s) == 0
def used_path(self, r):
return self.request_contains(r, self.PATH)
def wants_favicon(self, r):
return self.request_contains(r, '/favicon.ico')
def send_200_header(self, writer):
writer.write('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
def send_favicon(self, writer):
writer.write(self.AC_FAVICON)
def redirectToGET(self, writer, msg):
writer.write(f'HTTP/1.1 302 OK\r\nLocation: {self.PATH}?msg={ msg }\r\n\r\n')
async def run_ac_command(self, ac_command):
print("Running A/C Command: ", ac_command)
ac_command = ac_command.upper()
msg = ""
for _ in range(3):
if (self.ac.run_command(ac_command)):
msg = f"Last_Command:_{self.physical_location.replace(' ','_')}_{ac_command}"
await self.blinker.blink(1)
await asyncio.sleep_ms(1000)
return msg
async def serve_client(self, reader, writer):
try:
print("Client connected")
request = await reader.read(16384)
request = request.decode()
print("Request:", request)
# if the request doesn't use our path, we silently fail, by simply sending back HTTP code 200
# unless the request is for the favicon, which we serve up
if not self.used_path(request):
print("Didn't use the correct path to get to the menu!")
self.send_200_header(writer)
if self.wants_favicon(request):
print("Sending FAVICON")
self.send_favicon(writer)
return
# Parse the parameters
params = self.parse_params(request)
print('parsed params:',params)
# User is trying to run a command
if 'action' in params['POST']:
action = params['POST']['action']
msg = await self.run_ac_command(action)
self.redirectToGET(writer, msg)
return
# Standard GET request - just show the page
command_a_elements = [f"<a href=\"#\" onclick=\"document.getElementById('ACTION_ID').value='{c}'; document.forms['FORM_NAME'].submit();\">{c.replace('_',' ')}</a>" for c in self.COMMANDS]
html = f"""<!DOCTYPE html>
<html>
<head> <title>Home A/C</title> </head>
""" + """
<style>
h2 {font-size: 60px}
a {font-size:60px}
p {font-size: 60px}
</style>
""" + f"""
<body>
<p>{ params['GET'].get('msg','').replace('_',' ') }</p>"""
# Show the menu for each location
for (loc_ip, loc) in PHYSICAL_LOCATIONS.items():
(phys_loc, loc_port) = loc
action=f"http://{OUTSIDE_IP}:{loc_port}" + self.PATH
form_name = phys_loc.replace(' ','_')
action_id = form_name + '_action'
html += f"""
<h2>{phys_loc} A/C</h2>
<form method='POST' action="{action}" name="{form_name}" accept-charset=utf-8>
{('<br>'.join(command_a_elements)).replace('FORM_NAME',form_name).replace('ACTION_ID',action_id)}
<input type="hidden" name="action" id="{action_id}" value="None">
</form>
"""
html += """
</body>
</html>
"""
# Send Response
self.send_200_header(writer)
writer.write(html)
finally:
await writer.drain()
await writer.wait_closed()
async def main():
ac = ACControl()
blinker = LEDBlinker()
await blinker.blink(5, 250)
while 1: # Keep trying, despite wifi disconnections
print("Starting Wifi Connection")
async with WifiConnection(blinker) as wifi:
await blinker.blink(10, 25)
connectionHandler = ConnectionHandler(ac, blinker, wifi.physical_location)
print("Starting Server")
asyncio.create_task(asyncio.start_server(connectionHandler.serve_client, "0.0.0.0", 80))
hb_ctr = 0
hb_chars = '♡♥'
while 1:
print(f"heartbeat {hb_chars[hb_ctr]}", end = '\r')
hb_ctr = ( hb_ctr + 1 ) % len(hb_chars)
await asyncio.sleep(1)
asyncio.run(main())