M5-ESPNOW
M5-ESPNOW 是一种无连接的无线通信协议,支持:
最多 20 个已注册对等方之间的直接通信:
无需使用无线接入点(AP),
加密和非加密通信(最多 6 个加密对等端)。
消息大小最大为 250 bytes,
可在 ESP32 系列上与 WiFi 操作(network.WLAN)同时运行。
它提供了更简单、更易于使用的 API。
ESPNOW 应用示例:
import os, sys, io
import M5
from M5 import *
from m5espnow import *
import time
import random
def espnow_recv_callback(espnow_obj):
espnow_mac, espnow_data = espnow_obj.recv_data()
print(espnow_mac)
print(espnow_data)
espnow_0 = M5ESPNow(0)
espnow_0.set_irq_callback(espnow_recv_callback)
espnow_0.set_add_peer('xxxxxxxxxxxx', 1, 0, False)
while True::
espnow_0.send_data(1, random.randint(1000000, 9999999))
time.sleep(2)
UiFlow2 应用示例:
class M5ESPNow
Constructors
- class M5ESPNow(wifi_ch)
创建一个 M5ESPNow 对象。
- Arguments:
wifi_ch:与该 peer 通信所使用的 WiFi 信道(2.4 GHz)。必须为 0 到 14 之间的整数。如果信道设置为 0,则使用 WiFi 设备的当前信道。(默认值 = 0)
UiFlow2:

- M5ESPNow.deinit()
反初始化 ESP-NOW 软件栈,禁用回调函数,释放接收数据缓冲区,并注销所有对等设备。
UiFlow2:

- M5ESPNow.get_mac(mode)
获取设备网络的 MAC 地址。
- Arguments:
mode:0:M5ESPNow.STA 1:M5ESPNow.AP
Return:
bytes
UiFlow2:

- M5ESPNow.get_peer_list(encrypt)
返回所有已注册对等端的参数(以列表形式)。
- Arguments:
encrypt:0:普通 peer MAC address;1:加密 peer MAC address。
返回:
list
UiFlow2:

- M5ESPNow.get_remote_mac(select, ssid)
通过远程 SSID 查找远程 MAC 地址。
- Arguments:
select:0:通道;1:MAC。ssid:WiFi 接入点名称,字符串类型。
UiFlow2:

- M5ESPNow.set_ap_ssid(ssid)
设置 AP 模式下的 SSID 配置。
- Arguments:
ssid:WiFi 接入点名称,字符串类型。
UiFlow2:

Methods
- M5ESPNow.set_add_peer(peer_mac, peer_id, ifidx, encrypt, lmk)
将提供的 MAC 地址添加并注册为 peer。
- Arguments:
peer_mac:对端的 MAC 地址(十六进制字符串)。peer_id:MAC 地址存储在 ID 列表中。ID 必须是从 1 到 20 的整数。ifidx:用于向该对等端发送数据的 WiFi 接口索引。必须为整数,设置为network.STA_IF``(=0)或 ``network.AP_IF``(=1)。(默认值为 0/``network.STA_IF)。encrypt:如果设置为True,与此对等端交换的数据将使用 PMK 和 LMK 进行加密。(默认值:如果lmk设置为有效密钥则为False,否则为False。)lmk:用于与该对端加密数据传输的本地主密钥(LMK)密钥(除非encrypt参数设置为False)。必须为:长度为 16 字节的 byte-string 或 bytearray 或 string。
UiFlow2:

- M5ESPNow.set_delete_peer(peer_id)
注销与所提供的
peer_mac地址关联的对等设备。- Arguments:
peer_id:MAC 地址存储在 ID 列表中。ID 必须是从 1 到 20 的整数。
UiFlow2:

- M5ESPNow.set_pmk_encrypt(pmk)
设置主主密钥(PMK),该密钥用于加密本地主密钥(LMK),以便对消息进行加密。如果未设置,则底层 Espressif ESP-NOW 软件栈会使用默认 PMK。
- Arguments:
pmk:必须为长度为 16 字节的字节串、bytearray 或字符串。
UiFlow2:

- M5ESPNow.send_data(peer_id, msg)
使用指定网络将
msg中的数据发送到已存储的对等端 IDpeer_id。- Arguments:
peer_id:MAC 地址存储在 ID 列表中。ID 必须是从 1 到 20 的整数。msg:int、float、list、string 和最大 250 bytes 的 byte-string。
UiFlow2:



- M5ESPNow.broadcast_data(msg)
所有设备也将接收发送到
broadcastMAC 地址(b'\xff\xff\xff\xff\xff\xff')的消息。- Arguments:
msg:int、float、list、string 和最大 250 bytes 的 byte-string。
UiFlow2:



- M5ESPNow.set_irq_callback(callback)
设置一个回调函数,以便在从另一个 ESPNow 设备接收到消息后尽快调用。回调函数将以 ESPNow 实例对象作为参数被调用。为获得更可靠的运行效果,建议在调用回调函数时尽可能读出所有可用消息。:
def espnow_recv_callback(espnow_obj): espnow_mac, espnow_data = espnow_obj.recv_data() print(espnow_mac, espnow_data) M5ESPNow.set_irq_callback(espnow_recv_callback)
- recv_data()
等待接收传入消息并返回值:
[mac, msg]。
UiFlow2:

- M5ESPNow._bytes_to_hex_str(bytes)
将 bytes 字符串转换为十六进制字符串。
- Arguments:
bytes:bytearray
返回:
string:十六进制字符串。
UiFlow2:

- M5ESPNow._hex_str_to_bytes(hexstr)
将十六进制字符串转换为 bytes 字符串。
- Arguments:
hexstr:十六进制字符串
返回:
bytes:bytearray。
UiFlow2:

- M5ESPNow._bytes_to(bytes, format)
从 bytes 字符串中获取 int、float 或 list 类型的值。
- Arguments:
bytes:bytearray。format:0:int,1:float。
返回值:
int or float
UiFlow2:

- M5ESPNow._to_bytes(variable)
从 int、float 或 list 值获取 bytes 字符串。
- Arguments:
variable:int 或 float 或 list
返回:
bytes:bytearray。
UiFlow2:

