Skip to content

Commit 938e2c0

Browse files
projectgusdpgeorge
authored andcommitted
tests/multi_espnow: Add test case for espnow rate changes.
Uses constants added in previous commit. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent 7e10d22 commit 938e2c0

3 files changed

Lines changed: 124 additions & 1 deletion

File tree

docs/library/espnow.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ Configuration
172172
api-reference/network/esp_wifi.html#_CPPv415wifi_phy_rate_t>`_. This
173173
parameter is actually *write-only* due to ESP-IDF not providing any
174174
means for querying the radio interface's rate parameter.
175-
See also `espnow-long-range`.
175+
See also `espnow-long-range`. This API currently doesn't work on ESP32-C6.
176176

177177
.. data:: Returns:
178178

tests/multi_espnow/75_rate.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Test configuring transmit rates for ESP-NOW on ESP32
2+
3+
import sys
4+
import time
5+
6+
try:
7+
import network
8+
import espnow
9+
except ImportError:
10+
print("SKIP")
11+
raise SystemExit
12+
13+
# ESP8266 doesn't support multiple ESP-NOW data rates
14+
if sys.platform == "esp8266":
15+
print("SKIP")
16+
raise SystemExit
17+
18+
# Currently the config(rate=...) implementation is not compatible with ESP32-C6
19+
# (this test passes when C6 is receiver, but not if C6 is sender.)
20+
if "ESP32C6" in sys.implementation._machine:
21+
print("SKIP")
22+
raise SystemExit
23+
24+
# ESP32-C2 doesn't support Long Range mode. This test is currently written assuming
25+
# LR mode can be enabled.
26+
if "ESP32C2" in sys.implementation._machine:
27+
print("SKIP")
28+
raise SystemExit
29+
30+
31+
timeout_ms = 1000
32+
default_pmk = b"MicroPyth0nRules"
33+
CHANNEL = 9
34+
35+
36+
def init_sta():
37+
sta = network.WLAN(network.WLAN.IF_STA)
38+
e = espnow.ESPNow()
39+
e.active(True)
40+
sta.active(True)
41+
sta.disconnect() # Force AP disconnect for any saved config, important so the channel doesn't change
42+
sta.config(channel=CHANNEL)
43+
e.set_pmk(default_pmk)
44+
# Enable both default 802.11 modes and Long Range modes
45+
sta.config(protocol=network.WLAN.PROTOCOL_LR | network.WLAN.PROTOCOL_DEFAULT)
46+
return sta, e
47+
48+
49+
# Receiver
50+
def instance0():
51+
sta, e = init_sta()
52+
multitest.globals(PEER=sta.config("mac"))
53+
multitest.next()
54+
while True:
55+
peer, msg = e.recv(timeout_ms)
56+
if peer is None:
57+
print("Timeout")
58+
break
59+
# Note that we don't have any way in Python to tell what data rate this message
60+
# was received with, so we're assuming the rate was correct.
61+
print(msg)
62+
e.active(False)
63+
64+
65+
# Sender
66+
def instance1():
67+
sta, e = init_sta()
68+
multitest.next()
69+
peer = PEER
70+
71+
e.add_peer(peer)
72+
# Test normal, non-LR rates
73+
for msg, rate in (
74+
(b"default rate", None),
75+
(b"5Mbit", espnow.RATE_5M),
76+
(b"11Mbit", espnow.RATE_11M),
77+
(b"24Mbit", espnow.RATE_24M),
78+
(b"54Mbit", espnow.RATE_54M),
79+
(b"250K LR", espnow.RATE_LORA_250K),
80+
(b"500K LR", espnow.RATE_LORA_500K),
81+
# switch back to non-LR rates to check it's all OK
82+
(b"1Mbit again", espnow.RATE_1M),
83+
(b"11Mbit again", espnow.RATE_11M),
84+
):
85+
if rate is not None:
86+
e.config(rate=rate)
87+
for _ in range(3):
88+
e.send(peer, msg)
89+
time.sleep_ms(50) # give messages some time to be received before continuing
90+
e.del_peer(peer)
91+
92+
e.active(False)

tests/multi_espnow/75_rate.py.exp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--- instance0 ---
2+
b'default rate'
3+
b'default rate'
4+
b'default rate'
5+
b'5Mbit'
6+
b'5Mbit'
7+
b'5Mbit'
8+
b'11Mbit'
9+
b'11Mbit'
10+
b'11Mbit'
11+
b'24Mbit'
12+
b'24Mbit'
13+
b'24Mbit'
14+
b'54Mbit'
15+
b'54Mbit'
16+
b'54Mbit'
17+
b'250K LR'
18+
b'250K LR'
19+
b'250K LR'
20+
b'500K LR'
21+
b'500K LR'
22+
b'500K LR'
23+
b'1Mbit again'
24+
b'1Mbit again'
25+
b'1Mbit again'
26+
b'11Mbit again'
27+
b'11Mbit again'
28+
b'11Mbit again'
29+
Timeout
30+
--- instance1 ---
31+

0 commit comments

Comments
 (0)