-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.py
131 lines (104 loc) · 4.49 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from phew import access_point, connect_to_wifi, is_connected_to_wifi, dns, server
from phew.template import render_template
import json
import machine
import os
import utime
import _thread
AP_NAME = "pi pico"
AP_DOMAIN = "pipico.net"
AP_TEMPLATE_PATH = "ap_templates"
APP_TEMPLATE_PATH = "app_templates"
WIFI_FILE = "wifi.json"
WIFI_MAX_ATTEMPTS = 3
def machine_reset():
utime.sleep(1)
print("Resetting...")
machine.reset()
def setup_mode():
print("Entering setup mode...")
def ap_index(request):
if request.headers.get("host").lower() != AP_DOMAIN.lower():
return render_template(f"{AP_TEMPLATE_PATH}/redirect.html", domain = AP_DOMAIN.lower())
return render_template(f"{AP_TEMPLATE_PATH}/index.html")
def ap_configure(request):
print("Saving wifi credentials...")
with open(WIFI_FILE, "w") as f:
json.dump(request.form, f)
f.close()
# Reboot from new thread after we have responded to the user.
_thread.start_new_thread(machine_reset, ())
return render_template(f"{AP_TEMPLATE_PATH}/configured.html", ssid = request.form["ssid"])
def ap_catch_all(request):
if request.headers.get("host") != AP_DOMAIN:
return render_template(f"{AP_TEMPLATE_PATH}/redirect.html", domain = AP_DOMAIN)
return "Not found.", 404
server.add_route("/", handler = ap_index, methods = ["GET"])
server.add_route("/configure", handler = ap_configure, methods = ["POST"])
server.set_callback(ap_catch_all)
ap = access_point(AP_NAME)
ip = ap.ifconfig()[0]
dns.run_catchall(ip)
def application_mode():
print("Entering application mode.")
onboard_led = machine.Pin("LED", machine.Pin.OUT)
def app_index(request):
return render_template(f"{APP_TEMPLATE_PATH}/index.html")
def app_toggle_led(request):
onboard_led.toggle()
return "OK"
def app_get_temperature(request):
# Not particularly reliable but uses built in hardware.
# Demos how to incorporate senasor data into this application.
# The front end polls this route and displays the output.
# Replace code here with something else for a 'real' sensor.
# Algorithm used here is from:
# https://www.coderdojotc.org/micropython/advanced-labs/03-internal-temperature/
sensor_temp = machine.ADC(4)
reading = sensor_temp.read_u16() * (3.3 / (65535))
temperature = 27 - (reading - 0.706)/0.001721
return f"{round(temperature, 1)}"
def app_reset(request):
# Deleting the WIFI configuration file will cause the device to reboot as
# the access point and request new configuration.
os.remove(WIFI_FILE)
# Reboot from new thread after we have responded to the user.
_thread.start_new_thread(machine_reset, ())
return render_template(f"{APP_TEMPLATE_PATH}/reset.html", access_point_ssid = AP_NAME)
def app_catch_all(request):
return "Not found.", 404
server.add_route("/", handler = app_index, methods = ["GET"])
server.add_route("/toggle", handler = app_toggle_led, methods = ["GET"])
server.add_route("/temperature", handler = app_get_temperature, methods = ["GET"])
server.add_route("/reset", handler = app_reset, methods = ["GET"])
# Add other routes for your application...
server.set_callback(app_catch_all)
# Figure out which mode to start up in...
try:
os.stat(WIFI_FILE)
# File was found, attempt to connect to wifi...
with open(WIFI_FILE) as f:
wifi_current_attempt = 1
wifi_credentials = json.load(f)
while (wifi_current_attempt < WIFI_MAX_ATTEMPTS):
ip_address = connect_to_wifi(wifi_credentials["ssid"], wifi_credentials["password"])
if is_connected_to_wifi():
print(f"Connected to wifi, IP address {ip_address}")
break
else:
wifi_current_attempt += 1
if is_connected_to_wifi():
application_mode()
else:
# Bad configuration, delete the credentials file, reboot
# into setup mode to get new credentials from the user.
print("Bad wifi connection!")
print(wifi_credentials)
os.remove(WIFI_FILE)
machine_reset()
except Exception:
# Either no wifi configuration file found, or something went wrong,
# so go into setup mode.
setup_mode()
# Start the web server...
server.run()