/
billy_demo1.py
92 lines (76 loc) · 2.61 KB
/
billy_demo1.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
from billy import Billy
from time import sleep
import network
from secret import ssid, password
from machine import Pin, WDT, reset
import uasyncio as asyncio
import socket
from time import gmtime
from phew import server, connect_to_wifi
connect_to_wifi(ssid, password)
f = open("index.html","r")
html = f.read()
f.close()
onboard = Pin("LED", Pin.OUT, value=0)
billy = Billy()
stateis = ""
page_views = 0
def what_time_is_it_mr_wolf()->list:
time_list = gmtime()
current_time = {'year': str(time_list[0]),
'month': str(time_list[1]),
'day': str(time_list[2]),
'hour': str(time_list[3]),
'minute': str(time_list[4]),
'second': str(time_list[5])}
return str(current_time['hour'] + ":" + current_time['minute'] + ":" + current_time['second'] + " " + current_time['day'] + "/" + current_time['month'] + "/" + current_time['year'] )
async def serve_client(reader, writer):
global billy, stateis, page_views, boot_time
page_views += 1
print("Client Connected")
request_line = await reader.readline()
print("Request:", request_line)
while await reader.readline() != b"\r\n":
pass
request = str(request_line)
head_out = request.find('/head/out')
head_in = request.find('/head/in')
mouth_open = request.find('/mouth/open')
mouth_close = request.find('/mouth/close')
tail_out = request.find('/tail/out')
tail_in = request.find('/tail/in')
if head_out == 6: billy.head_out()
if head_in == 6: billy.head_in()
if mouth_open == 6: billy.mouth_open()
if mouth_close == 6: billy.mouth_close()
if tail_out == 6: billy.tail_out()
if tail_in == 6: billy.tail_in()
print(f'billy: "{billy}"')
response = html % (billy, billy.status, str(page_views), boot_time)
writer.write('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
writer.write(response)
await writer.drain()
await writer.wait_closed()
print('Client Disconnected')
async def main():
print ("setting up webserver")
asyncio.create_task(asyncio.start_server(serve_client, "0.0.0.0", 80))
wdt = WDT(timeout=8000)
while True:
# check wifi is connected:
if wlan.isconnected() == False:
print("wifi disconnected")
reset()
onboard.on()
print("heartbeat")
wdt.feed()
await asyncio.sleep(0.25)
onboard.off()
await asyncio.sleep(5)
boot_time = what_time_is_it_mr_wolf()
billy.reset()
sleep(0.1)
try:
asyncio.run(main())
finally:
asyncio.new_event_loop()