更新espnow 支持wifi联网与广播共存问题

This commit is contained in:
dahanzimin
2026-01-19 12:10:23 +08:00
parent e09689726d
commit 1fd9e7fa86

View File

@@ -15,14 +15,13 @@ from ubinascii import hexlify, unhexlify
import network import network
class ESPNow(espnow.ESPNow): class ESPNow(espnow.ESPNow):
def __init__(self, channel=1, txpower=20): def __init__(self, channel=None, txpower=20):
self._channel = channel
self._txpower = txpower
self._on_handle = {} self._on_handle = {}
self._once_irq = True self._once_irq = True
self._nic = network.WLAN(network.STA_IF) #if version else network.WLAN(network.AP_IF) self._nic = network.WLAN(network.STA_IF) #if version else network.WLAN(network.AP_IF)
self._nic.active(True) self._nic.active(True)
self._nic.config(channel=self._channel, txpower=self._txpower) if channel is not None:
self.set_channel(channel, txpower)
super().__init__() super().__init__()
self.active(True) self.active(True)
@@ -46,11 +45,8 @@ class ESPNow(espnow.ESPNow):
elif err.args[1] == 'ESP_ERR_ESPNOW_IF': elif err.args[1] == 'ESP_ERR_ESPNOW_IF':
self._nic.active(True) self._nic.active(True)
elif err.args[1] == 'ESP_ERR_ESPNOW_NOT_FOUND': elif err.args[1] == 'ESP_ERR_ESPNOW_NOT_FOUND':
super().add_peer(_peer, channel=self._channel) super().add_peer(_peer, channel=self.channel)
try:
return super().send(_peer, str(msg)) return super().send(_peer, str(msg))
except:
raise OSError("ESPNOW channel ({}) conflicts with WiFi channel ({})".format(self._channel, self.channel))
elif err.args[1] == 'ESP_ERR_ESPNOW_NO_MEM': elif err.args[1] == 'ESP_ERR_ESPNOW_NO_MEM':
raise OSError("internal ESP-NOW buffers are full") raise OSError("internal ESP-NOW buffers are full")
elif err.args[1] == 'ESP_ERR_ESPNOW_ARG': elif err.args[1] == 'ESP_ERR_ESPNOW_ARG':
@@ -66,11 +62,11 @@ class ESPNow(espnow.ESPNow):
else : else :
return None,None return None,None
def set_channel(self, channel=None, txpower=None): def set_channel(self, channel=1, txpower=20):
self._channel = self._channel if channel is None else channel if not self._nic.isconnected():
self._nic.config(channel=self._channel, txpower=self._txpower if txpower is None else txpower) self._nic.config(channel=channel, txpower=txpower)
if self._channel != self.channel: else:
print("Warning: The set channel ({}) does not match the actual channel ({})".format(self._channel, self.channel)) print("Warning: WiFi is connected, the actual espnow channel is {}".format(self.channel))
def _cb_handle0(self, event_code, data): def _cb_handle0(self, event_code, data):
'''Callback processing conversion''' '''Callback processing conversion'''