ESP-NOW: how to identify devices after boot?

YigalB
Posts: 10
Joined: Sun Mar 18, 2018 8:41 pm

ESP-NOW: how to identify devices after boot?

Postby YigalB » Wed Aug 17, 2022 6:24 am

As for now, I use hard coded MAC addresses of EWSP-NOW devices in my code, so every time a new device is added to the system, I need to recompile and burn few devices.

Is there a way to scan automatically all ESP-NOW devices in the nearest are, get from them a certain ID and MAC and configure them accordingly?

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP-NOW: how to identify devices after boot?

Postby ESP_Sprite » Thu Aug 18, 2022 1:19 am

You can, but I'd do it the other way: the new devices can send an ESP-NOW packet to the broadcast MAC (ff:ff:ff:ff:ff:ff) and the receiver can receive those and try to configure the device.

YigalB
Posts: 10
Joined: Sun Mar 18, 2018 8:41 pm

Re: ESP-NOW: how to identify devices after boot?

Postby YigalB » Thu Aug 18, 2022 3:49 am

ESP_Sprite wrote:
Thu Aug 18, 2022 1:19 am
You can
How can I do that?
, but I'd do it the other way: the new devices can send an ESP-NOW packet to the broadcast MAC (ff:ff:ff:ff:ff:ff) and the receiver can receive those and try to configure the device.
That is a nice idea, but it will fail in case the master device is going through a reset. After the master wakes up, the slaves will not re-send their ID.

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP-NOW: how to identify devices after boot?

Postby ESP_Sprite » Thu Aug 18, 2022 6:59 am

Obviously you'd need to take care of that by e.g. going back into broadcast mode on having a timeout. Alternatively, have the master send a broadcast and have all devices react.

On the 'how': off the top of my head (but admittedly it's been a while since I used this), you just add the broadcast address as a peer and send/receive to/from there.

YigalB
Posts: 10
Joined: Sun Mar 18, 2018 8:41 pm

Re: ESP-NOW: how to identify devices after boot?

Postby YigalB » Fri Aug 19, 2022 4:25 am

Can you share a code example of the master and slave for the case the master send "who is out there" and each slave respond?

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP-NOW: how to identify devices after boot?

Postby ESP_Sprite » Fri Aug 19, 2022 1:04 pm

No, as I don't have one.

OutOfLine
Posts: 52
Joined: Sat Feb 24, 2018 1:32 pm

Re: ESP-NOW: how to identify devices after boot?

Postby OutOfLine » Mon Aug 22, 2022 3:41 pm

Sorry I don't have example code neither, but I do have code of a project that uses a similar functionality to configure and play an 'orchestra' of esp32 'instruments', so you might find a couple of hints grepping through the code. But be warned: the code is long quite complicated, chaotic, experimental, higly configurable. I wrote (and used) that code for live performances and electronic art installations and certainly *not* as an example of good programming techniques. It might confuse you more than helping you. You *have* been warned...

A few points for better understanding: there is no master, there are no slaves, just participants.
I can take any of the instruments and give instructions to configure and play the thing or send them (using esp-now) to other instruments. Each instrument has a simple morse input (using a touch input), or the input can be given through serial input or bluetooth.

Each instrument has a name (and some other parameters) stored in nvs as an ID. I do *not* set them by the program, but on very first use with one of the above mentioned input methods using a serial menu system, that let's me change a lot of settings at run time.

All my ESP-NOW messages include a code for it's meaning. So each instrument can broadcast a message to tell all the others to broadcast their IDENTITY (including the name). Depending configuration this can be sent after booting, after finishing to play music, on request through the morse interface, etc. On receiving an ID it will be stored in the peer-list of the other instruments.
After that this peers are known to them, can be addressed individually (i.e. to start playing or configuring them). The list can be displayed including name, mac etc of all the instruments.
Most instruments have some sort of small display (like ePaper or OLED).

As you might imagine the esp-now system is deeply burried in many other code files.
Grep for 'USE_ESP_NOW' which is the main configuration switch to use it.
Compilation starts with 'pulses.ino'
Basic ESP-NOW functionality is in 'esp_now_pulses.h'

I *did* tell you it is chaotic, didn't I?

https://github.com/reppr/pulses/
Last edited by OutOfLine on Mon Aug 22, 2022 4:16 pm, edited 1 time in total.

YigalB
Posts: 10
Joined: Sun Mar 18, 2018 8:41 pm

Re: ESP-NOW: how to identify devices after boot?

Postby YigalB » Mon Aug 22, 2022 3:53 pm

You certainly said it, very clearly....
I just don't know where is that chaotic code.

OutOfLine
Posts: 52
Joined: Sat Feb 24, 2018 1:32 pm

Re: ESP-NOW: how to identify devices after boot?

Postby OutOfLine » Mon Aug 22, 2022 4:18 pm


Who is online

Users browsing this forum: No registered users and 52 guests