ESP-Now support for ESP32 (and ESP8266)

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Rocket2022
Posts: 4
Joined: Thu Jul 21, 2022 1:10 pm

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

Post by Rocket2022 » Thu Jul 21, 2022 7:23 pm


Rocket2022
Posts: 4
Joined: Thu Jul 21, 2022 1:10 pm

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

Post by Rocket2022 » Thu Jul 21, 2022 7:28 pm

I also double checked this image:

https://github.com/glenn20/micropython- ... ENERIC.bin

BTW:
I fixed the

Code: Select all

from esp import espnow
to

Code: Select all

import espnow

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 » Thu Jul 21, 2022 7:41 pm

Right. However, before you changed it you should have got another error message.
Last edited by davef on Fri Jul 22, 2022 9:42 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 » Thu Jul 21, 2022 7:52 pm

e0 singleton parameters

In the docs I see that the e0 singleton uses irecv to store data:
ESPNow.irecv([timeout])¶

Works like ESPNow.recv() but will re-use internal bytearrays to store the return values: [mac, peer], so that no new memory is allocated on each call.
Does that mean you can keep dumping in e0 traffic until you run out of RAM? Or how many bytearrays and how big?

Thanks,
Dave

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

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

Post by glenn20 » Thu Jul 21, 2022 7:55 pm

Rocket2022 wrote:
Thu Jul 21, 2022 1:17 pm
I'm trying to get esp_now running and created a controller class to handle the esp_now stuff.
When I try to sent a simple data string, I've got an internal error (Error: (-12394, 'ESP_ERR_ESPNOW_INTERNAL')).
I'm using the "firmware-esp32-GENERIC.bin" image.
Has someone an idea what can be wrong?
A quick look at your code and I can see that:

self.wlan.active() should be self.wlan.active(True).

However, I'm not sure that will be the cause of your problem. If you continue to get an espnow internal error, you might try hard resetting the device, eg. Power cycle it.

Good luck.

Rocket2022
Posts: 4
Joined: Thu Jul 21, 2022 1:10 pm

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

Post by Rocket2022 » Fri Jul 22, 2022 7:27 pm

Hi Glenn,

thanks you are right. I forgot the True value...
Now it's working properly.
self.wlan.active() should be self.wlan.active(True).
BR
Marco

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 Jul 22, 2022 11:55 pm

davef wrote:
Thu Jul 21, 2022 7:52 pm
e0 singleton parameters

In the docs I see that the e0 singleton uses irecv to store data:
ESPNow.irecv([timeout])¶

Works like ESPNow.recv() but will re-use internal bytearrays to store the return values: [mac, peer], so that no new memory is allocated on each call.
Does that mean you can keep dumping in e0 traffic until you run out of RAM? Or how many bytearrays and how big?
Thats a good question Dave.

TLDR; e.config(rxbuf=xxxx) might be what you are looking for if you want to increase the buffering of incoming espnow messages ;).

All incoming espnow messages are saved in an internal buffer which is about 530 bytes in size by default. Thats big enough for two full sized messages (250 bytes) plus some overhead for packet headers.

If the buffer fills up, any new messages will be silently dropped (although you can see if messages are being dropped on an esp32 by looking at the data returned by ESPNow.stats()). You can change the size of the buffer using ESPNow.config(rxbuf=2048) before calling active(True).

So, messages will always be saved (up to the limit of the buffer size) until you can read them out of the buffer with recv(), irecv() or recvinto().

recv() allocates a new bytes object for every new message that is received and copies the new message out of the buffer into the bytes object. If you are dealing with a lot of espnow traffic the performance of your app can be impacted by the garbage collection processes triggered by all those memory allocations and can lead to greater memory fragmentation. So, I added irecv() which allocates one bytearray and reuses that for every incoming message.

If you print(id(e.irecv()[1])) for every message you will see that the same memory is used for every incoming message. If you replace irecv() with recv() you will see that new memory is used for every message.

You can see how irecv() works by looking at https://github.com/glenn20/micropython/ ... /espnow.py

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 » Sat Jul 23, 2022 12:28 am

I hadn't "connected" rx_buffer size with irecv() but now see it is really a high-level configuration for all recv functions. Thank you.

Yesterday, I realised that you have a esp32_generic_spiram image. One day I will try that out on a spare SPIRAM variant.

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 » Mon Jul 25, 2022 1:11 am

Can't get the complete repeater link system working with v1.19.1

Three units:
- a remote
- a repeater
- a local

All units have v1.191.1 on them and I have the repeater talking to the local, but the remote doesn't talk to the repeater.

Local w0 channel 6
Repeater w0 channel 6
Remote w0 channel 6

On the remote ...

Code: Select all

    w0 = network.WLAN(network.STA_IF)
    w0.active(True)
    print (w0.config('mac'))
    w0.config(channel=6)
    print (w0.config('channel'))

    e0 = espnow.ESPNow()
#    print (e0)
    e0.active(True)
Tried channel 1 at the remote and leaving the w0 assignment off ... neither worked.

Is this the time you set different channels on the e0 interface just so the remote can talk to the repeater?

Thanks,
Dave

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 Jul 25, 2022 2:21 am

@davef - a few questions:

- are these all esp32 devices?
- does e0.send() on the remote return False or True?
- is the repeater connected to wifi?
- if so, what does w0.config("ps_mode") on the repeater return?

A few things to try:
- after you call e0.add_peer() on the remote, check that e0.get_peers() returns the expected values for the remote (esp. that the channel is 0).

Try sending a message from the repeater to the remote and see if it receives the message.

Post Reply