mirror of
https://github.com/micropython/micropython.git
synced 2025-09-05 17:30:41 +02:00
Some checks failed
JavaScript code lint and formatting with Biome / eslint (push) Has been cancelled
Check code formatting / code-formatting (push) Has been cancelled
Check spelling with codespell / codespell (push) Has been cancelled
Build docs / build (push) Has been cancelled
Check examples / embedding (push) Has been cancelled
Package mpremote / build (push) Has been cancelled
.mpy file format and tools / test (push) Has been cancelled
Build ports metadata / build (push) Has been cancelled
cc3200 port / build (push) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (push) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3) (push) Has been cancelled
esp8266 port / build (push) Has been cancelled
mimxrt port / build (push) Has been cancelled
nrf port / build (push) Has been cancelled
powerpc port / build (push) Has been cancelled
qemu port / build_and_test_arm (push) Has been cancelled
qemu port / build_and_test_rv32 (push) Has been cancelled
renesas-ra port / build_renesas_ra_board (push) Has been cancelled
rp2 port / build (push) Has been cancelled
samd port / build (push) Has been cancelled
stm32 port / build_stm32 (stm32_misc_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_nucleo_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_pyb_build) (push) Has been cancelled
unix port / minimal (push) Has been cancelled
unix port / reproducible (push) Has been cancelled
unix port / standard (push) Has been cancelled
unix port / standard_v2 (push) Has been cancelled
unix port / coverage (push) Has been cancelled
unix port / coverage_32bit (push) Has been cancelled
unix port / nanbox (push) Has been cancelled
unix port / float (push) Has been cancelled
unix port / stackless_clang (push) Has been cancelled
unix port / float_clang (push) Has been cancelled
unix port / settrace (push) Has been cancelled
unix port / settrace_stackless (push) Has been cancelled
unix port / macos (push) Has been cancelled
unix port / qemu_mips (push) Has been cancelled
unix port / qemu_arm (push) Has been cancelled
unix port / qemu_riscv64 (push) Has been cancelled
webassembly port / build (push) Has been cancelled
windows port / build-vs (Debug, x64, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x64, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x86, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x86, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2019, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2019, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2022, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-latest, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2019, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2019, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2022, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-latest, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, dev) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, standard) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, dev) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, standard) (push) Has been cancelled
windows port / cross-build-on-linux (push) Has been cancelled
zephyr port / build (push) Has been cancelled
Python code lint and formatting with ruff / ruff (push) Has been cancelled
Removes the deprecated network.[AP|STA]_IF form from unit tests. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
111 lines
2.8 KiB
Python
111 lines
2.8 KiB
Python
# Test of a ESPnow echo server and asyncio client transferring data.
|
|
# Test will SKIP if instance1 (asyncio client) does not support asyncio.
|
|
# - eg. ESP8266 with 1MB flash.
|
|
# Instance0 is not required to support asyncio.
|
|
|
|
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
|
|
print("OK")
|
|
|
|
|
|
def init(e, sta_active=True, ap_active=False):
|
|
wlans = [network.WLAN(i) for i in [network.WLAN.IF_STA, network.WLAN.IF_AP]]
|
|
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
|
|
|
|
|
|
async def client(e):
|
|
init(e, True, False)
|
|
e.config(timeout_ms=timeout_ms)
|
|
peer = PEERS[0]
|
|
e.add_peer(peer)
|
|
multitest.next()
|
|
|
|
print("airecv() test...")
|
|
msgs = []
|
|
for i in range(5):
|
|
# Send messages to the peer who will echo it back
|
|
msgs.append(bytes([random.getrandbits(8) for _ in range(12)]))
|
|
client_send(e, peer, msgs[i], True)
|
|
|
|
for i in range(5):
|
|
mac, reply = await e.airecv()
|
|
print("OK" if reply == msgs[i] else "ERROR: Received != Sent")
|
|
|
|
# Tell the server to stop
|
|
print("DONE")
|
|
msg = b"!done"
|
|
client_send(e, peer, msg, True)
|
|
mac, reply = await e.airecv()
|
|
print("OK" if reply == msg else "ERROR: Received != Sent")
|
|
|
|
e.active(False)
|
|
|
|
|
|
# Server
|
|
def instance0():
|
|
e = espnow.ESPNow()
|
|
init(e, 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():
|
|
try:
|
|
import asyncio
|
|
from aioespnow import AIOESPNow
|
|
except ImportError:
|
|
print("SKIP")
|
|
raise SystemExit
|
|
asyncio.run(client(AIOESPNow()))
|