espressif / ESP8266_NONOS_SDK

ESP8266 nonOS SDK
Other
925 stars 535 forks source link

ESP8266 stops receiving ESP-NOW broadcast messages after WiFi.scanNetworks () #334

Open gmag11 opened 3 years ago

gmag11 commented 3 years ago

I've been playing with esp-now for a long time. Until now I've always used unicast messages, but for a new project I'd like to use broadcast.

For testing, I've written a program that sends a broadcast message and a unicast message every 10 seconds from one esp8266. Another one is configured to print data over serial when a packet is received.

If I trigger a WiFi network scan it stops receiving broadcast messages but is still able to receive unicast messages. This stays so until I reboot ESP.

This is the output log:

Core version: 2_7_4
SDK version: 2.2.2-dev(a58da79)
W [2284][helperFunctions.cpp:57] initWiFi() Heap: 39152 | AP MAC address of this device is 5E:CF:7F:2C:18:09
W [2285][helperFunctions.cpp:58] initWiFi() Heap: 39152 | STA MAC address of this device is 5C:CF:7F:2C:18:09
Message from: b4:e6:2d:9d:ab:16 Hello Node broadcast
Message from: b4:e6:2d:9d:ab:16 Hello Node unicast
Message from: b4:e6:2d:9d:ab:16 Hello Node broadcast
Message from: b4:e6:2d:9d:ab:16 Hello Node unicast
Message from: b4:e6:2d:9d:ab:16 Hello Node broadcast
Message from: b4:e6:2d:9d:ab:16 Hello Node unicast

Start scan

 Broadast
 Monimar
 MOVISTAR_11F5
 EnigmaIOT
 Virus_Detected!!!
 skynet
 Orange-556E
 MOVISTAR_F54A_plus
 MOVISTAR_E670
 ONO68D5
 DIGIFIBRA-24-srfN
 Pantum-AP-CB06ED
 ONOF54E
 ONO2020
 MiFibra-8FD6

Scan complete

Message from: b4:e6:2d:9d:ab:16 Hello Node unicast
Message from: b4:e6:2d:9d:ab:16 Hello Node unicast
Message from: b4:e6:2d:9d:ab:16 Hello Node unicast
gmag11 commented 3 years ago

I can confirm this behabiour. But I've found a workaround. Turning WiFi to OFF mode and setting it to the previous mode seems to restart WiFi interface and restores ability to receive broadcast messages.

This means:

uint8 mode = wifi_get_opmode();
wifi_set_opmode(0);
wifi_set_opmode(mode);

If you are Arduino framework this code is equivalent:

WiFiMode_t mode = WiFi.getMode ();
WiFi.mode (WIFI_OFF);
WiFi.mode (mode);

I've tested that this works even after several consecutive network scans