Updates for async
This commit is contained in:
1 parent
6f2a926a27
commit
1b781e93bf
3 files changed
+1616
-41
No files matched your search
@@ -0,0 +1,4 @@
|
||||
from microdot.microdot import Microdot, Request, Response, abort, redirect, \
|
||||
send_file, URLPattern, AsyncBytesIO, iscoroutine # noqa: F401
|
||||
|
||||
__version__ = '2.7.0'
|
||||
@@ -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)
|
||||
@@ -87,8 +77,11 @@ def start_http_server():
|
||||
<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,7 @@ def start_http_server():
|
||||
+ response_body
|
||||
)
|
||||
|
||||
conn.send(response.encode())
|
||||
|
||||
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
|
||||
return response
|
||||
|
||||
def reconnect_wifi():
|
||||
while not wlan.isconnected():
|
||||
@@ -132,7 +107,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 +115,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
|
||||
@@ -182,7 +157,7 @@ except:
|
||||
|
||||
# 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,4 @@ while(True):
|
||||
# Once all LEDs updated
|
||||
wled.write()
|
||||
|
||||
sleep(1)
|
||||
asyncio.wait(1)
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user