Skip to content

Commit

Permalink
wifi 指示灯,远程交互,同步逻辑优化
Browse files Browse the repository at this point in the history
  • Loading branch information
TshineZheng committed May 23, 2024
1 parent a0b9120 commit 56f657b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 25 deletions.
46 changes: 30 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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) # 禁用休眠
Expand All @@ -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() # 执行垃圾回收
Expand All @@ -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')
27 changes: 18 additions & 9 deletions mpy_common/wifi_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down

0 comments on commit 56f657b

Please sign in to comment.