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 1617 additions and 41 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'
+17 -41
View File
@@ -1,5 +1,8 @@
from time import time, gmtime, mktime,sleep 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): def time_from_iso(datetime_str):
[yyyy, mm, dd] = [int(i) for i in datetime_str.split('T')[0].split('-')] [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) print("Configuration saved:", config)
def start_http_server(): @app.post('/save')
addr = socket.getaddrinfo("0.0.0.0", 80)[0][-1] async def save(request):
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"):
# Handle form submission # Handle form submission
body = request.split("\r\n\r\n", 1)[1] body = request.split("\r\n\r\n", 1)[1]
form_data = parse_form_data(body) form_data = parse_form_data(body)
@@ -87,8 +77,11 @@ def start_http_server():
<p><small>You can close this window.</small></p> <p><small>You can close this window.</small></p>
</body> </body>
</html>""" </html>"""
else:
# Serve the setup page for any other request return response_body
@app.get('/')
async def index(request):
try: try:
with open("portal.html") as f: with open("portal.html") as f:
response_body = f.read() response_body = f.read()
@@ -102,25 +95,8 @@ def start_http_server():
+ response_body + 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(): def reconnect_wifi():
while not wlan.isconnected(): while not wlan.isconnected():
@@ -132,7 +108,7 @@ def reconnect_wifi():
break break
max_wait -= 1 max_wait -= 1
print('Waiting for connection...') print('Waiting for connection...')
sleep(1) asyncio.wait(1)
if wlan.isconnected(): if wlan.isconnected():
print('Connected to Wi-Fi') print('Connected to Wi-Fi')
print('Network config:', wlan.ifconfig()) print('Network config:', wlan.ifconfig())
@@ -140,7 +116,7 @@ def reconnect_wifi():
except Exception as e: except Exception as e:
print('Connection error:', e) print('Connection error:', e)
print('Retrying connection...') print('Retrying connection...')
sleep(2) asyncio.wait(2)
# Synchronize time of RTC with NTP # Synchronize time of RTC with NTP
last_ntp_sync = -3700 # Init as more than an hour ago last_ntp_sync = -3700 # Init as more than an hour ago
@@ -178,11 +154,10 @@ try:
except: except:
# Failed to open file, launch AP mode # Failed to open file, launch AP mode
start_ap() start_ap()
start_http_server()
# Halt waiting for config # Halt waiting for config
while(True): while(True):
sleep(5) asyncio.wait(5)
while(True): while(True):
# Synchronise NTP time if hasn't been done in 1 hour # Synchronise NTP time if hasn't been done in 1 hour
@@ -236,4 +211,5 @@ while(True):
# Once all LEDs updated # Once all LEDs updated
wled.write() 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