# 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>My Website</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f0f0f0;
}
header {
background-color: #333;
color: #fff;
padding: 20px;
text-align: center;
}
nav {
background-color: #444;
color: #fff;
padding: 10px;
text-align: center;
}
nav a {
color: #fff;
text-decoration: none;
margin: 0 10px;
}
section {
padding: 20px;
}
footer {
background-color: #333;
color: #fff;
text-align: center;
padding: 10px;
position: fixed;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<header>
<h1>The Test</h1>
</header>
<nav>
<a href="#">Short</a>
<a href="#">Tester</a>
<a href="#">Click here</a>
</nav>
<section>
<h2>About Us</h2>
<p>This a Test.</p>
</section>
<footer>
© 2024 Tester. All rights reserved.
</footer>
</body>
</html>
htmlPage2= """
"""
# 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("/Tester", GET)
def defaultRequest(request: Request):
return Response(request, f"{htmlPage2}", 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))