-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
esp32: ESP-NOW support for ESP32 and ESP8266 #6515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thanks Glenn! I'll do some testing when I get a chance! |
Has this been addressed?
|
Thanks for bringing this up again - it fell off my radar.
I confess I've had no more success than any others in divining the mysteries behind the espressif docs. I think I am doing just what the docs recommend - handing data off to a queue (ring buffer) from the callback functions (I think of them as ISRs). The ring buffer is thread safe so long as only one producer is pushing new data onto the head of the buffer (recv_cb() and send_cb() - which execute in the "high-priority Wifi Task") and only one consumer (callback_wrapper()) is pulling data off the tail of the ring buffer. So, I don't believe it matters which CPU they are being executed on - so long as recv_cb() is never pre-empted by another recv_cb() (on the same or another CPU) and callback_wrapper() is never pre-empted by another callback_wrapper(). I am interested to understand better the concerns around the mp_sched_schedule vulnerability to Pro CPU invocations. If there is some further protection required I'd welcome any guidance. I'm keen to make this as robust as possible. EDIT: I've done some small sustained transfer tests and haven't seen any buffer corruption yet - but that's no guarantee. |
Tried to use espnow, using the documentation docs/library/espnow.rst. I can't init espnow, what argument is it expecting? see code below.
|
Hi Feiko, Thanks for testing. ESPNow is a Class, you need to invoke a class instance first before calling the init() method. The errors is less than useful - I admit. Typical usage (I'll put a short code snippet at the start of the docs in the next commit).
Furthermore, you need to initialise a WLAN interface before calling init() or your ESP32 may reset (on the todo list): eg.
Additionally, you need to invoke |
Thanks Glenn20, Of course! That did the trick. My Python is a bit rusty. A short code snippet would be very welcome. |
@tve: I've done a little empirical testing, but I'm not sure if they are valid in micropython. Using xPortGetCoreID() I tested the coreid for
I was a little surprised because I thought the micropython thread ran on coreid=1 (but I'm not sure why I had acquired that assumption). Is it possible that xPortGetCoreID() does not report correctly (eg. not initialised under micropython)? Let me know if there are any specific effects I should be protecting against. |
Ok - I've just checked and discovered that arising from #5489 micropython recently switched to pin everything to core 0, so the results of my test were not as unexpected as I thought. |
Code looks good to me! |
Thanks @tve,
Actually, I am in the midst of a significant rewrite to remove the
dependence on mp_sched_schedule to schedule callback functions. I have come
to realise the fragility this introduces, and for these purposes it is not
necessary now that we have a buffering solution.
I've also implemented asyncio and stream support (like uart). I'll post a
new commit within 24 hours. It is a pretty substantial change. I understand
that it is not ideal to make significant changes against a PR, but I do
think it will be a much more robust solution.
…On Sun, 11 Oct 2020 at 15:55, Thorsten von Eicken ***@***.***> wrote:
Let me know if there are any specific effects I should be protecting
against.
Code looks good to me!
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#6515 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABVEQRYPXQYQFXLUT5Y6GOTSKE3DBANCNFSM4SDXWCEA>
.
|
Is possible to do a like wlan.scan() to get all APs MAC and their signal? Thank you. |
@beyonlo
By default, espnow devices respond to acknowledge receipt of packets, so it
would be straightforward to scan status of a local network of known Mac
addresses, but not to scan for unknown devices - as far as I am aware.
I'm not aware of any location based uses of espnow, but it might be an
interesting experiment to see if a network of fixed esp32s could be used
for location with rssi or round trip location. The max peer count might be
a limitation.
Cheers,
Glenn.
…On Fri, 16 Oct 2020, 5:15 am beyonlo, ***@***.***> wrote:
@glenn20 <https://github.com/glenn20>
Is possible to do a like wlan.scan() to get all APs MAC and their signal?
Actually, I'm working (ESP32) with wlan.scan() to get location-based on
fixed APs. How is possible to do that with ESPNOW?
Thank you.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#6515 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABVEQR64EEEPGUITZNQZHVDSK432LANCNFSM4SDXWCEA>
.
|
Actually - scrap that. A little further checking and I see that ToF is not
supported in IDF (though there are hints that the hardware may be capable).
People seem to be more optimistic that the ESP32-S2 may get 802.11mc
support at some point in the future.
…On Fri, 16 Oct 2020 at 07:51, Glenn Moloney ***@***.***> wrote:
@beyonlo
By default, espnow devices respond to acknowledge receipt of packets, so
it would be straightforward to scan status of a local network of known Mac
addresses, but not to scan for unknown devices - as far as I am aware.
I'm not aware of any location based uses of espnow, but it might be an
interesting experiment to see if a network of fixed esp32s could be used
for location with rssi or round trip location. The max peer count might be
a limitation.
Cheers,
Glenn.
On Fri, 16 Oct 2020, 5:15 am beyonlo, ***@***.***> wrote:
> @glenn20 <https://github.com/glenn20>
>
> Is possible to do a like wlan.scan() to get all APs MAC and their signal?
> Actually, I'm working (ESP32) with wlan.scan() to get location-based on
> fixed APs. How is possible to do that with ESPNOW?
>
> Thank you.
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <#6515 (comment)>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/ABVEQR64EEEPGUITZNQZHVDSK432LANCNFSM4SDXWCEA>
> .
>
|
As flagged, a substantial rewrite with breaking changes. Apologies for the delay - shoulder injury slowed me down for a while and I spent some time faffing about with various approaches to supporting more reliable IO, allocation-free reads and asyncio support. Learnt a lot more about micropython internals. I know that a substantial change of direction in a pull request is less than ideal, but I do believe the new approach warrants the change. I'm happy to take on board feedback. I will say that I get more reliable and faster data transfers with the new approach. Breaking Changes
Also a substantial internal rewrite to better consolidate the buffer and data packet logic. |
Ok - I've squashed the commits to just two: original import from PR#4115 and my current HEAD. I believe this is ready for review. It is working well for me but happy to hear from others. |
Kudos for the uasyncio support 👍 |
Thanks Peter - and thanks for all your great work on uasyncio and the
magnificent support resources.... :)
…On Mon, 19 Oct 2020 at 21:48, Peter Hinch ***@***.***> wrote:
Kudos for the uasyncio support 👍
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#6515 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABVEQR527VDFIINDB5IJGJ3SLQKQ5ANCNFSM4SDXWCEA>
.
|
For those interested, I now have a heavily pared down version of the esp32 espnow support working on esp8266 available at: https://github.com/glenn20/micropython/tree/espnow-g20-8266. I am planning to make a separate PR for the esp8266 support after a little more testing, but I welcome any testers or users in the meantime. Does not support:
However, it does support:
With this build my compile builds to exactly the max .text size (32768 bytes) for the esp8266. |
Is there any indication when this will be merged and available for download as part of the main micropython binaries? |
Sorry for the slow response (been away on holiday for a few weeks). I don't really know how to get an eye on the process for getting PRs reviewed and merged. I'm just hoping it will be soon-ish. Any suggestions for prodding the decision makers are welcome :-). |
I have added some updates:
ESP32 and ESP8266 users can get their code from this one branch now. |
940b47b
to
bb6b9b8
Compare
Hello, thank you for adding this feature to Micropython. I verified that code is working with actual master branch of MPY and I can send messages esp8266 -> esp32 and esp32 -> esp32. I hope it will be merged soon ;) |
I have just applied a commit which:
I have also rebased against micropython/master as of 28 Jan 2021. |
New commit implementing requested changes (will squash later). |
bed8c46
to
c6f950e
Compare
74bb60a
to
0cfb346
Compare
I think once the above comments are addressed this is go to be merged! If you do a rebase, please put the ringbuf commit first (because the espnow commit relies on it). |
OK. All done. I have also rebased against latest master. The multi-tests succeed on my ESP8266, ESP32, ESP32S2 and ESP32S3 test devices. |
OK, code looks good. Only thing left is some minor tweaks to the commit messages. I would do that during merge, but let me know if you object to that and want to do it yourself. |
Completely happy for you to reword the commit messages as you see fit. Thanks for helping get this through and the improvements to the code. 👍 |
ESP-NOW is a proprietary wireless communication protocol which supports connectionless communication between ESP32 and ESP8266 devices, using vendor specific WiFi frames. This commit adds support for this protocol through a new `espnow` module. This commit builds on original work done by @nickzoic, @shawwwn and with contributions from @zoland. Features include: - Use of (extended) ring buffers in py/ringbuf.[ch] for robust IO. - Signal strength (RSSI) monitoring. - Core support in `_espnow` C module, extended by `espnow.py` module. - Asyncio support via `aioespnow.py` module (separate to this commit). - Docs provided at `docs/library/espnow.rst`. Methods available in espnow.ESPNow class are: - active(True/False) - config(): set rx buffer size, read timeout and tx rate - recv()/irecv()/recvinto() to read incoming messages from peers - send() to send messages to peer devices - any() to test if a message is ready to read - irq() to set callback for received messages - stats() returns transfer stats: (tx_pkts, tx_pkt_responses, tx_failures, rx_pkts, lost_rx_pkts) - add_peer(mac, ...) registers a peer before sending messages - get_peer(mac) returns peer info: (mac, lmk, channel, ifidx, encrypt) - mod_peer(mac, ...) changes peer info parameters - get_peers() returns all peer info tuples - peers_table supports RSSI signal monitoring for received messages: {peer1: [rssi, time_ms], peer2: [rssi, time_ms], ...} ESP8266 is a pared down version of the ESP32 ESPNow support due to code size restrictions and differences in the low-level API. See docs for details. Also included is a test suite in tests/multi_espnow. This tests basic espnow data transfer, multiple transfers, various message sizes, encrypted messages (pmk and lmk), and asyncio support. Initial work is from micropython#4115. Initial import of code is from: https://github.com/nickzoic/micropython/tree/espnow-4115.
Merged! Thanks @glenn20 for persisting with this for such a long time, and to everyone else involved. |
So happy to see this finally merged! Congrats |
woohoo!
... and thanks everyone else involved for picking it up and running with it!
|
Yes, thank you @nickzoic ! |
The MP master branch documentation says I can use this but the code says I can't?
|
@bulletmark |
Thanks Damien. I set up 3 slaves and a master and it works great. I'm completely new to ESPNow but the MP documentation on it is detailed and clear. This is a seriously cool addition to MP! |
@dpgeorge : thanks for merging aioespnow into micropython-lib. Just a reminder of what I think my TODOs are:
If you have any concerns or changes, let me know. |
ESP-NOW is a proprietary wireless communication protocol which supports communication between ESP32 and ESP8266 devices. See: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_now.html
ESP-NOW would be of particular interest for low-power wireless communication requirements, such as battery-powered operations.
This PR is based on the previous work by @nickzoic, @shawwwn and contributions from @zoland, including:
This PR builds on the espnow-4115 branch by adding:
Docs
API docs are provided in 3d4058b. See docs/library/espnow.rst. The latest API docs can be browsed at: https://micropython-glenn20.readthedocs.io/en/latest/library/espnow.html.
This is working reliably for my purposes as a low-level ESPNow interface. Testers and proposed improvements are welcome.
Pre-compiled images
Pre-compiled micropython images with ESPNow support are provided at: https://github.com/glenn20/micropython-espnow-images.
Issues
If you are a user of this PR or the images I provide, please post any Issues at https://github.com/glenn20/micropython/issues or https://github.com/glenn20/micropython-espnow-images/issues.
This conversation thread is intended for discussion about this PR and possible eventual merge into micropython.
Comments, suggestion are welcome.