5
5
:synopsis: ESP-NOW wireless protocol support
6
6
7
7
This module provides an interface to the ESP-NOW protocol provided by Espressif
8
- on ESP32 and ESP8266 devices.
8
+ on ESP32 and ESP8266 devices. Some calls are only available on the ESP32 due to
9
+ code size restrictions on the ESP8266 and differences in the Espressif API.
9
10
10
11
Load this module from the :doc: `esp ` module. A simple example would be:
11
12
@@ -42,11 +43,12 @@ Load this module from the :doc:`esp` module. A simple example would be:
42
43
peer = b'\xaa\xaa\xaa\xaa\xaa\xaa' # MAC address of peer's wifi interface
43
44
e.add_peer(peer)
44
45
45
- print(e.irecv())
46
- for msg in e:
47
- print(msg)
48
- if (msg[1] == b'end')
49
- break
46
+ while True:
47
+ msg = e.irecv() # Available on ESP32 and ESP8266
48
+ if msg: # msg == None if timeout in irecv()
49
+ print(msg)
50
+ if msg[1] == b'end':
51
+ break
50
52
51
53
.. note :: This module is still under development and its classes, functions,
52
54
methods and constants are subject to change.
@@ -59,7 +61,8 @@ Constructor
59
61
60
62
.. class :: ESPNow()
61
63
62
- Returns the singleton ESPNow object.
64
+ Returns the singleton ESPNow object. As this is a singleton, all calls to
65
+ `espnow.ESPNow() ` return a reference to the same object.
63
66
64
67
Configuration
65
68
-------------
@@ -76,7 +79,7 @@ Configuration
76
79
- register the send and recv callbacks.
77
80
78
81
**ESP8266 **: The recv buffer size may be set as an argument to `init() ` as
79
- there is no `config() ` method on the ESP8266.
82
+ there is no `config() ` method on the ESP8266 (due to code size restrictions) .
80
83
81
84
.. method :: ESPNow.deinit()
82
85
@@ -86,11 +89,13 @@ Configuration
86
89
**Note **: `deinit() ` will also deregister all peers which must be
87
90
re-registered after `init() `.
88
91
89
- .. method :: ESPNow.config() (ESP32 only)
90
- ESPNow.config('param')
92
+ .. method :: ESPNow.config('param')
91
93
ESPNow.config(param=value, ...)
92
94
93
- Get or set configuration values of the ESPNow interface. To get a value the
95
+ **Note: ** ESP32 only - Use `init([recv_bufsize])<ESPNow.init()> ` on the
96
+ ESP8266.
97
+
98
+ Get or set configuration values of the ESPNow interface. To get a value the
94
99
parameter name should be quoted as a string, and just one parameter is
95
100
queried at a time. To set values use the keyword syntax, and one or more
96
101
parameters can be set at a time.
@@ -138,7 +143,7 @@ For example::
138
143
w0 = network.WLAN(network.STA_IF)
139
144
w0.active(True)
140
145
141
- .. method :: ESPNow.send(mac, msg, [sync=True]) (ESP32 only)
146
+ .. method :: ESPNow.send(mac, msg, [sync=True])
142
147
ESPNow.send(msg) (ESP32 only)
143
148
144
149
Send the data contained in ``msg `` to the peer with given network ``mac ``
@@ -169,15 +174,19 @@ For example::
169
174
170
175
.. method :: ESPNow.recv([timeout]) (ESP32 only)
171
176
172
- Wait for an incoming message and return a newly allocated tuple of
173
- bytestrings: ``(mac, message) ``, where:
177
+ **Note: ** ESP32 only. Use `irecv() ` on the esp8266.
178
+
179
+ Wait for an incoming message and return:
180
+
181
+ - ``None `` if ``timeout `` is reached before a mes
F438
sage is received, or
182
+ - a newly allocated tuple of `bytes `: ``(mac, message) ``, where:
174
183
175
- - ``mac `` is the mac address of the sending device (peer) and
184
+ - ``mac `` is the mac address of the sending device (peer) and
176
185
177
- - ``msg `` is the message/data sent from the peer.
186
+ - ``msg `` is the message/data sent from the peer.
178
187
179
188
``timeout `` optionally sets a timeout (in milliseconds) for the read. The
180
- default timeout can be set in `ESPNow.config() `.
189
+ default timeout (5 minutes) can be set on the ESP32 using `ESPNow.config() `.
181
190
182
191
**Note **: repeatedly calling `recv()<ESPNow.recv()> ` will exercise the
183
192
micropython memory allocation as new storage is allocated for each new
@@ -186,12 +195,30 @@ For example::
186
195
187
196
.. method :: ESPNow.irecv([timeout])
188
197
189
- As for `recv()<ESPNow.recv()> ` except that ``irecv() `` will return a
190
- "callee-owned" tuple of bytearrays.
191
- That is, memory will be allocated once for the tuple and byte strings on
192
- invocation of espnow.ESPNow() and re-used for subsequent calls to
198
+ Wait for an incoming message and return:
199
+
200
+ - ``None `` if ``timeout `` is reached before a message is received, or
201
+ - a "callee-owned" tuple of `bytearray `: ``(mac, message) ``, where:
202
+
203
+ - ``mac `` is the mac address of the sending device (peer) and
204
+
205
+ - ``msg `` is the message/data sent from the peer.
206
+
207
+ ``timeout `` optionally sets a timeout (in milliseconds) for the read. The
208
+ default timeout (5 minutes) can be set on the ESP32 using `ESPNow.config() `.
209
+
210
+ **Note **: Equivalent to `recv()<ESPNow.recv()> `, except that
211
+ `irecv()<ESPNow.irecv()> ` will
212
+ return a "callee-owned" tuple of bytearrays.
213
+ That is, memory will be allocated once for the tuple and bytearrays on
214
+ invocation of `espnow.ESPNow()<ESPNow()> ` and reused for subsequent calls to
193
215
`irecv()<ESPNow.irecv()> `. You must make copies if you
194
- wish to keep the values across calls to ``irecv() ``.
216
+ wish to keep the values across subsequent calls to `irecv()<ESPNow.irecv()> `.
217
+ `irecv()<ESPNow.irecv()> ` is more efficient on memory constrained
218
+ microcontrollers like the ESP32 and ESP8266.
219
+
220
+ On timeout, `irecv() ` will return `None ` and set the length of the
221
+ callee-owned ``message `` bytearray to zero.
195
222
196
223
.. method :: ESPNow.stats() (ESP32 only)
197
224
@@ -255,6 +282,27 @@ The Esspresif ESP-Now software requires that other devices (peers) must be
255
282
256
283
**ESP8266 **: Keyword args may not be used on the ESP8266.
257
284
285
+ **Note **: Managing peers can become complex on the ESP32/8266 if you are
286
+ using more than just the STA_IF interface. The ESP32/8266 effectively has two
287
+ independent wifi interfaces (STA_IF and AP_IF) and each has their own MAC
288
+ address. You must:
289
+
290
+ - choose the correct MAC address of the remote peer (STA_IF or AP_IF) to
291
+ register,
292
+
293
+ - register it with the correct local interface (``ifidx `` = STA_IF or AP_IF),
294
+ and
295
+
296
+ - ensure the correct interfaces are ``active(True) `` on the local and remote
297
+ peer.
298
+
299
+ `ESPNow.send()<ESPNow.send()> ` will raise an
300
+ ``OSError('ESP_ERR_ESPNOW_IF') ``
301
+ exception when trying to send a message to a peer which is registered to a
302
+ local interface which is not ``active(True) ``. Note also that both
303
+ interfaces may be active simultaneously, leading to a lot of flexibility
304
+ in configuring ESPNow and Wifi networks.
305
+
258
306
.. method :: ESPNow.get_peer(mac) (ESP32 only)
259
307
260
308
Return a 5-tuple of the "peer info" associated with the ``mac `` address::
0 commit comments