arduino-esp32 icon indicating copy to clipboard operation
arduino-esp32 copied to clipboard

Changing channel breaks ESP-Now Master/Slave example

Open skrenes opened this issue 6 years ago • 22 comments

Hardware:

Board: ESP32 D32 v1.0.0 Core Installation version: 1.0.1 IDE name: Arduino IDE Flash Frequency: 80Mhz PSRAM enabled: ? Upload Speed: 921600 Computer OS: Mac OSX 10.14.4

Description:

In the basic example of ESP-Now (and I believe Multi-Slave example too), if the user changes the CHANNEL definitions from 1 to anything else, the example fails.

The definitions are here:

https://github.com/espressif/arduino-esp32/blob/da8b7c1b80f1375be564674565c609aaa054e41c/libraries/ESP32/examples/ESPNow/Basic/Slave/Slave.ino#L35

https://github.com/espressif/arduino-esp32/blob/da8b7c1b80f1375be564674565c609aaa054e41c/libraries/ESP32/examples/ESPNow/Basic/Master/Master.ino#L37

An example of the output from the master is this:

Found 42 devices 
Found a Slave.
1: Slave:30:AE:A4:8B:38:10 [30:AE:A4:8B:38:11] (-32)
Slave Found, processing..
Slave Status: Already Paired
Sending: 2
Send Status: Success
Last Packet Sent to: 30:ae:a4:8b:38:11
Last Packet Send Status: Delivery Fail

Notice the Delivery Fail status. The slave doesn't detect the master at all. Using a channel scan tool on my laptop, I see that the slave is in fact broadcasting its softAP on the properly defined channel. I don't know enough about the underlying SDK to determine what's going on within a reasonable amount of time. I'm hoping someone else can figure out what's going on, especially given how easy it is to replicate.

skrenes avatar Apr 10 '19 22:04 skrenes

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 01 '19 00:08 stale[bot]

This stale issue has been automatically closed. Thank you for your contributions.

stale[bot] avatar Aug 15 '19 00:08 stale[bot]

Can someone comment on why this occurs?

devSarry avatar Nov 28 '19 08:11 devSarry

Support seems none-existent.

skrenes avatar Dec 02 '19 03:12 skrenes

was this solved?

sheminasalam avatar Jun 25 '22 16:06 sheminasalam

This needs to be checked, reopening.

VojtechBartoska avatar Jun 27 '22 11:06 VojtechBartoska

Can you please take a look @me-no-dev?

VojtechBartoska avatar Jun 27 '22 11:06 VojtechBartoska

@VojtechBartoska I'm not sure what the problem was. it seems like OP tried to set one channel for master and another for slave and expected the code to work. Should be closed IMHO

me-no-dev avatar Jun 27 '22 13:06 me-no-dev

is anyone able to retest this on v2.0.3? Thanks in advance!

VojtechBartoska avatar Jun 28 '22 09:06 VojtechBartoska

I had a similar situation with ESP8266. On investigation I found that even when we set channel as anything other than 1 while adding peers, the device doesn't execute that. The solution that I found with esp8266 is that we need to first set the channel with wifi.begin and then disconnect wifi with wifi.disconnect and proceed with adding peer. In Code it would look like below.

    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password, WIFI_CHANNEL,NULL,false);
    WiFi.disconnect(); //we do not want to connect to a WiFi network



    if (esp_now_init() != 0) {
      if (DEBUG_MODE == 1) {
        Serial.println("ESP-NOW initialization failed");
      }
      return;
    }

sheminasalam avatar Jun 28 '22 09:06 sheminasalam

Hi,

is anyone able to retest this on v2.0.3? Thanks in advance!

@VojtechBartoska Same on my side on v2.0.4 (not tested before)

I'm not sure what the problem was. it seems like OP tried to set one channel for master and another for slave and expected the code to work. Should be closed IMHO

Indeed if we leave the default channel values in sketches (master=3 and slave=1) we can't expect the code to work ! In this case we got an explicit message in master output :

Found 7 devices
Found a Slave.
1: Slave_1 [XX:XX:XX:XX:XX:XX] (-28)
Slave Found, processing..
Slave Status: Already Paired
Sending: 27
E (205350) ESPNOW: Peer channel is not equal to the home channel, send fail!
Send Status: Invalid Argument

But if I set the same channel for slave and master, it not work better and I got this output

Found 7 devices
Found a Slave.
1: Slave_1 [XX:XX:XX:XX:XX:XX] (-28)
Slave Found, processing..
Slave Status: Already Paired
Sending: 4
Send Status: Success
Last Packet Sent to: xx:xx:xx:xx:xx:xx
Last Packet Send Status: Delivery Fail

On the slave side I don't have any output after init :

ESPNow/Basic/Slave Example
AP Config Success. Broadcasting with AP: Slave_1
AP MAC: XX:XX:XX:XX:XX:XX
ESPNow Init Success

I tried a simpler way to get working ESPNow: 2 ESPs in STA mode and hardcoding the peer address on sender side but without success.

My setup: Win10 / Arduino 1.8.19 / arduino-esp32 2.0.4 Master: Lolin C3 mini (ESP32-C3FH4) / Slave: Lilygo ESP32_S2_WOOR (ESP32-S2-WROOM)

reppad avatar Jul 20 '22 13:07 reppad

Adding this to our Roadmap as it needs further testing.

VojtechBartoska avatar Jul 26 '22 13:07 VojtechBartoska

Hi, I have opened PR to fix the small problem with the channel, but I could not replicate your issue with Delivery Fail Tested on ESP32 DevKit. Can you please provide more info to reproduce this issue on my side?

PilnyTomas avatar Aug 09 '22 13:08 PilnyTomas

Of course,

Here is the basic informations :

My setup: Win10 Arduino 1.8.19 arduino-esp32 2.0.4 Master: Lolin C3 mini (ESP32-C3FH4) Slave: Lilygo ESP32_S2_WOOR (ESP32-S2-WROOM)

If it works on your side with 2 ESP32, maybe the problem is related to ESP32-S2 or C3, or an incompatibility between the two? What information can I provide to help you?

reppad avatar Aug 09 '22 14:08 reppad

Thanks for the info. I have tried using the same chips as you (different boards) - Master C3; Slave S2 - and it is working for me ok. Can you please try compiling it with a verbose debug level? Maybe changing both channels to different number might do something.

PilnyTomas avatar Aug 10 '22 07:08 PilnyTomas

Hi,

Some news after several tests.

ESPNow does not work with the Lolin C3 Mini board specifically, I was able to get it to work successfully on another ESP32-C3 based board with native USB. It may be related to the fact that the Lolin C3 Mini uses an ESP32-C3FH4 chip with built-in flash memory ? The working board uses an external SPI flash chip.

Here are the "verbose" logs of the Lolin C3 Mini running "Master" sketch

[   249][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino EventESPNow/Basic/Master Example
STA MAC: 60:55:F9:29:FE:FC
ESPNow Init Success
[  3112][V][WiFiGeneric.cpp:381] _arduino_event_cb(): SCAN Done: ID: 128, Status: 0, Results: 6
[  3113][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 1 - SCAN_DONE

Found 6 devices 
Found a Slave.
1: Slave_1 [7C:DF:A1:02:DD:8B] (-31)
Slave Found, processing..
Slave Status: Pair success
Sending: 1
Send Status: Success
Last Packet Sent to: 7c:df:a1:02:dd:8b
Last Packet Send Status: Delivery Fail
[ 11041][V][WiFiGeneric.cpp:381] _arduino_event_cb(): SCAN Done: ID: 129, Status: 0, Results: 4
[ 11042][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 1 - SCAN_DONE

Found 4 devices 
Found a Slave.
1: Slave_1 [7C:DF:A1:02:DD:8B] (-33)
Slave Found, processing..
Slave Status: Already Paired
Sending: 2
Send Status: Success
Last Packet Sent to: 7c:df:a1:02:dd:8b
Last Packet Send Status: Delivery Fail

On the slave side, nothing happens.

On the other hand I detected another problem, even with the board for which ESPNow works (by re-reading I think this is the problem initially reported by @skrenes):

If I use a channel other than 1 (same value on the master and slave side of course), the communication does not work and we get the behavior below

[   247][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino EventESPNow/Basic/Master Example
STA MAC: 60:55:F9:79:A4:5C
ESPNow Init Success

Found 10 devices 
Found a Slave.
1: Slave_1 [7C:DF:A1:02:DD:8B] (-47)
Slave Found, processing..
Slave Status: Pair success
Sending: 1
Send Status: Invalid Argument

Fou[ 20394][V][WiFiGeneric.cpp:381] _arduino_event_cb(): SCAN Done: ID: 130, Status: 0, Results: 12
[ 20394][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 1 - SCAN_DONE

Found 12 devices 
Found a Slave.
1: Slave_1 [7C:DF:A1:02:DD:8B] (-33)
Slave Found, processing..
Slave Status: Already Paired
Sending: 2
E (20481) ESPNOW: Peer channel is not equal to the home channel, send fail!
Send Status: Invalid Argument

reppad avatar Aug 14 '22 21:08 reppad

There is definitely a hardware design issue with the board I used for my 1st test : https://www.wemos.cc/en/latest/tutorials/c3/get_started_with_arduino_c3.html#wifi https://github.com/arendst/Tasmota/discussions/15443

So the issue is only about using channel other than 1 :

If I use a channel other than 1 (same value on the master and slave side of course), the communication does not work and we get the behavior below

[   247][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino EventESPNow/Basic/Master Example
STA MAC: 60:55:F9:79:A4:5C
ESPNow Init Success

Found 10 devices 
Found a Slave.
1: Slave_1 [7C:DF:A1:02:DD:8B] (-47)
Slave Found, processing..
Slave Status: Pair success
Sending: 1
Send Status: Invalid Argument

Fou[ 20394][V][WiFiGeneric.cpp:381] _arduino_event_cb(): SCAN Done: ID: 130, Status: 0, Results: 12
[ 20394][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 1 - SCAN_DONE

Found 12 devices 
Found a Slave.
1: Slave_1 [7C:DF:A1:02:DD:8B] (-33)
Slave Found, processing..
Slave Status: Already Paired
Sending: 2
E (20481) ESPNOW: Peer channel is not equal to the home channel, send fail!
Send Status: Invalid Argument

reppad avatar Aug 17 '22 22:08 reppad

Hi, it seems that the WiFi library does not allow changing channel for Station, so it is impossible to use any other channel than 1. BTW could please explain why you need other channels? It might be possible to add this ability later, but I'm not promising anything... In ESP-IDF example it is possible to change the channel in menuconfig, however, this results in the same error as for you: ESPNOW: Peer channel is not equal to the home channel, send fail! I will create an issue for IDF example nad hopefully we will hear more from the IDF team.

PilnyTomas avatar Aug 19 '22 05:08 PilnyTomas

Hi, it seems that the WiFi library does not allow changing channel for Station, so it is impossible to use any other channel than 1.

OK, that explains the behavior.

BTW could please explain why you need other channels?

In my case, I tried to change to use a channel as less congested as possible. But honestly it's not critical and if there hadn't been a need to modify the default channel to make the sketch work, I probably wouldn't have tried to configure it!

It might be possible to add this ability later, but I'm not promising anything...

That would be great, in the meantime it might be helpful to put a comment on the CHANNEL constant?

Thanks a lot for the help!

reppad avatar Aug 19 '22 09:08 reppad

put a comment on the CHANNEL constant

I will just remove the option to change CHANNEL until the real issue is solved (if ever).

PilnyTomas avatar Aug 19 '22 09:08 PilnyTomas

I had a similar situation with ESP8266. On investigation I found that even when we set channel as anything other than 1 while adding peers, the device doesn't execute that. The solution that I found with esp8266 is that we need to first set the channel with wifi.begin and then disconnect wifi with wifi.disconnect and proceed with adding peer. In Code it would look like below.

    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password, WIFI_CHANNEL,NULL,false);
    WiFi.disconnect(); //we do not want to connect to a WiFi network



    if (esp_now_init() != 0) {
      if (DEBUG_MODE == 1) {
        Serial.println("ESP-NOW initialization failed");     
      }
      return;
    }
    else // Add peer with correct channel

This worked for me. I needed to change the channel because one of the boards I am using is connected to a WiFi router that changes channels when it sees fit, and thus all the esp_now devices have to jump channels to the one the router is using.

juantinazzo avatar Jan 12 '23 12:01 juantinazzo

Hi there ! Is this issue still running ?

As [juantinazzo] said the problem is when you need to connect one device (at least) to router, in order to communicate out of local network.

Even if [juantinazzo] workaround works, all ESP-Now devices have to get connected on the same router to synchronise their operating channel, but they often can't be in router's range ....

laverdanny avatar Apr 04 '23 15:04 laverdanny

@P-R-O-C-H-Y As you worked on ESP-Now recently, can you please take a look on this issue?

VojtechBartoska avatar Apr 03 '24 11:04 VojtechBartoska

This issue can be closed as solved by the ESP-NOW library PR #9395. Channel of the ESP-NOW peer can be specified, but must be same with the Wifi Interface channel.

P-R-O-C-H-Y avatar Apr 03 '24 13:04 P-R-O-C-H-Y