10000 esp8266/espnow: Add espnow support for esp8266. · micropython/micropython@3117e04 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3117e04

Browse files
committed
esp8266/espnow: Add espnow support for esp8266.
Pared down version of the esp32 espnow support. Supports sync and non-sync send()s and alloc-free buffered irecv() with optional timeout. - init([recv_bufsize]) - deinit() - irecv([timeout]) - send(peer, message, [sync]) - add_peer(peer[, lmk[, channel]]) - del_peer(peer) There is NO support for the stream IO functions or asyncio. This is pared down to fit in the .text segment for esp8266 (total textsize is exactly 32768 bytes in GENERIC_1M build). Conditional compile flag so espnow is not compiled for GENERIC_512K target (it is too big to include in standard build). (Also includes minor formatting fixes for esp32).
1 parent f125a8e commit 3117e04

File tree

9 files changed

+624
-21
lines changed

9 files changed

+624
-21
lines changed

docs/library/espnow.rst

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Configuration
6565
-------------
6666

6767
.. method:: ESPNow.init()
68+
ESPNow.init([recv_bufsize]) (ESP8266 only)
6869

6970
Prepare the software and hardware for use of the ESPNow communication
7071
protocol, including:
@@ -74,6 +75,9 @@ Configuration
7475
- invoke esp_now_init() and
7576
- register the send and recv callbacks.
7677

78+
**ESP8266**: The recv buffer size may be set as an argument to `init()` as
79+
there is no `config()` method on the ESP8266.
80+
7781
.. method:: ESPNow.deinit()
7882

7983
De-initialise the Espressif ESPNow software stack (esp_now_deinit()),
@@ -82,7 +86,7 @@ Configuration
8286
**Note**: `deinit()` will also deregister all peers which must be
8387
re-registered after `init()`.
8488

85-
.. method:: ESPNow.config()
89+
.. method:: ESPNow.config() (ESP32 only)
8690
ESPNow.config('param')
8791
ESPNow.config(param=value, ...)
8892

@@ -107,7 +111,7 @@ Configuration
107111
- ``timeout``: *(default=300,000)* Default read timeout (in milliseconds).
108112
The timeout can also be provided as arg to `recv()` and `irecv()`.
109113

110-
.. method:: ESPNow.clear(True)
114+
.. method:: ESPNow.clear(True) (ESP32 only)
111115

112116
Clear out any data in the recv buffer. Use this to clean
113117
up after receiving a ``Buffer error`` (should not happen). All data in the
@@ -134,8 +138,8 @@ For example::
134138
w0 = network.WLAN(network.STA_IF)
135139
w0.active(True)
136140

137-
.. method:: ESPNow.send(mac, msg, [sync=True])
138-
ESPNow.send(msg)
141+
.. method:: ESPNow.send(mac, msg, [sync=True]) (ESP32 only)
142+
ESPNow.send(msg) (ESP32 only)
139143

140144
Send the data contained in ``msg`` to the peer with given network ``mac``
141145
address. In the second form, ``mac=None`` and ``sync=True``.
@@ -163,7 +167,7 @@ For example::
163167
it has initialised it's ESP-Now system or is actively listening for ESP-Now
164168
traffic (see the Espressif ESP-Now docs).
165169

166-
.. method:: ESPNow.recv([timeout])
170+
.. method:: ESPNow.recv([timeout]) (ESP32 only)
167171

168172
Wait for an incoming message and return a newly allocated tuple of
169173
bytestrings: ``(mac, message)``, where:
@@ -189,11 +193,11 @@ For example::
189193
`irecv()<ESPNow.irecv()>`. You must make copies if you
190194
wish to keep the values across calls to ``irecv()``.
191195

192-
.. method:: ESPNow.stats()
196+
.. method:: ESPNow.stats() (ESP32 only)
193197

194198
Return a 5-tuple containing the number of packets sent/received/lost::
195199

196-
(sent_packets, sent_responses, sent_failures, recv_packets, lost_rx_packets)
200+
(sent_packets, sent_responses, sent_failures, recv_packets, dropped_recv_packets)
197201

198202
Incoming packets are *dropped* when the recv buffers are full. To reduce
199203
packet loss, increase the ``rxbuf`` config parameters and ensure you are
@@ -205,6 +209,8 @@ For example::
205209
Iteration over ESPNow
206210
---------------------
207211

212+
**Note**: ESP32 only
213+
208214
It is also possible to read messages by iterating over the ESPNow singleton
209215
object. This will yield ``(mac, message)`` tuples using the alloc-free
210216
`irecv()` method, eg::
@@ -222,7 +228,7 @@ The Esspresif ESP-Now software requires that other devices (peers) must be
222228
*registered* before we can `send()<ESPNow.send()>` them messages.
223229

224230
.. method:: ESPNow.add_peer(mac, [lmk], [channel], [ifidx], [encrypt])
225-
ESPNow.add_peer(mac, 'param'=value, ...)
231+
ESPNow.add_peer(mac, param=value, ...) (ESP32 only)
226232

227233
Add/register the provided ``mac`` address (a 6-byte byte-string) as a peer
228234
under the ESPNow protocol. The following "peer info" parameters may also be
@@ -239,30 +245,33 @@ The Esspresif ESP-Now software requires that other devices (peers) must be
239245
be an integer from 0 to 14. If channel is set to 0 the current channel
240246
of the wifi device will be used. (default=0)
241247

242-
- ``ifidx``: Index of the wifi interface which will be used to send data to
243-
this peer. Must be an integer set to ``network.STA_IF`` (=0) or
244-
``network.AP_IF`` (=1). (default=0/``network.STA_IF``).
248+
- ``ifidx``: *(ESP32 only)* Index of the wifi interface which will be used
249+
to send data to this peer. Must be an integer set to
250+
``network.STA_IF`` (=0) or ``network.AP_IF`` (=1).
251+
(default=0/``network.STA_IF``).
252+
253+
- ``encrypt``: *(ESP32 only)* If set to ``True`` data exchanged with this
254+
peer will be encrypted with the PMK and LMK. (default=``False``)
245255

246-
- ``encrypt``: If set to ``True`` data exchanged with this peer will be
247-
encrypted with the PMK and LMK. (default=``False``)
256+
**ESP8266**: Keyword args may not be used on the ESP8266.
248257

249-
.. method:: ESPNow.get_peer(mac)
258+
.. method:: ESPNow.get_peer(mac) (ESP32 only)
250259

251260
Return a 5-tuple of the "peer info" associated with the ``mac`` address::
252261

253262
(mac, lmk, channel, ifidx, encrypt)
254263

255-
.. method:: ESPNow.peer_count()
264+
.. method:: ESPNow.peer_count() (ESP32 only)
256265

257266
Return the number of peers which have been registered.
258267

259-
.. method:: ESPNow.get_peers()
268+
.. method:: ESPNow.get_peers() (ESP32 only)
260269

261270
Return the "peer info" parameters for all the registered peers (as a tuple
262271
of tuples).
263272

264-
.. method:: ESPNow.mod_peer(mac, lmk, [channel], [ifidx], [encrypt])
265-
ESPNow.mod_peer(mac, 'param'=value, ...)
273+
.. method:: ESPNow.mod_peer(mac, lmk, [channel], [ifidx], [encrypt]) (ESP32 only)
274+
ESPNow.mod_peer(mac, 'param'=value, ...) (ESP32 only)
266275

267276
Modify the parameters of the peer associated with the provided ``mac``
268277
address. Parameters may be provided as positional or keyword arguments.
@@ -274,6 +283,8 @@ The Esspresif ESP-Now software requires that other devices (peers) must be
274283
Stream IO interface
275284
-------------------
276285

286+
**Note**: ESP32 only
287+
277288
**Note**: The ESPNow buffer packet format is not yet fully documented. It
278289
will be supported by a python support module for reading and sending ESPNow
279290
message packets through the ``stream`` interface.
@@ -315,6 +326,8 @@ may wait on received events.
315326
Supporting ``uasyncio``
316327
-----------------------
317328

329+
**Note**: ESP32 only
330+
318331
`ESPNow` uses the ``stream`` io interface to support the micropython
319332
``uasyncio`` module for asynchronous IO. A ``StreamReader`` class may be
320333
constructed from an ESPNow object and used to support async IO. Eg::
@@ -330,6 +343,8 @@ constructed from an ESPNow object and used to support async IO. Eg::
330343
Constants
331344
---------
332345

346+
**Note**: ESP32 only
347+
333348
.. data:: espnow.MAX_DATA_LEN (=250)
334349
espnow.KEY_LEN (=16)
335350
espnow.MAX_TOTAL_PEER_NUM (=20)

ports/esp32/esp_espnow.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ static void _wait_for_pending_responses(esp_espnow_obj_t *self) {
437437
mp_hal_delay_ms(BUSY_WAIT_MS);
438438
}
439439
if (self->sent_responses != self->sent_packets) {
440-
mp_raise_ValueError(MP_ERROR_TEXT("Send timeout on synch."));
440+
mp_raise_ValueError(MP_ERROR_TEXT("Send timeout on synch."));
441441
}
442442
}
443443

@@ -460,7 +460,7 @@ static int _do_espnow_send(
460460
esp_err_t e;
461461
int64_t start = mp_hal_ticks_ms();
462462
while ((e = esp_now_send(peer_addr, message, length))
463-
== ESP_ERR_ESPNOW_NO_MEM &&
463+
== ESP_ERR_ESPNOW_NO_MEM &&
464464
(mp_hal_ticks_ms() - start) <= DEFAULT_SEND_TIMEOUT) {
465465
// Won't yield unless delay > portTICK_PERIOD_MS (10ms)
466466
mp_hal_delay_ms(BUSY_WAIT_MS);

ports/esp8266/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ CFLAGS = $(INC) -Wall -Wpointer-arith -Werror -std=gnu99 -nostdlib -DUART_OS=$(U
6363

6464
LD_FILES ?= boards/esp8266_2m.ld
6565
LDFLAGS = -nostdlib -T $(LD_FILES) -Map=$(@:.elf=.map) --cref
66-
LIBS = -L$(ESP_SDK)/lib -lmain -ljson -llwip_open -lpp -lnet80211 -lwpa -lphy -lnet80211 $(LDFLAGS_MOD)
66+
LIBS = -L$(ESP_SDK)/lib -lmain -ljson -llwip_open -lpp -lnet80211 -lwpa -lphy -lnet80211 -lespnow $(LDFLAGS_MOD)
6767

6868
LIBGCC_FILE_NAME = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
6969
LIBS += -L$(dir $(LIBGCC_FILE_NAME)) -lgcc
@@ -103,6 +103,7 @@ SRC_C = \
103103
machine_wdt.c \
104104
machine_hspi.c \
105105
modesp.c \
106+
esp_espnow.c \
106107
modnetwork.c \
107108
modutime.c \
108109
moduos.c \

ports/esp8266/boards/GENERIC/mpconfigboard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#define MICROPY_PERSISTENT_CODE_LOAD (1)
55
#define MICROPY_EMIT_XTENSA (1)
66
#define MICROPY_EMIT_INLINE_XTENSA (1)
7+
#define MICROPY_ESP8266_ESPNOW (1)
78

89
#define MICROPY_DEBUG_PRINTERS (1)
910
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_NORMAL)

ports/esp8266/boards/GENERIC_1M/mpconfigboard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#define MICROPY_PERSISTENT_CODE_LOAD (1)
55
#define MICROPY_EMIT_XTENSA (1)
66
#define MICROPY_EMIT_INLINE_XTENSA (1)
7+
#define MICROPY_ESP8266_ESPNOW (1)
78

89
#define MICROPY_DEBUG_PRINTERS (1)
910
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_NORMAL)

ports/esp8266/boards/esp8266_common.ld

Lines changed: 1 addition & 0 deletions
5703
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ SECTIONS
8383
*libnet80211.a:(.literal.* .text.*)
8484
*libwpa.a:(.literal.* .text.*)
8585
*libwpa2.a:(.literal.* .text.*)
86+
*libespnow.a:(.literal.* .text.*)
8687

8788
/* we put some specific text in this section */
8889

0 commit comments

Comments
 (0)
0