backup codes
This commit is contained in:
@@ -198,6 +198,16 @@ async def animation_task():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"动画任务初始化失败: {e}")
|
print(f"动画任务初始化失败: {e}")
|
||||||
|
|
||||||
|
def cb_progress(data):
|
||||||
|
if isinstance(data, bytes):
|
||||||
|
if data == b'/':
|
||||||
|
display.portal_info('load iwconfig page ')
|
||||||
|
elif data == b'/login':
|
||||||
|
display.portal_info('WiFi connecting ... ')
|
||||||
|
elif isinstance(data, str):
|
||||||
|
display.message(data, 19, 204)
|
||||||
|
|
||||||
|
if data: print(f'progress: {str(data)}')
|
||||||
|
|
||||||
def start():
|
def start():
|
||||||
# 初始化液晶屏
|
# 初始化液晶屏
|
||||||
@@ -207,12 +217,14 @@ def start():
|
|||||||
gc.collect()
|
gc.collect()
|
||||||
display.message("WiFi connect ...")
|
display.message("WiFi connect ...")
|
||||||
|
|
||||||
if not wifi_manager.connect():
|
if not wifi_manager.connect(cb_progress):
|
||||||
gc.collect()
|
gc.collect()
|
||||||
from captive_portal import CaptivePortal
|
from captive_portal import CaptivePortal
|
||||||
portal = CaptivePortal()
|
portal = CaptivePortal()
|
||||||
display.portal_win(portal.essid.decode('ascii'))
|
display.portal_win(portal.essid.decode('ascii'))
|
||||||
return portal.start()
|
portal.start(cb_progress)
|
||||||
|
# just reboot
|
||||||
|
machine.reset()
|
||||||
|
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
|
||||||
|
|||||||
@@ -70,15 +70,15 @@ class HTTPServer(BaseServer):
|
|||||||
if sock is self.sock:
|
if sock is self.sock:
|
||||||
# client connecting on port 80, so spawn off a new
|
# client connecting on port 80, so spawn off a new
|
||||||
# socket to handle this connection
|
# socket to handle this connection
|
||||||
print("- Accepting new HTTP connection")
|
# print("- Accepting new HTTP connection")
|
||||||
self.accept(sock)
|
self.accept(sock)
|
||||||
elif event & select.POLLIN:
|
elif event & select.POLLIN:
|
||||||
# socket has data to read in
|
# socket has data to read in
|
||||||
print("- Reading incoming HTTP data")
|
# print("- Reading incoming HTTP data")
|
||||||
self.read(sock)
|
return self.read(sock)
|
||||||
elif event & select.POLLOUT:
|
elif event & select.POLLOUT:
|
||||||
# existing connection has space to send more data
|
# existing connection has space to send more data
|
||||||
print("- Sending outgoing HTTP data")
|
# print("- Sending outgoing HTTP data")
|
||||||
self.write_to(sock)
|
self.write_to(sock)
|
||||||
|
|
||||||
def accept(self, server_sock):
|
def accept(self, server_sock):
|
||||||
@@ -217,6 +217,7 @@ class HTTPServer(BaseServer):
|
|||||||
# host and a valid route
|
# host and a valid route
|
||||||
body, headers = self.get_response(req)
|
body, headers = self.get_response(req)
|
||||||
self.prepare_write(s, body, headers)
|
self.prepare_write(s, body, headers)
|
||||||
|
return req.path
|
||||||
|
|
||||||
def prepare_write(self, s, body, headers):
|
def prepare_write(self, s, body, headers):
|
||||||
# add newline to headers to signify transition to body
|
# add newline to headers to signify transition to body
|
||||||
|
|||||||
@@ -47,18 +47,18 @@ class CaptivePortal:
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def check_valid_wifi(self):
|
def check_valid_wifi(self, callback):
|
||||||
if not wifi_manager.is_connected():
|
if not wifi_manager.is_connected():
|
||||||
if config.is_valid():
|
if config.is_valid():
|
||||||
# have credentials to connect, but not yet connected
|
# have credentials to connect, but not yet connected
|
||||||
# return value based on whether the connection was successful
|
# return value based on whether the connection was successful
|
||||||
return self.connect_to_wifi()
|
return True
|
||||||
# not connected, and no credentials to connect yet
|
# not connected, and no credentials to connect yet
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def captive_portal(self):
|
def captive_portal(self, callback):
|
||||||
print("Starting captive portal")
|
print("Starting captive portal")
|
||||||
self.start_access_point()
|
self.start_access_point()
|
||||||
|
|
||||||
@@ -77,9 +77,9 @@ class CaptivePortal:
|
|||||||
sock, event, *others = response
|
sock, event, *others = response
|
||||||
is_handled = self.handle_dns(sock, event, others)
|
is_handled = self.handle_dns(sock, event, others)
|
||||||
if not is_handled:
|
if not is_handled:
|
||||||
self.handle_http(sock, event, others)
|
callback(self.handle_http(sock, event, others))
|
||||||
|
|
||||||
if self.check_valid_wifi():
|
if self.check_valid_wifi(callback):
|
||||||
print("Connected to WiFi!")
|
print("Connected to WiFi!")
|
||||||
self.http_server.stop(self.poller)
|
self.http_server.stop(self.poller)
|
||||||
self.dns_server.stop(self.poller)
|
self.dns_server.stop(self.poller)
|
||||||
@@ -100,7 +100,7 @@ class CaptivePortal:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def handle_http(self, sock, event, others):
|
def handle_http(self, sock, event, others):
|
||||||
self.http_server.handle(sock, event, others)
|
return self.http_server.handle(sock, event, others)
|
||||||
|
|
||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
print("Cleaning up")
|
print("Cleaning up")
|
||||||
@@ -126,8 +126,8 @@ class CaptivePortal:
|
|||||||
print("Connection failed but keeping configuration for retry")
|
print("Connection failed but keeping configuration for retry")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def start(self):
|
def start(self, callback):
|
||||||
# turn off station interface to force a reconnect
|
# turn off station interface to force a reconnect
|
||||||
wifi_manager.disconnect()
|
wifi_manager.disconnect()
|
||||||
if not self.try_connect_from_file():
|
if not self.try_connect_from_file():
|
||||||
return self.captive_portal()
|
return self.captive_portal(callback)
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ class Display:
|
|||||||
self._backlight = None
|
self._backlight = None
|
||||||
self._brightness = 80 # 默认亮度80%
|
self._brightness = 80 # 默认亮度80%
|
||||||
self._initialized = True
|
self._initialized = True
|
||||||
|
self._pos_y0 = 10
|
||||||
self.en_font = '/rom/fonts/en-8x16.rfont'
|
self.en_font = '/rom/fonts/en-8x16.rfont'
|
||||||
self.cn_font = '/rom/fonts/cn-22x24.bfont'
|
self.cn_font = '/rom/fonts/cn-22x24.bfont'
|
||||||
self.vector_font = '/rom/fonts/en-32x32.hfont'
|
self.vector_font = '/rom/fonts/en-32x32.hfont'
|
||||||
@@ -77,6 +78,7 @@ class Display:
|
|||||||
def clear(self, color=st7789.BLACK):
|
def clear(self, color=st7789.BLACK):
|
||||||
"""清屏"""
|
"""清屏"""
|
||||||
self.tft.fill(color)
|
self.tft.fill(color)
|
||||||
|
self._pos_y0 = 10
|
||||||
|
|
||||||
def show_jpg(self, filename, x=0, y=0, mode=st7789.SLOW):
|
def show_jpg(self, filename, x=0, y=0, mode=st7789.SLOW):
|
||||||
"""显示JPG图片"""
|
"""显示JPG图片"""
|
||||||
@@ -98,8 +100,14 @@ class Display:
|
|||||||
self._brightness = _brightness
|
self._brightness = _brightness
|
||||||
return self._brightness
|
return self._brightness
|
||||||
|
|
||||||
def message(self, msg, x=10, y=10, fg=st7789.WHITE, bg=st7789.BLACK):
|
def message(self, msg, x=10, y=None, fg=st7789.WHITE, bg=st7789.BLACK):
|
||||||
|
if y == None:
|
||||||
|
y = self._pos_y0
|
||||||
self.tft.text(self.en_font, msg, x, y, fg, bg)
|
self.tft.text(self.en_font, msg, x, y, fg, bg)
|
||||||
|
# 每次打印完消息则滚动到下一行
|
||||||
|
self._pos_y0 += 20
|
||||||
|
if self._pos_y0 >= 240-20:
|
||||||
|
self._pos_y0 = 10
|
||||||
|
|
||||||
def window(self, title=None, content=None, info=None):
|
def window(self, title=None, content=None, info=None):
|
||||||
C_FG,C_BG,C_BT = self._COLORS
|
C_FG,C_BG,C_BT = self._COLORS
|
||||||
@@ -117,6 +125,9 @@ class Display:
|
|||||||
if line:
|
if line:
|
||||||
self.tft.write(self.cn_font, line, 19, 68+i*28, C_FG, C_BG)
|
self.tft.write(self.cn_font, line, 19, 68+i*28, C_FG, C_BG)
|
||||||
|
|
||||||
|
def portal_info(self, msg):
|
||||||
|
C_FG,C_BG,C_BT = self._COLORS
|
||||||
|
self.tft.text(self.en_font, msg, 19, 204, C_FG, C_BT)
|
||||||
def portal_win(self, ssid):
|
def portal_win(self, ssid):
|
||||||
tips = [
|
tips = [
|
||||||
"> 连接热点:",
|
"> 连接热点:",
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ class WiFiManager:
|
|||||||
return None
|
return None
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def connect(self):
|
def connect(self, callback=None):
|
||||||
"""尝试连接到WiFi"""
|
"""尝试连接到WiFi"""
|
||||||
# 加载配置
|
# 加载配置
|
||||||
if not self.config.is_valid():
|
if not self.config.is_valid():
|
||||||
@@ -86,7 +86,8 @@ class WiFiManager:
|
|||||||
print(f"Connected successfully! IP: {ip}")
|
print(f"Connected successfully! IP: {ip}")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
print(f"Connection attempt {attempts}/{self.MAX_CONN_ATTEMPTS}...")
|
# print(f"Connection attempt {attempts}/{self.MAX_CONN_ATTEMPTS}...")
|
||||||
|
if callback: callback(f"WiFi connect {attempts} ...")
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
attempts += 1
|
attempts += 1
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user