esp8266 / esp8266/Arduino

Doing a Wifi scan makes ESP-NOW receiving unreliable afterwards

Open
#8,690 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
16.7k
Forks
13.1k
PR merge metrics
No merged PRs in 30d

Description

### Basic Infos

- [X] This issue complies with the [issue POLICY doc](https://github.com/esp8266/Arduino/blob/master/POLICY.md).
- [X] I have read the documentation at [readthedocs](https://arduino-esp8266.readthedocs.io/en/latest) and the issue is not addressed there.
- [ ] I have tested that the issue is present in current master branch (aka latest git).
- [X] I have searched the issue tracker for a similar issue.
- [ ] If there is a stack dump, I have decoded it.
- [X] I have filled out all fields below.

#### Platform

- Hardware: Any ESP8266, like ESP-12F or D1 mini
- Core Version: 3.0.2
- Development Env: The repro below is for Arduino IDE, issue happens in PlatformIO as well
- Operating System: Windows

### Settings in IDE

- Module: Generic ESP8266 Module
- Flash Mode: DOUT compatible
- Flash Size: 1MB
- lwip Variant: v2 Lower Memory
- Reset Method: nodemcu
- Flash Frequency: 40Mhz
- CPU Frequency: 80Mhz
- Upload Using: SERIAL
- Upload Speed: 115200

### Problem Description

To reproduce this, you need two ESP8266.

Load the first one with a sketch that continuously sends ESP-NOW broadcasts, like this one:

```cpp
#include
#include
#include
uint8_t broadcastAddress[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

void setup()
{
Serial.begin(115200);
WiFi.disconnect();
WiFi.mode(WIFI_STA);
wifi_set_channel(6);
esp_now_init();
delay(10);
esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
}

void loop()
{
Serial.printf("Channel: %u\n", wifi_get_channel());
int data = 1234;
esp_now_send(broadcastAddress, (uint8_t*)&data, sizeof(data));
delay(500);
}
```

Load the second one with a sketch that displays a text when an ESP-NOW message is received, like this one:

```cpp
#include
#include
#include

void on_espnow_receive(uint8_t *mac_addr, uint8_t *data, uint8_t len) {
Serial.printf("Received %u bytes from ", len);
for (uint8_t b = 0; b < 6 ; b++)
Serial.printf("%02hhX", mac_addr[b]);
Serial.println();
}

void on_scan_done(void *arg, STATUS status) {}

void setup() {
delay(3000);
Serial.begin(115200);
WiFi.disconnect();
WiFi.mode(WIFI_STA);

// struct scan_config config = {0};
// wifi_station_scan(&config, on_scan_done);

wifi_set_channel(6);
esp_now_init();
delay(10);
Serial.printf("Channel: %u\n", wifi_get_channel());
esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
esp_now_register_recv_cb(on_espnow_receive);
}

void loop() {
delay(1000);
}
```

If you now run the receiver, in the Serial Monitor you'll see many received messages, just as expected:
```
Received 4 bytes from ...
Received 4 bytes from ...
Received 4 bytes from ...
```

Now uncomment the two commented lines in the receiver sketch:
```
struct scan_config config = {0};
wifi_station_scan(&config, on_scan_done);
```
You can also call `WiFi.scanNetworks()` instead of `wifi_station_scan(&config, on_scan_done)`, it has the same effect.

If you then run the receiver again, you'll still get a message from time to time, but much much fewer, almost every message is lost.
```
Received 4 bytes from ...
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.