-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb.py
More file actions
51 lines (44 loc) · 1.18 KB
/
web.py
File metadata and controls
51 lines (44 loc) · 1.18 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
import uasyncio as asyncio
import utime
import machine
import json
from microdot_asyncio import Microdot, send_file, redirect
web = Microdot()
@web.route('/')
async def root(request):
return send_file("index.html")
@web.route('/uptime')
async def uptime(request):
return "I've been awake {} seconds".format(int(utime.ticks_us()/1000000))
@web.route("/setup", methods=["GET", "POST"])
async def setup(request):
if request.method == "POST":
ap = request.form.get("ssid")
pw = request.form.get("password")
im = request.form.get("inmod")
om = request.form.get("outmod")
try:
mp = int(request.form.get("maxpower"))
except (TypeError, ValueError):
mp = None
if all((ap, pw, mp, im, om)):
with open("config.json", "w") as fp:
json.dump({
"ap": ap,
"pw": pw,
"mp": mp,
"im": im,
"om": om,
}, fp)
return redirect("/")
return send_file("setup.html") # , {"Content-Type": "text/html"}
@web.route("/reboot")
async def reboot(request):
asyncio.get_event_loop().create_task(_reboot())
return redirect("/")
async def _reboot():
await asyncio.sleep(1)
machine.reset()
async def main():
print ("Starting web server")
await web.start_server(port=80)