|
| 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