Files
led-fan/test/ap_microdot.py
T

47 lines
1.1 KiB
Python

"""Soft-AP + a tiny Microdot app.
Requires lib/microdot on the board (see test/README.md).
Connect to SSID AP_SSID, then open http://192.168.4.1/
"""
import network
from microdot import Microdot
AP_SSID = "led-test"
AP_PASSWORD = "" # empty = open network
AP_HOST = "0.0.0.0"
AP_PORT = 80
def start_ap(ssid, password):
ap_if = network.WLAN(network.AP_IF)
ap_if.active(True)
if password:
ap_if.config(essid=ssid, password=password)
else:
ap_if.config(essid=ssid, authmode=network.AUTH_OPEN)
# Toggle like src/wifi.py so the AP actually comes up on ESP32.
ap_if.active(False)
ap_if.active(True)
ip, netmask, gateway, dns = ap_if.ifconfig()
print("AP ssid=%s ip=%s" % (ssid, ip))
return ap_if, ip
app = Microdot()
@app.route("/")
async def index(request):
return "hello from microdot\n", 200, {"Content-Type": "text/plain"}
@app.route("/ping")
async def ping(request):
return {"ok": True}
if __name__ == "__main__":
_, ip = start_ap(AP_SSID, AP_PASSWORD)
print("listening on http://%s:%d/" % (ip, AP_PORT))
app.run(host=AP_HOST, port=AP_PORT, debug=True)