Skip to content

Latest commit

 

History

History
 
 

aioespnow

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

aioespnow

A supplementary module which extends the micropython espnow module to provide asyncio support.

  • Asyncio support is available on all ESP32 targets as well as those ESP8266 boards which include the asyncio module (ie. ESP8266 devices with at least 2MB flash storage).

API reference

  • class AIOESPNow(): inherits all the methods of the ESPNow class and extends the interface with the following async methods:

    • async AIOESPNow.arecv()

      Asyncio support for ESPNow.recv(). Note that this method does not take a timeout value as argument.

    • async AIOESPNow.airecv()

      Asyncio support for ESPNow.irecv(). Use this method to reduce memory fragmentation, as it will reuse common storage for each new message received, whereas the arecv() method will allocate new memory for every message received.

    • async AIOESPNow.asend(mac, msg, sync=True)

    • async AIOESPNow.asend(msg)

      Asyncio support for ESPNow.send().

    • __aiter__()/async __anext__()

      AIOESPNow also supports reading incoming messages by asynchronous iteration using async for, eg:

      e=AIOESPNow() e.active(True) asyncdefrecv_till_halt(e): asyncformac, msgine: print(mac, msg) ifmsg==b'halt': breakasyncio.run(recv_till_halt(e))

Example Usage

A small async server example::

importnetworkimportaioespnowimportasyncio# A WLAN interface must be active to send()/recv()network.WLAN(network.WLAN.IF_STA).active(True) e=aioespnow.AIOESPNow() # Returns AIOESPNow enhanced with async supporte.active(True) peer=b'\xbb\xbb\xbb\xbb\xbb\xbb'e.add_peer(peer) # Send a periodic ping to a peerasyncdefheartbeat(e, peer, period=30): whileTrue: ifnotawaite.asend(peer, b'ping'): print("Heartbeat: peer not responding:", peer) else: print("Heartbeat: ping", peer) awaitasyncio.sleep(period) # Echo any received messages back to the senderasyncdefecho_server(e): asyncformac, msgine: print("Echo:", msg) try: awaite.asend(mac, msg) exceptOSErroraserr: iflen(err.args) >1anderr.args[1] =='ESP_ERR_ESPNOW_NOT_FOUND': e.add_peer(mac) awaite.asend(mac, msg) asyncdefmain(e, peer, timeout, period): asyncio.create_task(heartbeat(e, peer, period)) asyncio.create_task(echo_server(e)) awaitasyncio.sleep(timeout) asyncio.run(main(e, peer, 120, 10))
close