{
  "id": "aioespnow",
  "name": "aioespnow",
  "description": "Extends the micropython espnow module with methods to support asyncio.",
  "version": "0.1.0",
  "author": "",
  "license": "MIT",
  "tags": [
    "micropython"
  ],
  "status": "HUMAN_REVIEWED",
  "official": true,
  "source": "https://github.com/micropython/micropython-lib/tree/master/micropython/aioespnow",
  "updatedAt": "2025-05-27T04:19:19.510Z",
  "page": "https://checkmim.com/packages/aioespnow",
  "createdAt": "2025-05-27T04:19:19.510Z",
  "install": {
    "mpremote": "mpremote mip install aioespnow",
    "mip": "import mip; mip.install(\"aioespnow\")"
  },
  "readme": "# `aioespnow`\n\nA supplementary module which extends the micropython `espnow` module to provide\n`asyncio` support.\n\n- Asyncio support is available on all ESP32 targets as well as those ESP8266\nboards which include the `asyncio` module (ie. ESP8266 devices with at least\n2MB flash storage).\n\n## API reference\n\n- class `AIOESPNow()`: inherits all the methods of the `ESPNow` class and\n  extends the interface with the following async methods:\n\n  - `async AIOESPNow.arecv()`\n\n      Asyncio support for ESPNow.recv(). Note that this method does not take a\n      timeout value as argument.\n\n  - `async AIOESPNow.airecv()`\n\n      Asyncio support for ESPNow.irecv(). Use this method to reduce memory\n      fragmentation, as it will reuse common storage for each new message\n      received, whereas the `arecv()` method will allocate new memory for every\n      message received.\n\n  - `async AIOESPNow.asend(mac, msg, sync=True)`\n  - `async AIOESPNow.asend(msg)`\n\n      Asyncio support for ESPNow.send().\n\n  - `__aiter__()/async __anext__()`\n\n      AIOESPNow also supports reading incoming messages by asynchronous\n      iteration using `async for`, eg:\n\n    ```python\n      e = AIOESPNow()\n      e.active(True)\n      async def recv_till_halt(e):\n          async for mac, msg in e:\n              print(mac, msg)\n              if msg == b'halt':\n                  break\n      asyncio.run(recv_till_halt(e))\n    ```\n\n## Example Usage\n\nA small async server example::\n\n```python\n    import network\n    import aioespnow\n    import asyncio\n\n    # A WLAN interface must be active to send()/recv()\n    network.WLAN(network.WLAN.IF_STA).active(True)\n\n    e = aioespnow.AIOESPNow()  # Returns AIOESPNow enhanced with async support\n    e.active(True)\n    peer = b'\\xbb\\xbb\\xbb\\xbb\\xbb\\xbb'\n    e.add_peer(peer)\n\n    # Send a periodic ping to a peer\n    async def heartbeat(e, peer, period=30):\n        while True:\n            if not await e.asend(peer, b'ping'):\n                print(\"Heartbeat: peer not responding:\", peer)\n            else:\n                print(\"Heartbeat: ping\", peer)\n            await asyncio.sleep(period)\n\n    # Echo any received messages back to the sender\n    async def echo_server(e):\n        async for mac, msg in e:\n            print(\"Echo:\", msg)\n            try:\n                await e.asend(mac, msg)\n            except OSError as err:\n                if len(err.args) > 1 and err.args[1] == 'ESP_ERR_ESPNOW_NOT_FOUND':\n                    e.add_peer(mac)\n                    await e.asend(mac, msg)\n\n    async def main(e, peer, timeout, period):\n        asyncio.create_task(heartbeat(e, peer, period))\n        asyncio.create_task(echo_server(e))\n        await asyncio.sleep(timeout)\n\n    asyncio.run(main(e, peer, 120, 10))\n```\n"
}