Skip to content

Commit 5aa4da4

Browse files
committed
clean up and ESP version
1 parent 992eb7f commit 5aa4da4

File tree

2 files changed

+86
-9
lines changed

2 files changed

+86
-9
lines changed

gurgleapps_webserver.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,24 @@ def __init__(self, wifi_ssid, wifi_password, port=80, timeout=20, doc_root="/www
4242
print('waiting for connection...')
4343
time.sleep(1)
4444

45-
if self.wlan.status() != 3:
45+
#if self.wlan.status() != 3:
46+
if self.wlan.isconnected() == False:
4647
raise RuntimeError('network connection failed')
4748
else:
4849
print('connected')
4950
status = self.wlan.ifconfig()
5051
print('ip = ' + status[0])
5152
self.serving = True
5253
print('point your browser to http://', status[0])
53-
try:
54-
pass
55-
# asyncio.run(self.start_server())
56-
except OSError as e:
57-
print(e)
58-
finally:
59-
asyncio.new_event_loop()
54+
#asyncio.new_event_loop()
6055
print("exit constructor")
6156

6257
async def start_server(self):
58+
print("start_server")
6359
asyncio.create_task(asyncio.start_server(
6460
self.serve_request, "0.0.0.0", 80))
6561
while self.serving:
66-
await asyncio.sleep(1)
62+
await asyncio.sleep(0.1)
6763

6864
def add_function_route(self, route, function):
6965
self.function_routes.append({"route": route, "function": function})

main_esp.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import config
2+
from gurgleapps_webserver import GurgleAppsWebserver
3+
import utime as time
4+
import uasyncio as asyncio
5+
from machine import Pin
6+
import ujson as json
7+
8+
blink_off_time = 0.5
9+
blink_on_time = 0.5
10+
11+
status = True
12+
led = Pin(2, Pin.OUT)
13+
14+
async def example_func(request, response, param1, param2):
15+
print("example_func")
16+
print("param1: " + param1)
17+
print("param2: " + param2)
18+
response_string = json.dumps({ "param1": param1, "param2": param2, "post_data": request.post_data})
19+
await response.send_json(response_string, 200)
20+
21+
async def say_hello(request, response, name):
22+
await response.send_html("Hello " + name + " hope you are well")
23+
24+
async def send_status(request, response):
25+
# send boolean status and number frequency
26+
response_string = json.dumps({"status": status, "delay": (blink_off_time + blink_on_time) *0.5, "blink_on_time": blink_on_time, "blink_off_time": blink_off_time})
27+
await response.send_json(response_string, 200)
28+
29+
async def set_blink_pattern(request, response, on, off):
30+
print("on: " + on)
31+
print("off: " + off)
32+
global blink_off_time, blink_on_time
33+
blink_off_time = float(off)
34+
blink_on_time = float(on)
35+
await send_status(request, response)
36+
37+
async def set_delay(request, response, new_delay):
38+
print("new delay: " + new_delay)
39+
global blink_off_time, blink_on_time
40+
blink_off_time = float(new_delay)
41+
blink_on_time = float(new_delay)
42+
await send_status(request, response)
43+
44+
async def stop_flashing(request, response):
45+
global status
46+
status = False
47+
await send_status(request, response)
48+
49+
async def start_flashing(request, response):
50+
global status
51+
status = True
52+
await send_status(request, response)
53+
54+
async def main():
55+
await server.start_server()
56+
57+
async def background_task():
58+
while True:
59+
if status:
60+
led.on()
61+
await asyncio.sleep(blink_on_time)
62+
led.off()
63+
await asyncio.sleep(blink_off_time)
64+
else:
65+
led.off()
66+
await asyncio.sleep(0.2)
67+
68+
69+
async def run():
70+
await asyncio.gather(main(), background_task())
71+
72+
server = GurgleAppsWebserver(config.WIFI_SSID, config.WIFI_PASSWORD, port=80, timeout=30, doc_root="/www", log_level=2)
73+
server.add_function_route("/set-delay/<delay>", set_delay)
74+
server.add_function_route("/set-blink-pattern/<on_time>/<off_time>", set_blink_pattern)
75+
server.add_function_route("/stop", stop_flashing)
76+
server.add_function_route("/start", start_flashing)
77+
server.add_function_route("/status", send_status)
78+
server.add_function_route("/example/func/<param1>/<param2>", example_func)
79+
server.add_function_route("/hello/<name>", say_hello)
80+
81+
asyncio.run(run())

0 commit comments

Comments
 (0)