# SPDX-FileCopyrightText: 2022 Michał Pokusa
#
# SPDX-License-Identifier: Unlicense
import board
import socketpool
import wifi
import os
from adafruit_httpserver import Server, Route, as_route, Request, Response, GET, POST
print("My MAC address:", [hex(i) for i in wifi.radio.mac_address])
# Connect to wifi
wifi.radio.connect(os.getenv('CIRCUITPYTHON_SSID'), os.getenv('CIRCUITPYTHON_PASSWORD'))
print("My IP address: ", wifi.radio.ipv4_address)
pool = socketpool.SocketPool(wifi.radio)
# Our Server is established here as well as a default route to our files on the server
server = Server(pool, "/", debug=True)
# A variable to test the connection with, we can set a GPIO pin to turn on if our variable is True
onOffState = True
# This is where your HTML code should go.
# Normally we would use an external file, but for our purposes this works just fine
htmlPage = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>"""
# Here you can create new routes to different files or execute a function based on user input
@server.route("/", GET)
def defaultRequest(request: Request):
return Response(request, f"{htmlPage}", content_type = "text/html")
@server.route("/toggle", GET)
def defaultRequest(request: Request):
onOffState = not onOffState
return Response(request, onOffState, content_type="text/html")
server.serve_forever(str(wifi.radio.ipv4_address))