7fa322afb8
ESP-NOW is a proprietary wireless communication protocol which supports connectionless communication between ESP32 and ESP8266 devices, using vendor specific WiFi frames. This commit adds support for this protocol through a new `espnow` module. This commit builds on original work done by @nickzoic, @shawwwn and with contributions from @zoland. Features include: - Use of (extended) ring buffers in py/ringbuf.[ch] for robust IO. - Signal strength (RSSI) monitoring. - Core support in `_espnow` C module, extended by `espnow.py` module. - Asyncio support via `aioespnow.py` module (separate to this commit). - Docs provided at `docs/library/espnow.rst`. Methods available in espnow.ESPNow class are: - active(True/False) - config(): set rx buffer size, read timeout and tx rate - recv()/irecv()/recvinto() to read incoming messages from peers - send() to send messages to peer devices - any() to test if a message is ready to read - irq() to set callback for received messages - stats() returns transfer stats: (tx_pkts, tx_pkt_responses, tx_failures, rx_pkts, lost_rx_pkts) - add_peer(mac, ...) registers a peer before sending messages - get_peer(mac) returns peer info: (mac, lmk, channel, ifidx, encrypt) - mod_peer(mac, ...) changes peer info parameters - get_peers() returns all peer info tuples - peers_table supports RSSI signal monitoring for received messages: {peer1: [rssi, time_ms], peer2: [rssi, time_ms], ...} ESP8266 is a pared down version of the ESP32 ESPNow support due to code size restrictions and differences in the low-level API. See docs for details. Also included is a test suite in tests/multi_espnow. This tests basic espnow data transfer, multiple transfers, various message sizes, encrypted messages (pmk and lmk), and asyncio support. Initial work is from https://github.com/micropython/micropython/pull/4115. Initial import of code is from: https://github.com/nickzoic/micropython/tree/espnow-4115.
114 lines
3.0 KiB
Python
114 lines
3.0 KiB
Python
# Test of a ESPnow echo server and client transferring data.
|
|
# This test works with ESP32 or ESP8266 as server or client.
|
|
# Explicitly tests the irecv(), rev() and recvinto() methods.
|
|
|
|
try:
|
|
import network
|
|
import random
|
|
import espnow
|
|
except ImportError:
|
|
print("SKIP")
|
|
raise SystemExit
|
|
|
|
# Set read timeout to 5 seconds
|
|
timeout_ms = 5000
|
|
default_pmk = b"MicroPyth0nRules"
|
|
sync = True
|
|
|
|
|
|
def echo_server(e):
|
|
peers = []
|
|
while True:
|
|
peer, msg = e.irecv(timeout_ms)
|
|
if peer is None:
|
|
return
|
|
if peer not in peers:
|
|
peers.append(peer)
|
|
e.add_peer(peer)
|
|
|
|
# Echo the MAC and message back to the sender
|
|
if not e.send(peer, msg, sync):
|
|
print("ERROR: send() failed to", peer)
|
|
return
|
|
|
|
if msg == b"!done":
|
|
return
|
|
|
|
|
|
def client_send(e, peer, msg, sync):
|
|
print("TEST: send/recv(msglen=", len(msg), ",sync=", sync, "): ", end="", sep="")
|
|
try:
|
|
if not e.send(peer, msg, sync):
|
|
print("ERROR: Send failed.")
|
|
return
|
|
except OSError as exc:
|
|
# Don't print exc as it is differs for esp32 and esp8266
|
|
print("ERROR: OSError:")
|
|
return
|
|
|
|
|
|
def init(sta_active=True, ap_active=False):
|
|
wlans = [network.WLAN(i) for i in [network.STA_IF, network.AP_IF]]
|
|
e = espnow.ESPNow()
|
|
e.active(True)
|
|
e.set_pmk(default_pmk)
|
|
wlans[0].active(sta_active)
|
|
wlans[1].active(ap_active)
|
|
wlans[0].disconnect() # Force esp8266 STA interface to disconnect from AP
|
|
return e
|
|
|
|
|
|
# Server
|
|
def instance0():
|
|
e = init(True, False)
|
|
multitest.globals(PEERS=[network.WLAN(i).config("mac") for i in (0, 1)])
|
|
multitest.next()
|
|
print("Server Start")
|
|
echo_server(e)
|
|
print("Server Done")
|
|
e.active(False)
|
|
|
|
|
|
# Client
|
|
def instance1():
|
|
# Instance 1 (the client)
|
|
e = init(True, False)
|
|
e.config(timeout_ms=timeout_ms)
|
|
multitest.next()
|
|
peer = PEERS[0]
|
|
e.add_peer(peer)
|
|
|
|
print("RECVINTO() test...")
|
|
msg = bytes([random.getrandbits(8) for _ in range(12)])
|
|
client_send(e, peer, msg, True)
|
|
data = [bytearray(espnow.ADDR_LEN), bytearray(espnow.MAX_DATA_LEN)]
|
|
n = e.recvinto(data)
|
|
print("OK" if data[1] == msg else "ERROR: Received != Sent")
|
|
|
|
print("IRECV() test...")
|
|
msg = bytes([random.getrandbits(8) for _ in range(12)])
|
|
client_send(e, peer, msg, True)
|
|
p2, msg2 = e.irecv()
|
|
print("OK" if msg2 == msg else "ERROR: Received != Sent")
|
|
|
|
print("RECV() test...")
|
|
msg = bytes([random.getrandbits(8) for _ in range(12)])
|
|
client_send(e, peer, msg, True)
|
|
p2, msg2 = e.recv()
|
|
print("OK" if msg2 == msg else "ERROR: Received != Sent")
|
|
|
|
print("ITERATOR() test...")
|
|
msg = bytes([random.getrandbits(8) for _ in range(12)])
|
|
client_send(e, peer, msg, True)
|
|
p2, msg2 = next(e)
|
|
print("OK" if msg2 == msg else "ERROR: Received != Sent")
|
|
|
|
# Tell the server to stop
|
|
print("DONE")
|
|
msg = b"!done"
|
|
client_send(e, peer, msg, True)
|
|
p2, msg2 = e.irecv()
|
|
print("OK" if msg2 == msg else "ERROR: Received != Sent")
|
|
|
|
e.active(False)
|