Skip to content

Commit afdaa96

Browse files
committed
Add documentation of ESP-NOW module
1 parent 5260974 commit afdaa96

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed

config.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,13 @@ theme = "doc-theme"
800800
parent = "firmwareapi@pycom@network"
801801
weight = 25
802802

803+
[[menu.main]]
804+
name = "ESP-NOW"
805+
url = "/firmwareapi/pycom/network/espnow/"
806+
identifier = "firmwareapi@pycom@network@espnow"
807+
parent = "firmwareapi@pycom@network"
808+
weight = 30
809+
803810
[[menu.main]]
804811
name = "Bluetooth"
805812
url = "/firmwareapi/pycom/network/bluetooth/"
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
---
2+
title: "ESPNOW"
3+
aliases:
4+
- firmwareapi/pycom/network/espnow.html
5+
- firmwareapi/pycom/network/espnow.md
6+
- chapter/firmwareapi/pycom/network/espnow
7+
---
8+
This module implements interface to ESP-NOW protocol.
9+
10+
## Usage Example
11+
12+
```python
13+
14+
from network import WLAN
15+
from network import ESPNOW
16+
import binascii
17+
import time
18+
19+
# The callback to be registered when a message has been sent to a Peer
20+
def espnow_tx(result):
21+
# "result" is the parameter in form of 2 element long tuple
22+
# "peer" is the Peer which the message has been sent to
23+
# "sent" is a boolean showing whether the message could be sent
24+
peer, sent = result
25+
mac = peer.addr()
26+
if(sent == False):
27+
print("Sending message to %s failed!" % binascii.hexlify(mac))
28+
else:
29+
print("Message sent to: %s" % (binascii.hexlify(mac)))
30+
31+
# The callback to be registered when a message has been received
32+
def espnow_rx(result):
33+
# "result" is the parameter in form of 3 element long tuple
34+
# "mac" is the MAC address of the sender
35+
# "peer" is the Peer which the message has been received from. If message has been received from a not registered Peer this parameter is None
36+
# "msg" is the payload from the received message
37+
mac, peer, msg = result
38+
if(peer is None):
39+
print("Message received from an unknown Peer, registering...")
40+
peer = ESPNOW.add_peer(mac)
41+
print("Message received from %s with content: %s" % (binascii.hexlify(mac), msg))
42+
peer.send("Sending back an answer")
43+
44+
# The ESPNOW module needs that WLAN is initialized
45+
w = WLAN()
46+
# Initialize the ESPNOW module
47+
ESPNOW.init()
48+
print("ESP-NOW version: %s" % ESPNOW.version())
49+
50+
# Register the callback which will be called on TX
51+
ESPNOW.on_send(espnow_tx)
52+
# Register the callback which will be called on RX
53+
ESPNOW.on_recv(espnow_rx)
54+
# Configure the Primary Master Key which is used to encrypt the Local Master Key
55+
ESPNOW.pmk('0123456789abcdef')
56+
57+
# Add a dedicated Peer with MAC address: 11:22:33:44:55:66
58+
p1 = ESPNOW.add_peer("112233445566")
59+
# Add a dedicated Peer with MAC address: 66:55:44:33:22:11
60+
p2 = ESPNOW.add_peer("665544332211")
61+
# Set the Local Master Key of Peer p1
62+
p1.lmk(b'0123456789123456')
63+
# Do not set LMK for p2, traffic is not encrypted
64+
65+
# Get the number of registered Peers and how many is encrypted
66+
count = ESPNOW.peer_count()
67+
print("Number of Peers: %s, encrypted: %s" % (count[0], count[1]))
68+
69+
# Sending some messages to the Peers individually
70+
i = 0
71+
while i < 10:
72+
# The Peer p1 will only receive this message if the same LMK is configured for the Peer object created for this device on the other device also
73+
p1.send("%s" % (i))
74+
# Message to p2 is not encrypted, on the p2 device encryption for this current device's Peer object should not be configured
75+
p2.send("%s" % (i))
76+
i = i + 1
77+
time.sleep(1)
78+
79+
# Sending 1 message to all Peers which are registered
80+
ESPNOW.send(None, "Hello all Peers!")
81+
82+
# Remove 1 Peer
83+
ESPNOW.del_peer(p1)
84+
85+
# Deinit the module
86+
ESPNOW.deinit()
87+
88+
```
89+
90+
## Initialization
91+
92+
#### ESPNOW.init()
93+
94+
Initialize the ESP-NOW module.
95+
96+
## Methods:
97+
98+
#### ESPNOW.deinit()
99+
100+
Deinitialize the ESP-NOW module.
101+
102+
#### ESPNOW.pmk(pmk)
103+
104+
Configures the Primary Master Key which is used to encrypt the Local Master Key.
105+
106+
* `pmk` is the Primary Master Key to be configured.
107+
108+
The PMK specified by `pmk` is accepted in format of Byte array with length 16.
109+
110+
#### ESPNOW.send(addr, msg)
111+
112+
Sends a message `msg` to a remote device with MAC address `addr`.
113+
114+
* `addr` is the MAC address of the remote device. If `None` is passed then the message is sent to all registered Peers.
115+
* `msg` is the message to send.
116+
117+
#### ESPNOW.on_recv(cb)
118+
119+
Registers the `cb` callback which will be called when a new message has been received.
120+
121+
* `cb` is the callback function to be called, it expects 1 parameter which is a tuple with 3 elements:
122+
* Element 0: is the MAC address of the sender.
123+
* Element 1: is the Peer which the message has been received from. If message has been received from a not registered Peer this parameter is None.
124+
* Element 2: is the payload from the received message.
125+
126+
#### ESPNOW.on_send(cb)
127+
128+
Registers the `cb` callback which will be called when a new message has been sent.
129+
130+
* `cb` is the callback function to be called, it expects 1 parameter which is a tuple with 2 elements:
131+
* Element 0: is the Peer which the message has been sent to.
132+
* Element 1: is a boolean showing whether the message could be sent.
133+
134+
#### ESPNOW.peer_count()
135+
136+
Returns with the number of registered Peers in a form of tuple with 2 elements:
137+
* Element 0: is the number of all the registered Peers.
138+
* Element 1: is the number of encrypted Peers.
139+
140+
#### ESPNOW.version()
141+
142+
Returns with the curently used version of the ESP-NOW protocol.
143+
144+
#### ESPNOW.add_peer(addr, lmk=None)
145+
146+
Creates a new Peer object and registers it into ESP-NOW module.
147+
* `addr` is the MAC address of the Peer to be created. MAC address is accepted in either String format or as a Byte array.
148+
* `lmk` is the Local Master Key to be used when communicating with the Peer. By default it is None which means the communication will not be encrypted.
149+
150+
The LMK specified by `lmk` is accepted in format of Byte array with length 16.
151+
152+
This function returns with an `ESPNOW_Peer` object.
153+
154+
#### ESPNOW.del_peer(Peer)
155+
156+
Destroys the Peer object with type `ESPNOW_Peer`.
157+
158+
## Class ESPNOW_Peer
159+
160+
The ESPNOW_Peer class represents a Peer in the scope of the ESP-NOW module. A new resource can only be created with the `ESPNOW.add_peer` function.
161+
162+
#### Class methods
163+
164+
#### ESPNOW_Peer.addr(addr=None)
165+
166+
Returns with the MAC address of the Peer.
167+
168+
#### ESPNOW_Peer.lmk(lmk=None)
169+
170+
Configures or returns the Local Master Key of the Peer.
171+
* `lmk` is the new LMK to be set to this Peer.
172+
173+
If `lmk` is not used then this functions returns the LMK of the Peer.
174+
The LMK specified by `lmk` is accepted in format of Byte array with length 16.
175+
176+
#### ESPNOW_Peer.send(msg)
177+
178+
Sends a message to the Peer.
179+
* `msg` is the message to send.

0 commit comments

Comments
 (0)