ESP-Now support for ESP32 (and ESP8266)

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: ESP-Now support for ESP32 (and ESP8266)

Post by davef » Sun Aug 29, 2021 1:54 am

On the line:

Code: Select all

e.send(local, msg, sync=True)
TypeError: function doesn't take keyword arguments
so I just changed it to 
e.send(local, msg)
This code seems to be working just need to test it as a complete system.

Code: Select all

#  ESPNow repeater

import network
from esp import espnow
import utime


#  A WLAN interface must be active to send()/recv()
w0 = network.WLAN(network.STA_IF)
w0.active(True)
mac = w0.config('mac')
print (mac)

e0 = espnow.ESPNow()
print (e0)

retval = e0.init()

if (retval != None):
    print ('init() did NOT work')
    machine.reset()

local = b'\x08:\xf2\xacj\xe4'
remote = b'\x08:\xf2\xab\xe2\x0c'

retval = e0.add_peer(local)

if (retval != None):
    print ('add peer did NOT work')
    machine.reset()


while True:
    print ('waiting for msg')

    for mac, msg in e0:
        if mac == remote:
            wanted_msg = msg.decode('utf-8')
            print (wanted_msg)
            e0.send(local, msg, True) #  corrected
        else:
            print ('Recv from {}: "{}"'.format(mac, msg))
Thanks again for the kickstart.
Last edited by davef on Sun Aug 29, 2021 7:31 am, edited 1 time in total.

User avatar
glenn20
Posts: 132
Joined: Mon Jun 04, 2018 10:09 am

Re: ESP-Now support for ESP32 (and ESP8266)

Post by glenn20 » Sun Aug 29, 2021 6:04 am

davef wrote:
Sun Aug 29, 2021 1:54 am
This code seems to be working just need to test it as a complete system.
Excellent - a few things you might want to keep in mind when in production:
  • In the for loop, mac, msg will equal None, None if the recv timeout is exceeded.
  • the for loop will exit if the espnow object is not initialised - eg. if you forget to call e.init() before entering the loop or if you call e.deinit() during the loop.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: ESP-Now support for ESP32 (and ESP8266)

Post by davef » Sun Aug 29, 2021 6:11 am

Production, I might end up with a 2nd system for a friend. I'll keep your suggestions in mind.

Any comment about the error:

Code: Select all

e.send(local, msg, sync=True)
TypeError: function doesn't take keyword arguments
so I just changed it to 
e.send(local, msg)
Thanks

User avatar
glenn20
Posts: 132
Joined: Mon Jun 04, 2018 10:09 am

Re: ESP-Now support for ESP32 (and ESP8266)

Post by glenn20 » Sun Aug 29, 2021 7:03 am

davef wrote:
Sun Aug 29, 2021 6:11 am
Production, I might end up with a 2nd system for a friend. I'll keep your suggestions in mind.

Any comment about the error:

Code: Select all

e.send(local, msg, sync=True)
TypeError: function doesn't take keyword arguments
so I just changed it to 
e.send(local, msg)
Thanks
I would expect that message on an esp8266 (I don't allow keyword args on the esp8266 because I have to squeeze the size of the code so damn hard to make it fit without disabling other micropython functionality).

Correction: Actually, I just checked and the 'sync' argument is also only a positional argument on the ESP32 too. At one point in history it was also a keyword arg - and I got confused!!! Sorry about that.

If you do want to specify the "sync" argument, you can just provide it as a 3rd positional argument, eg:

Code: Select all

e.send(local, msg, True)
or

Code: Select all

e.send(local, msg, False)
It defaults to True if you don't specify it. Generally it is best to keep sync=True unless you are trying to achieve high throughput and don't care about possibly missing a few messages - or possibly if you are really trying to save every electron in your battery ;).
Last edited by glenn20 on Sun Aug 29, 2021 7:23 am, edited 1 time in total.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: ESP-Now support for ESP32 (and ESP8266)

Post by davef » Sun Aug 29, 2021 7:22 am

For all my "production" and testing I only use ESP32. I see I didn't understand how to call True!

I'll worry about the electrons after I gain some understanding of using the various sleep modes on the ESP32 with ESP-Now.

fromvega
Posts: 4
Joined: Mon Aug 30, 2021 12:23 am

Re: ESP-Now support for ESP32 (and ESP8266)

Post by fromvega » Mon Aug 30, 2021 12:32 am

Hey friends, I wanted to try using the ESP-NOW library but I'm getting an error when trying to import it. Seems like espnow is not available in the module esp. I did a search in the source code and there's nothing related to espnow. I'm a bit confused because I can see there are docs for it at https://micropython-glenn20.readthedocs ... spnow.html.

And then I found this https://github.com/micropython/micropython/pull/6515, so it seems like the library is not yet merged? Do I need to switch to that branch and recompile? :?:

I would be glad if someone could help me getting this setup.

Thank you very much,

User avatar
glenn20
Posts: 132
Joined: Mon Jun 04, 2018 10:09 am

Re: ESP-Now support for ESP32 (and ESP8266)

Post by glenn20 » Mon Aug 30, 2021 6:03 am

fromvega wrote:
Mon Aug 30, 2021 12:32 am
Hey friends, I wanted to try using the ESP-NOW library but I'm getting an error when trying to import it. Seems like espnow is not available in the module esp. I did a search in the source code and there's nothing related to espnow. I'm a bit confused because I can see there are docs for it at https://micropython-glenn20.readthedocs ... spnow.html.

And then I found this https://github.com/micropython/micropython/pull/6515, so it seems like the library is not yet merged? Do I need to switch to that branch and recompile? :?:

I would be glad if someone could help me getting this setup.

Thank you very much,
Yes, you can checkout that branch (at https://github.com/glenn20/micropython/tree/espnow-g20), follow the build and compile instructions at: https://github.com/micropython/micropyt ... /README.md.

Or, you can download a pre-compiled image from: https://github.com/glenn20/micropython-espnow-images.

Good luck.

User avatar
glenn20
Posts: 132
Joined: Mon Jun 04, 2018 10:09 am

Re: ESP-Now support for ESP32 (and ESP8266)

Post by glenn20 » Fri Sep 03, 2021 5:04 am

Update: New v1.17 Images available, Add on_recv callback, Updated docs.

I have added support for scheduling a callback whenever an espnow message is received. A simple example:

Code: Select all

def recv_cb(e):
    print(e.irecv(0))
e.config(on_recv=recv_cb)
However, you should check Notes on using on_recv_callbacks before using.

New espnow-enabled micropython release v1.17 images are available at https://github.com/glenn20/micropython-espnow-images.
These images have been:
  • built from the espnow-g20-v1.17 branch which contains the latest espnow code rebased against micropython release v1.17.
I have also been updating the docs following various clarifications arising out of this forum (thanks for unearthing the ambiguities etc.) and these are now merged into the main espnow-g20 branch (PR#6515).

User avatar
glenn20
Posts: 132
Joined: Mon Jun 04, 2018 10:09 am

Re: ESP-Now support for ESP32 (and ESP8266)

Post by glenn20 » Fri Sep 03, 2021 6:33 am

glenn20 wrote:
Fri Sep 03, 2021 5:04 am
Update: New v1.17 Images available, Add on_recv callback, Updated docs.

New espnow-enabled micropython release v1.17 images are available at https://github.com/glenn20/micropython-espnow-images.
These images have been:
  • built from the espnow-g20-v1.17 branch which contains the latest espnow code rebased against micropython release v1.17.
Also, if you like to compile your own images, you may prefer to use the release branches (eg. espnow-g20-v1.17 or
espnow-g20-v116) instead of espnow-g20 (which is regularly rebased against the main branch).

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: ESP-Now support for ESP32 (and ESP8266)

Post by davef » Sun Sep 19, 2021 8:12 am

Links to a ESP-Now repeater implementation used in a water tank level alarm system.
viewtopic.php?f=5&t=11145 and
https://github.com/davefes/ESP-Now-repeater

Post Reply