ESP-Now support for ESP32 (and ESP8266)

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
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 18, 2022 9:34 am

davef wrote:
Mon Jul 18, 2022 6:31 am
I "vote" for clearer.

A note in ESPNow.config() for setting the channel of the remote ESPNow devices when used with an ESPNow/WiFi-connected device. This would need referencing in the ESPNow/WiFi section.
Thanks Dave. I had hoped the Managing wifi channels paragraph and examples addressed that.

The channel is actually set in the network.WLAN.config() method, not the ESPNow.config() method. See https://docs.micropython.org/en/latest/ ... LAN.config.

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

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

Post by davef » Mon Jul 18, 2022 9:48 am

Oops ... w0.config(), I did get it right in the examples I posted previously.

The examples do make channel configuration clear. I was over-reacting to my experience of trying to set it in add_peer()

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 18, 2022 10:11 am

davef wrote:
Mon Jul 18, 2022 9:48 am
Oops ... w0.config(), I did get it right in the examples I posted previously.

The examples do make channel configuration clear. I was over-reacting to my experience of trying to set it in add_peer()
OK - I get it now. Yeah - the espnow docs imply you could/should just register peers on different channels with add_peer() and espnow will automatically handle switching channels. In practice, however, I have found that very unreliable depending on the ESP-IDF version (and fails if you are connected to a wifi AP) and is not a good mode of operation if you also want to receive messages from those peers. It is much easier to just run all your peers on the same channel and set that with w0.config(channel=X) - or just rely on the default channel=1.

I'm sorry that I included add_peer() as an option in my suggestions for setting the channel. I should have been clear that it is better to use w0.config(channel=X).

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

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

Post by davef » Mon Jul 18, 2022 10:18 am

I assume channel in add_peer() is when you want to set the channel in a ESPNow-only system.
Last edited by davef on Thu Jul 21, 2022 1:47 am, edited 1 time in total.

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

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

Post by davef » Tue Jul 19, 2022 10:33 pm

One issue with ESPNow/WiFi is if the WiFi link breaks then you lose traffic on the ESPNow interface.

However, I read this:
f the ESP device is connected to a Wifi Access Point that goes down, the device will start scanning channels in an attempt to reconnect to the Access Point. This means ESPNow messages will be lost while scanning for the AP. This can be disabled by w0.config(reconnects=0), which will also disable the automatic re-connection after losing connection.
So, if I am prepared to miss the 1 in a 1,000,000 messages on the ESPNow interface then I don't need to do any connection testing in my code?

How many messages can the e0 singleton (irecv) hold? And for how long? Currently, I have e0.config(timeout=1000) and then do some other processing, which takes 1-2 seconds. I have been hitting the "proxy", say with 5 messages while the other processing is taking place and when that is done it comes back and it processes all 5 messages. Really nice!
Last edited by davef on Tue Jul 19, 2022 11:39 pm, edited 1 time in total.

Guido
Posts: 2
Joined: Thu Jul 21, 2022 1:02 am

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

Post by Guido » Thu Jul 21, 2022 1:28 am

I am testing the espnow functionality using a Pre-compiled image (firmware-esp32-GENERIC.bin)
The Espnow functionality is working great but I noticed I cannot import the 'urequests' lib anymore.
It looks like 'urequests' is not included in the pre-compiled image?

(I was able to import this lib when I was using the offical 1.19.1 generic-esp32-image)

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 11:10 am

Guido wrote:
Thu Jul 21, 2022 1:28 am
I am testing the espnow functionality using a Pre-compiled image (firmware-esp32-GENERIC.bin)
The Espnow functionality is working great but I noticed I cannot import the 'urequests' lib anymore.
It looks like 'urequests' is not included in the pre-compiled image?

(I was able to import this lib when I was using the offical 1.19.1 generic-esp32-image)
Hi Guido,

Yes - you are right. Your post prompted me to dig a bit deeper into why this happening and I discovered that there is a different manifest_release.py which is used to build esp32 release images (and includes urequests) while the normal build process in the micropython docs uses manifest.py which does not include urequests (and also umqtt as well).

I'll try to adapt my build scripts to include the release manifest (though I'm not quite sure yet how that is done) for my future image builds. In the meantime, you could just copy urequests.py (or urequest.mpy) to your device filesystems and load them from there.

Thanks for raising this. One more thing to tweak in my image build process :-).

Guido
Posts: 2
Joined: Thu Jul 21, 2022 1:02 am

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

Post by Guido » Thu Jul 21, 2022 12:39 pm

Hi Glenn,

Thanks for your answer and all your efforts on this topic!
Unfortunately your suggestion does not work for me
I already tried to copy the requests.py to the esp-device but then I got another error:

AttributeError: 'module' object has no attribute 'IPPROTO_SEC'

I get this when I try to consume an api webservice, using the 'requests.put' command on a https-url.
The only thing I could find on IPPROTO_SEC is that it should come from the (u)socket module:

https://docs.micropython.org/en/latest/ ... PPROTO_SEC

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 1:17 pm

Hi,

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?

Here is the code for the conteroller class:

Code: Select all

from esp import espnow
import network
import time
import ubinascii

import components.device_type.DeviceType as DeviceType

class ESPNowController():

    def __init__(self):
        self.wlan = network.WLAN(network.STA_IF)  # network.STA_IF Or network.AP_IF
        self.wlan.active()        
        print ('wifi initialized')
        self.esp_now = espnow.ESPNow()
        self.esp_now.init()
        print ('esp_now initialized')

    def get_current_mac(self):
        return ubinascii.hexlify(self.wlan.config('mac'),':').decode()
        # return self.wlan.config('mac')

    def add_peer(self, mac):
        self.esp_now.add_peer(mac)

    def send_data(self, data):
        try:
            self.esp_now.send("test")
        except OSError as err:
            print("Error: {0}".format(err))

        time.sleep_ms(1000)    


davef
Posts: 813
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:04 pm

Which image version are you using? It looks like before 1.19.1.

Post Reply