From 56f657bf9c22d0bf372f04c54cd4ea41b4434932 Mon Sep 17 00:00:00 2001 From: Tshine Date: Thu, 23 May 2024 20:23:18 +0800 Subject: [PATCH] =?UTF-8?q?wifi=20=E6=8C=87=E7=A4=BA=E7=81=AF=EF=BC=8C?= =?UTF-8?q?=E8=BF=9C=E7=A8=8B=E4=BA=A4=E4=BA=92=EF=BC=8C=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 46 +++++++++++++++++++++++++------------- mpy_common/wifi_connect.py | 27 ++++++++++++++-------- 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/main.py b/main.py index 5d4d80c..b37abf2 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ import time import esp # type: ignore +import webrepl # type: ignore import asm_pcb as ams from mpy_common import wifi_connect @@ -12,28 +13,35 @@ def on_socket_recv(client, data): + data_len = len(data) # 简单检验客户端发送的数据是否正确 - if len(data) == 8 and data[0:6] == b'\x2f\x2f\xff\xfe\x01\x02': # YBA 原来的指令 + if data_len == 8 and data[0:6] == b'\x2f\x2f\xff\xfe\x01\x02': # YBA 原来的指令 ch = int(data[-2]) fx = int(data[-1]) ams.motor_control(ch, fx) - elif len(data) >= 5 and data[0:4] == b'\x2f\x2f\xff\xfe': # 获取内存数据 + elif data_len >= 5 and data[0:4] == b'\x2f\x2f\xff\xfe': cmd = data[4:5] - - if cmd == b'\xff': + if b'\x02' == cmd: # 同步所有通道 + if 6 + 4 == data_len: + fx0 = int.from_bytes(data[6:7], 'big') + fx1 = int.from_bytes(data[7:8], 'big') + fx2 = int.from_bytes(data[8:9], 'big') + fx3 = int.from_bytes(data[9:10], 'big') + ams.motor_control(0, fx0) + ams.motor_control(1, fx1) + ams.motor_control(2, fx2) + ams.motor_control(3, fx3) + elif b'\xff' == cmd: # gc free = gc.mem_free() alloc = gc.mem_alloc() - gc.collect() - gc_free = gc.mem_free() gc_alloc = gc.mem_alloc() - socket_server.send( client, f'esp32c3 memory alloc:{alloc}, free:{free}, gc alloc:{gc_alloc}, gc free:{gc_free}') - elif cmd == b'\xfe': + elif b'\xfe' == cmd: # 获取系统状态信息 gc_free = gc.mem_free() gc_alloc = gc.mem_alloc() socket_server.send( @@ -45,11 +53,18 @@ def on_socket_recv(client, data): def on_client_connect(client, address): ams.led_connect_io.value(1) - def on_client_disconnect(client, address): # TODO:此处最好判断下有几个客户端,如果还有客户端就不要关闭,虽然目前按逻辑只有一个 ams.led_connect_io.value(0) +power_led_shake = True +def on_wifi_state(connected): + global power_led_shake + if connected: + ams.led_power_io.value(1) + else: + ams.led_power_io.value(0 if power_led_shake else 1) + power_led_shake = not power_led_shake esp.osdebug(0) # 禁用调试 # esp.sleep_type(esp.SLEEP_NONE) # 禁用休眠 @@ -64,21 +79,19 @@ def on_client_disconnect(client, address): # ams.self_check() +thread_id = _thread.start_new_thread(wifi_connect.wifi_check, (on_wifi_state, )) # 启动Wifi检测线程 + wifi_connect.wifi_init() # 先连接网络 -sync_ntp() # 同步时间 +sync_ntp(sync_interval=0.1) # 同步时间 log_boot() # 记录启动 -import webrepl - -webrepl.start(password='123456') - -thread_id = _thread.start_new_thread(wifi_connect.wifi_check, ()) # 启动Wifi检测线程 +webrepl.start(password='123456') # 启动 webrepl socket_server = SocketServer('0.0.0.0', 3333, on_socket_recv, need_send=True, on_client_connect=on_client_connect, on_client_disconnect=on_client_disconnect, - recive_size=8) + recive_size=512) socket_server.start() gc.collect() # 执行垃圾回收 @@ -93,4 +106,5 @@ def on_client_disconnect(client, address): ams.gpio_init() wifi_connect.stop_wifi_check() socket_server.stop() + webrepl.stop() print('程序结束\n') diff --git a/mpy_common/wifi_connect.py b/mpy_common/wifi_connect.py index a5217dd..a04aa14 100644 --- a/mpy_common/wifi_connect.py +++ b/mpy_common/wifi_connect.py @@ -103,10 +103,12 @@ def connect_wifi(wlan: network.WLAN, WIFI_SSID: str, WIFI_PASSWORD: str): print("WiFi Connected!") print("IP Address:", wlan.ifconfig()[0]) +_inited = False def wifi_init(): """ Wifi初始化,开机启动时先执行此函数 """ + global _inited # 设置WiFi为station模式并激活 wlan = network.WLAN(network.STA_IF) wlan.active(True) @@ -119,23 +121,30 @@ def wifi_init(): while not smartconfig.done(): wifi_smartconfig() -_running = False + _inited = True -def wifi_check(): +_running = False +def wifi_check(on_wifi_state = None): """ 检测WiFi连接状态并重新连接(如果需要) + + Args: + on_wifi_state (func(connected: bool)): wifi 状态回调函数,每秒一次 """ - global _running + global _running, _inited _running = True print("Wifi 守护线程启动\n") wlan = network.WLAN(network.STA_IF) - WIFI_SSID, WIFI_PASSWORD = read_config() while _running: - if not wlan.isconnected(): - # 记录 wifi 断开 - log('WiFi disconnected, reconnecting...') - wlan.disconnect() - connect_wifi(wlan, WIFI_SSID, WIFI_PASSWORD) + state = wlan.isconnected() + if on_wifi_state: on_wifi_state(state) + + if not state: + if _inited: # 初始化后再进行重连 + log('WiFi disconnected, reconnecting...') # 记录 wifi 断开 + wlan.disconnect() + WIFI_SSID, WIFI_PASSWORD = read_config() + connect_wifi(wlan, WIFI_SSID, WIFI_PASSWORD) time.sleep(1) # 检测WiFi连接状态的间隔 print("Wifi 守护线程结束\n")