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 » Sat Jul 30, 2022 5:42 am

davef wrote:
Sat Jul 30, 2022 4:12 am
1) the remote w0 channel needs to be the same as the repeater and the proxy, correct?
2) no messing-around with channel assignments in add_peers(), correct?
That SHOULD be the case. It is much better to use the WLAN.config(channel=X) to set the channel. The espressif documentation is really unclear about what setting the channel in add_peer() actually does. It suggests that you are still responsible for setting the channel manually, regardless of what is set in add_peer(), but I believe I have seen versions of the IDF where it would automatically change channels to match what is set in add_peer(). That might have been an experimental feature though.

I have found it best to always leave the channel set to 0 in add_peer() and control the channel yourself with WLAN.config().
One thing to check: on the remote - if you call active(False) or deinit() or enter any form of light sleep the channel will be reset to 1 after you reactivate the wifi.
Now, there is another thing I didn't know. For quite awhile the remote still had lightsleep() in it, the last day or two I commented it out ... because it slowed down the testing I was doing.
As suggested in https://micropython-glenn20.readthedocs ... leep-modes, you should always call WLAN.active(False) before calling lightsleep() and that will reset the channel to 1.

When I get confirmation on 1) and 2) I will do more testing tomorrow.

As always, thank you.
Dave
You are very welcome. Thanks for giving the software (and the docs) a good workout. Theyve both improved significantly thanks to your (and other's) testing and reports.

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 30, 2022 6:08 am

However, applications must disable the WLAN peripheral (using active(False)) before entering light or deep sleep (see Sleep Modes). Otherwise the WiFi radio may not be initialised properly after wake from sleep.
I was doing that in the remote that was using lightsleep().
and that will reset the channel to 1.
If you do a active(False) before lightsleep() the radio should be initialised properly ... except it will be on channel 1, correct?

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

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

Post by glenn20 » Sat Jul 30, 2022 7:15 am

davef wrote:
Sat Jul 30, 2022 6:08 am
However, applications must disable the WLAN peripheral (using active(False)) before entering light or deep sleep (see Sleep Modes). Otherwise the WiFi radio may not be initialised properly after wake from sleep.
I was doing that in the remote that was using lightsleep().
and that will reset the channel to 1.
If you do a active(False) before lightsleep() the radio should be initialised properly ... except it will be on channel 1, correct?
Yes, active(False) calls esp_wifi_stop() if both interfaces are inactive, and active(True) calls esp_wifi_start() if both interfaces are inactive.

While the espressif documentation is not very specific, it says that esp_wifi_start() will "create a new station control block" which implies a complete reset of the wifi configuration (see the comment on the very last line of the light sleep example at the end of my docs - https://micropython-glenn20.readthedocs ... leep-modes).

The same will apply to deepsleep() too.

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 30, 2022 7:43 am

I have made a note to do w0.config(channel=x) after lightsleep() .... when I find out what x is.

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 30, 2022 11:07 am

I have success with sending more than one message from the remote to the repeater. Complete system is now working.
Try adding print("channel =", e0.config('channel')) immediately before every call to e0.send() on the remote.
Threw an error so I changed it to w0 (the WiFi). Even without the lightsleep() it became obvious that what was channel 6 on the first loop now become channel 1. So I added the suggested line of ensuring that w0 was on channel 6 and heh presto. With that addition the unit keeps working even after adding-in the lightsleep().

Just to focus ... this is the working remote processing loop:

Code: Select all

 
        w0.active(True) #  turn on radio only when needed
     #  if you want to save more battery, set sync=False
     #  at cost of not knowing if message was received.

        w0.config(channel=6) #  to put radio back on ch 6 after lightsleep()
                                           #  and after bringing the WiFi back-up
        print ('channel = ', w0.config('channel'))

        retval = e0.send(repeater_mac, status, True)
        print (str(retval))

        w0.active(False)

        wdt.feed()
#        utime.sleep(5) #  for testing
        machine.lightsleep(CYCLE_TIME * 1000)
        wdt.feed()
Looks like trying to save radio power by doing w0.active(False) means that you also lose channel information!

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

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

Post by glenn20 » Sat Jul 30, 2022 12:19 pm

Glad it is working now Dave.
davef wrote:
Sat Jul 30, 2022 11:07 am
I have success with sending more than one message from the remote to the repeater. Complete system is now working.
Try adding print("channel =", e0.config('channel')) immediately before every call to e0.send() on the remote.
Whoops - that should have been w0.config('channel') - <sheepish grin>.
Threw an error so I changed it to w0 (the WiFi). Even without the lightsleep() it became obvious that what was channel 6 on the first loop now become channel 1. So I added the suggested line of ensuring that w0 was on channel 6 and heh presto. With that addition the unit keeps working even after adding-in the lightsleep().

...

Looks like trying to save radio power by doing w0.active(False) means that you also lose channel information!
Yep - if you deconfigure the device, you need to re-configure it again :) .

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 30, 2022 11:44 pm

During the last few weeks I saw a comment to the effect ... that the WiFi would attempt to re-connect if it went down.

So, there is no point in checking that the WiFi is connected to the router, say every hour, correct?

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

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

Post by glenn20 » Sat Aug 06, 2022 4:13 am

davef wrote:
Sat Jul 30, 2022 11:44 pm
During the last few weeks I saw a comment to the effect ... that the WiFi would attempt to re-connect if it went down.

So, there is no point in checking that the WiFi is connected to the router, say every hour, correct?
Yes, the micropython wifi module automatically tries to reconnect if it becomes disconnected from the wifi network. This a micropython feature - not a feature of the underlying Espressif wifi software stack. And you can change that behaviour with WLAN.config(reconnects=X) (see the micropython docs for details).

However, it is not unheard of for the wifi on the esp devices to get itself into some bad state (app, micropython or espressif software or hardware bugs) and you just have to fall back to a hard reset. For example Peter Hinch's resilient mqtt implementation (https://github.com/peterhinch/micropython-mqtt) resets the board if unable to reconnect to wifi. So, for really resilient operation you would want to detect if the device has become disconnected from wifi for an extended period and reset.

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 Aug 06, 2022 4:35 am

OK, thanks for that. My perennial problem of running a program where I am connected to a hotspot. If I send logs every hour everything is OK. If I wait say 24 hours in the connected-state I can log-in to Gmail's SMTP server but the data does not go.

So, for both normal-WiFi and ESPNow/WiFi I keep w0.active(True) all the time and just connect when I want to send then disconnect.
I can't find anything that tells me a Huawei-E8231 gets tired of waiting.

I am now extending the ESPNow/WiFi network to include two more sensors and one controller. Looking really useful. Will give mode_LR another go as I read it is suppose to be good for a 10dB improvement.

Post Reply