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 » Thu Aug 19, 2021 9:49 pm

dhust wrote:
Thu Aug 19, 2021 7:13 pm

Code: Select all

// Set the device as a Station and Soft Access Point simultaneously
WiFi.mode(WIFI_AP_STA);
figuring it would allow me to do both. Not sure if that's true, but I'm not seeing this as an option in MicroPython. I'm not sure how to approach communicating over the wifi for ESP-Now and for texting. Thoughts?
If you set both w0 and w1 active(True) then the wifi mode will be automatically set to WIFI_AP_STA.
On an added note, when I read:
There are two WiFi interfaces, one for the station (when the ESP8266 connects to a router) and one for the access point (for other devices to connect to the ESP8266).
I'm thinking that I can use STA_IF for texting through the WiFi and then use AP_IF to get communications from another ESP32. It doesn't look like that's the case, unless I'm reading it wrong. Simply changing from network.AP_IF to network.STA_IF allows the message to be received.
Yes, you can use the STA_IF for texting and the AP_IF for receiving messages (a good solution to the reliability issue mentioned in my docs). However, you still need to recognise that the AP_IF will operate on the same channel as the Access Point (there is only one radio in the ESP device after all).

Oh - and remember that the AP_IF has a different mac address (so make sure you add_peer() the right MAC address on the sender).
Last edited by glenn20 on Thu Aug 19, 2021 10:02 pm, edited 1 time in total.

mhepp63
Posts: 8
Joined: Wed Apr 28, 2021 2:17 pm

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

Post by mhepp63 » Mon Aug 23, 2021 11:30 am

Glenn20, thanks it helped. I must read the doc better for the next time...
glenn20 wrote:
Mon Aug 09, 2021 2:23 am
I just ran a small test... it works (at least for the small test I ran) if you:

Code: Select all

	...
	w0.active(0)
	machine.lightsleep(1000)
	w0.active(1)
	w0.config(channel=chan)
	...

mhepp63
Posts: 8
Joined: Wed Apr 28, 2021 2:17 pm

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

Post by mhepp63 » Mon Aug 23, 2021 11:35 am

I had a similar problem and I have working solution: https://gitlab.com/mhepp63/espnow-gateway
dhust wrote:
Thu Aug 19, 2021 7:13 pm
Thank you.

I've been having a bit of an issue with receiving an ESP-Now message and then sending a text to my phone. Both work separately, but not together.

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

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

Post by glenn20 » Tue Aug 24, 2021 7:32 am

mhepp63 wrote:
Mon Aug 23, 2021 11:30 am
Glenn20, thanks it helped. I must read the doc better for the next time...
Excellent - glad to hear it is working. I only added the section on sleep modes to the espnow docs a few days ago - so it's understandable you missed those ;). And it is quite difficult finding the right info from the Espressif docs and relating that to the micropython docs.

I have found the interaction point between the espnow module and other esp32 and micropython functonality (esp. wifi, sleep and uasyncio) the most challenging to document (hence the ever growing size of the micropython espnow docs). The info on sleep modes and AP/STA should really be documented in the micropython esp32 wifi docs as the issues are just as relevent to wifi as they are to 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 » Sat Aug 28, 2021 7:00 am

ESPNow link or repeater

I am trying to setup a ESP32 to receive a message from a remote device and
then transmit that message to a local device.

The sending side I think is sorted because one can specify a particular
peer address. But, on the receive side I don't see that you can specify a
particular peer address.

To switch back and forth from receive and transmit, at the link or repeater,
I could:

Code: Select all

#  RX-mode
    ESPNow.add_peer(mac, param=value, ...)   (ESP32 only)
    msg = e0.irecv()
    ESPNow.del_peer(mac)
#  TX-mode
    ESPNow.add_peer(mac, param=value, ...)   (ESP32 only)
    ESPNow.send(mac, msg[, sync=True])
    ESPNow.del_peer(mac)
    
    repeat
Appreciate any hints regarding the proper way to do this.

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 » Sat Aug 28, 2021 8:45 am

davef wrote:
Sat Aug 28, 2021 7:00 am
ESPNow link or repeater

I am trying to setup a ESP32 to receive a message from a remote device and
then transmit that message to a local device.
As you probably know, you only need to register peers if you want to send to them (or receive encrypted messages from them). Also, you can have many peers registered so your should not need to del_peer(). Unless I have completely misunderstood your use case ;).

So, on the repeater you could:

Code: Select all

#  RX-mode
    local=b'local_mac'
    remote=b'remote_mac'
    e = espnow.ESPNow()
    e.init()
    e.add_peer(local)
    for mac, msg in e:
  	if mac == remote:
  	    e.send(local, msg[, sync=True])
  	else
  	    print('Recv from {}: "{}"'.format(mac, msg))
 
Last edited by glenn20 on Sat Aug 28, 2021 11:35 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 » Sat Aug 28, 2021 9:04 am

Hi glenn20,

Thanks for the prompt response.
As you probably know, you only need to register peers if you want to send to them (or receive encrypted messages from them).
I thought one had to register peers you want to send to and receive "normal" messages. That is why I was performing the switching.

I will study your code snippet more closely, however the point I was most confused about is ... I thought the MAC address of the transmitter was required before you do a irecv()?

I'll search for a hi-level document from Espressif as I really don't have a good "big picture" view.

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 28, 2021 9:13 am

Ah, I commented out add_peer in the RX and it still works.

Those last 5 lines in your snippet ... is there a receive function in there somewhere?

OK, found it:
Iteration over ESPNow
A new concept to me.

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 28, 2021 1:04 pm

davef wrote:
Sat Aug 28, 2021 9:04 am
Hi glenn20,

Thanks for the prompt response.
As you probably know, you only need to register peers if you want to send to them (or receive encrypted messages from them).
I thought one had to register peers you want to send to and receive "normal" messages. That is why I was performing the switching.

I will study your code snippet more closely, however the point I was most confused about is ... I thought the MAC address of the transmitter was required before you do a irecv()?

I'll search for a hi-level document from Espressif as I really don't have a good "big picture" view.
I have read a lot of docs from Espressif - and the networking and sleep mode docs from mcropython - but I had so many misunderstandings from reading the docs that were only clarified by testing, testing, trying and failing (and also from the questions raised here in the forum - that led me to test some more). In many cases the espressif docs are correct - and they seem obvious once you know what the correct behaviour is - but on first reading they can be open to interpretation. Initially I misunderstood a lot about epsnow because I didn't completely understand wifi on the esp32/8266 (and the interactions between the STA_IF and AP_IF). I've tried to clarify a lot of these learnings in my espnow docs - but it is a work in progress.

However, I can confirm that you don't need to register a peer to receive messages from it. I rely on this for my network of devices at home. When I deploy my software on a new espnow device it (simplified):
  • Announces itself with a specific "discovery" message on broadcast (unencrypted) asking for a service name (eg. "broker") and waits for responses for a few seconds
  • Any of my "broker" nodes that receive the message add the new device as an unencrypted peer and responds with an "Announce" response.
    • Then my broker nodes delete the peer and re-add it as an encrypted peer with pmk and lmk set to shared secrets for my services/apps (encrypted peer).
  • For each responding candidate "broker" the new device does:
    • adds the candidate broker mac as a peer with the pmk and lmk set to shared secrets for my services/apps (encrypted peer).
    • Sends a "Challenge" message to the candidate with a generated random number "secret" as the argument
    • The candidate broker receives the message and "secret" (after it has been unencrypted by espnow) and sends it back to the new device as a "Challenge_response" message (sent over encrypted espnow).
    • The new device checks that the argument in the "Challenge_response" message matches the random "secret"
    • Now the new device and the candidate "broker" know that each other possess the same app/service shared secret pmk and lmk - and can trust each other,
  • The new device then picks its preferred "broker" device (by asking each to turn on their AP_IF for a few seconds and performing a "scan" to identify the broker with the best RSSI.
  • The new device then saves it's preferred broker and all other brokers in the ESP32 NVS (non-volatile-storage) or on the device filesystem - and just tries those devices when it reboots and only does a rediscovery again if it can't connect to an existing device.
It is still a work in progress - but hope to make it available soon. The "broker" (or other "service" devices) must be ESP32s (to receive a broadcast message) but the client devices can be esp32 or ESP8266.

Currently my "broker" devices act as proxies to send/relay messages from my scattered devices to/from an mqtt proxy.

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 28, 2021 7:51 pm

Hi glenn20,

I now understand that you can find out who sent the message, thanks for the example.

To clarify ... I know what iteration is but didn't know what "iterating over the ESPNow singleton object" was.

So much to learn!

Post Reply