|
| 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) |
0 commit comments