pycom / pycom-micropython-sigfox

A fork of MicroPython with the ESP32 port customized to run on Pycom's IoT multi-network modules.
MIT License
200 stars 166 forks source link

Add espnow #483

Open rodgergr opened 4 years ago

rodgergr commented 4 years ago

Added espnow class to Pycom code based on earlier work done by Nick Moore and shawwwn for generic micropython. Main changes involved adopting the Pycom approach to callbacks using mp_irq_queue_interrupt() in the ISR routine to route the data to an appropriate queue handler and converted to be a Class. The proper object oriented (Class) version of espnow is in mods/ , but an earlier more primitive version has been left in the esp32 root directory for information only and should be removed before merging. TODO: detection of wifi status at line 253 of espnow is not implemented yet and could be improved by someone with greater familiarity of the wifi code.

geza-pycom commented 3 years ago

@rodgergr : thanks for the contribution, esp-now module will be part of a future release.

rodgergr commented 3 years ago

Hi Geza,

Thank you for your help.

I have been holding off progressing ESPNOW over the last few weeks as it has been significantly enhanced and updated by a contributor to the main MicroPython project and now includes ring buffers to make it more resilient and to avoid the need to use callbacks. There are also a number of other significant improvements.

I have ported this new version to the Pycom environment and am currently testing and debugging it.

When complete, should I issue a new PR? Or do you already have it covered?

Sent from my iPad

On 27/01/2021, at 5:12 AM, Géza Husi notifications@github.com wrote:

 @rodgergr : thanks for the contribution, esp-now module will be part of a future release.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

geza-pycom commented 3 years ago

@rodgergr : yes please append it here and then we can check it and decide what to do. By default I think it should be kept simple, what is the problem with the callbacks?

rodgergr commented 3 years ago

@geza-pycom The following are the key changes with the enhanced version of ESPNow. The ability to use ESPNow inside of an asyncio environment would be most beneficial. Callbacks are still used internally, but no longer exposed.

Ring buffers in esp32/ringbuffer.[ch] for robust IO. Allocate the ESPNow singleton dynamically (protect bufs from gc).

New methods:

  • recv() to read incoming messages (instead of callbacks).
  • irecv() for allocation-free read of incoming messages.
  • Stream IO support: read(), write() and poll through ioctl(). This can be used to support asyncio use of espnow.
  • Also added read1() and readinto1().
  • Add support for iteration on ESPNow object for alloc-free read.
  • config(): set tx and rx buf sizes and read timeout
  • stats(): Returns transfer stats: (tx_packets, tx_packet_responses, rx_packets, lost_rx_packets).
  • add_peer(mac[,lmk[,channel[,ifidx[,encrypt]]]]): Add peer info
  • get_peer(mac): Return peer info: (mac,lmk,channel,ifidx,encrypt)
  • mod_peer(mac, ...) to change peer info parameters.
  • get_peers(): to return all peer info tuples.
rodgergr commented 3 years ago

@geza-pycom I have now merged in the latest changes from the micropython version by glenn20 and this substantially improves the performance and stability of espnow. I have not been able to squash the commits very well, but the key files can be found in esp32/mod and are espnow.c, ringbuffer.c and ringbuffer.h. I also added espnow error codes in esp32/mp_pycom_err.h. Initial tests show that the espnow methods successfully send and receive espnow packets as expected. Please advise next steps.

livius2 commented 3 years ago

Hi this pull request should have bigger priority. As for "ESP now" connectionless communication, no AP is required. It is really simple to use, and it work on quite good ranges. Thank you @rodgergr for this pull request.

geza-pycom commented 3 years ago

Hello! Espnow support has been added to a development release with version 1.20.3.b3, however the implementation has been changed compared to this PR (including changes on API level as well). Code can be found here: https://github.com/pycom/pycom-micropython-sigfox/blob/master_idf_v4.1/esp32/mods/modespnow.c