Compare commits

...
Author SHA1 Message Date
iosullivan ab110013c0 Updates further 2026-10-10 19:55:04 +10:00
iosullivan 1b781e93bf Updates for async 2026-10-10 19:28:04 +10:00
iosullivan 6f2a926a27 Updates for Async 2026-10-10 18:58:02 +10:00
14 changed files with 1624 additions and 48 deletions

No files matched your search

Regular → Executable
View File
File mode changed.
Regular → Executable
View File
File mode changed.
View File
File mode changed.
View File
File mode changed.
View File
File mode changed.
@@ -0,0 +1,4 @@
from microdot.microdot import Microdot, Request, Response, abort, redirect, \
send_file, URLPattern, AsyncBytesIO, iscoroutine # noqa: F401
__version__ = '2.7.0'
+24 -48
View File
@@ -1,5 +1,8 @@
from time import time, gmtime, mktime,sleep
import network, machine, ntptime, socket, json, requests,neopixel
import network, machine, ntptime, socket, json, requests,neopixel, asyncio
from microdot import Microdot
app = Microdot()
def time_from_iso(datetime_str):
[yyyy, mm, dd] = [int(i) for i in datetime_str.split('T')[0].split('-')]
@@ -51,21 +54,8 @@ def save_config(data):
print("Configuration saved:", config)
def start_http_server():
addr = socket.getaddrinfo("0.0.0.0", 80)[0][-1]
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(1)
print("HTTP server listening on port 80")
while True:
try:
conn, addr = s.accept()
request = conn.recv(2048).decode()
if request.startswith("POST /save"):
@app.post('/save')
async def save(request):
# Handle form submission
body = request.split("\r\n\r\n", 1)[1]
form_data = parse_form_data(body)
@@ -74,21 +64,24 @@ def start_http_server():
response_body = """<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Setup Complete</title>
<style>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Setup Complete</title>
<style>
body { font-family: sans-serif; padding: 16px; text-align: center; }
h2 { color: #28a745; }
</style>
</style>
</head>
<body>
<h2>Configuration saved</h2>
<p>Device will reboot and connect to Wi-Fi.</p>
<p><small>You can close this window.</small></p>
<h2>Configuration saved</h2>
<p>Device will reboot and connect to Wi-Fi.</p>
<p><small>You can close this window.</small></p>
</body>
</html>"""
else:
# Serve the setup page for any other request
return response_body
@app.get('/')
async def index(request):
try:
with open("portal.html") as f:
response_body = f.read()
@@ -102,25 +95,8 @@ def start_http_server():
+ response_body
)
conn.send(response.encode())
return response
except Exception as e:
print("HTTP error:", e)
try:
error = f"<h2>Error</h2><p>{e}</p>"
conn.send(
"HTTP/1.1 500 Internal Server Error\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n\r\n"
.encode() + error.encode()
)
except:
pass
finally:
try:
conn.close()
except:
pass
def reconnect_wifi():
while not wlan.isconnected():
@@ -132,7 +108,7 @@ def reconnect_wifi():
break
max_wait -= 1
print('Waiting for connection...')
sleep(1)
asyncio.wait(1)
if wlan.isconnected():
print('Connected to Wi-Fi')
print('Network config:', wlan.ifconfig())
@@ -140,7 +116,7 @@ def reconnect_wifi():
except Exception as e:
print('Connection error:', e)
print('Retrying connection...')
sleep(2)
asyncio.wait(2)
# Synchronize time of RTC with NTP
last_ntp_sync = -3700 # Init as more than an hour ago
@@ -178,11 +154,10 @@ try:
except:
# Failed to open file, launch AP mode
start_ap()
start_http_server()
# Halt waiting for config
while(True):
sleep(5)
asyncio.wait(5)
while(True):
# Synchronise NTP time if hasn't been done in 1 hour
@@ -236,4 +211,5 @@ while(True):
# Once all LEDs updated
wled.write()
sleep(1)
asyncio.wait(1)
File diff suppressed because it is too large. Load diff
View File
File mode changed.
View File
File mode changed.
View File
File mode changed.
Regular → Executable
View File
File mode changed.
Regular → Executable
View File
File mode changed.

Before

Width:  |  Height:  |  Size: 55 KiB

After

Width:  |  Height:  |  Size: 55 KiB

Regular → Executable
View File
File mode changed.

Before

Width:  |  Height:  |  Size: 55 KiB

After

Width:  |  Height:  |  Size: 55 KiB