Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonprickett committed Nov 28, 2022
1 parent e904d42 commit 5f36d66
Showing 1 changed file with 31 additions and 30 deletions.
61 changes: 31 additions & 30 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,44 @@
AP_DOMAIN = "pipico.net"
WIFI_FILE = "wifi.json"

# TODO check for a wifi.json file or something and load secrets from
# that if it is there, then connect to wifi and add different routes.
# if no wifi.py then go with the captive portal routes you see below.

wifi_configured = False
def setup_mode():
print("Entering setup mode...")

def ap_index(request):
if request.headers.get("host") != AP_DOMAIN:
return render_template("redirect.html", domain = AP_DOMAIN)

try:
stat(WIFI_FILE)
# TODO load wifi stuff and make a connection...
wifi_configured = True
except OSError:
pass
# TODO do access point stuff...
return render_template("index.html")

def ap_index(request):
if request.headers.get("host") != AP_DOMAIN:
return render_template("redirect.html", domain = AP_DOMAIN)
def ap_configure(request):
print("Request data...")
print(request.form)
# TODO persist this in the right place, then reboot...
return render_template("configured.html", ssid = request.form["ssid"])

return render_template("index.html")
def ap_catch_all(request):
if request.headers.get("host") != AP_DOMAIN:
return render_template("redirect.html", domain = AP_DOMAIN)

def ap_configure(request):
print("Request data...")
print(request.form)
return render_template("configured.html", ssid = request.form["ssid"])
return "Not found.", 404

def ap_catch_all(request):
if request.headers.get("host") != AP_DOMAIN:
return render_template("redirect.html", domain = AP_DOMAIN)
server.add_route("/", handler = ap_index, methods = ["GET"])
server.add_route("/configure", handler = ap_configure, methods = ["POST"])
server.set_callback(ap_catch_all)

return "Not found.", 404
ap = access_point(AP_NAME)
ip = ap.ifconfig()[0]
dns.run_catchall(ip)

# TODO check for a wifi.json file or something and load secrets from
# that if it is there, then connect to wifi and add different routes.
# if no wifi.py then go with the captive portal routes you see below.

server.add_route("/", handler = ap_index, methods = ["GET"])
server.add_route("/configure", handler = ap_configure, methods = ["POST"])
server.set_callback(ap_catch_all)
try:
stat(WIFI_FILE)
# TODO load wifi stuff and make a connection...
except OSError:
# No wifi configuration, so go to setup mode...
setup_mode()

ap = access_point(AP_NAME)
ip = ap.ifconfig()[0]
dns.run_catchall(ip)
server.run()

0 comments on commit 5f36d66

Please sign in to comment.