espnow
– ESP-NOW Module
The espnow
module provides an interface to the
ESP-NOW
protocol provided by Espressif on its SoCs.
Sender
import espnow
e = espnow.ESPNow()
peer = espnow.Peer(mac=b'ªªªªªª')
e.peers.append(peer)
e.send("Starting...")
for i in range(10):
e.send(str(i)*20)
e.send(b'end')
Receiver
import espnow
e = espnow.ESPNow()
packets = []
while True:
if e:
packet = e.read()
packets.append(packet)
if packet.msg == b'end':
break
print("packets:", f"length={len(packets)}")
for packet in packets:
print(packet)
Available on these boards
- class espnow.ESPNow(buffer_size: int = 526, phy_rate: int = 0)
Provides access to the ESP-NOW protocol.
Allocate and initialize
ESPNow
instance as a singleton.- Parameters:
buffer_size (int) – The size of the internal ring buffer. Default: 526 bytes.
phy_rate (int) – The ESP-NOW physical layer rate. Default: 1 Mbps. wifi_phy_rate_t
- __exit__() None
Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.
- send(message: circuitpython_typing.ReadableBuffer, peer: Peer | None = None) None
Send a message to the peer’s mac address.
This blocks until a timeout of
2
seconds if the ESP-NOW internal buffers are full.
- read() ESPNowPacket | None
Read a packet from the receive buffer.
This is non-blocking, the packet is received asynchronously from the peer(s).
- Returns:
An
ESPNowPacket
if available in the buffer, otherwiseNone
.
- send_success: int
The number of tx packets received by the peer(s)
ESP_NOW_SEND_SUCCESS
. (read-only)
- set_pmk(pmk: circuitpython_typing.ReadableBuffer) None
Set the ESP-NOW Primary Master Key (pmk) for encrypted communications.
- Parameters:
pmk (ReadableBuffer) – The ESP-NOW Primary Master Key (length = 16 bytes).
- phy_rate: int
The ESP-NOW physical layer rate. wifi_phy_rate_t
- class espnow.ESPNowPacket
A packet retrieved from ESP-NOW communication protocol. A namedtuple.
- mac: circuitpython_typing.ReadableBuffer
The sender’s mac address (length = 6 bytes).
- msg: circuitpython_typing.ReadableBuffer
The message sent by the peer (length <= 250 bytes).
- class espnow.Peer(mac: bytes, *, lmk: bytes | None, channel: int = 0, interface: int = 0, encrypted: bool = False)
A data class to store parameters specific to a peer.
Construct a new peer object.
- Parameters:
- mac: circuitpython_typing.ReadableBuffer
The WiFi mac to use.
- lmk: circuitpython_typing.ReadableBuffer
The WiFi lmk to use.