Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ab110013c0 | ||
|
|
1b781e93bf | ||
|
|
6f2a926a27 |
No files matched your search
Regular → Executable
File mode changed.
Regular → Executable
File mode changed.
Regular → Executable
File mode changed.
Regular → Executable
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'
|
||||||
Regular → Executable
+24
-48
@@ -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)
|
||||||
@@ -74,21 +64,24 @@ def start_http_server():
|
|||||||
response_body = """<!DOCTYPE html>
|
response_body = """<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<title>Setup Complete</title>
|
<title>Setup Complete</title>
|
||||||
<style>
|
<style>
|
||||||
body { font-family: sans-serif; padding: 16px; text-align: center; }
|
body { font-family: sans-serif; padding: 16px; text-align: center; }
|
||||||
h2 { color: #28a745; }
|
h2 { color: #28a745; }
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h2>Configuration saved</h2>
|
<h2>Configuration saved</h2>
|
||||||
<p>Device will reboot and connect to Wi-Fi.</p>
|
<p>Device will reboot and connect to Wi-Fi.</p>
|
||||||
<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
Regular → Executable
File mode changed.
Regular → Executable
File mode changed.
Regular → Executable
File mode changed.
Regular → Executable
File mode changed.
Regular → Executable
File mode changed.
|
Before Width: | Height: | Size: 55 KiB After Width: | Height: | Size: 55 KiB |
Regular → Executable
File mode changed.
|
Before Width: | Height: | Size: 55 KiB After Width: | Height: | Size: 55 KiB |
Reference in new issue
Block a user