Description
Hardware:
Board: LOLIN32
Core Installation version: Latest commit (4846d9c)
IDE name: Arduino IDE / VSCode
Flash Frequency: 80Mhz
PSRAM enabled: no
Upload Speed: 921600
Computer OS: Win10-x64
Description:
An old piece of code that worked for some time stopped working after updating core to latest commit (4846d9c). More specifically calls to WiFi.scanNetworks() failed with messages like this in the serial:
E (8376) wifi: esp_wifi_scan_start 1245 wifi not start
-2
E (9563) wifi: esp_wifi_scan_start 1245 wifi not start
-2
By git bisecting, I found the last core version that my code works to be commit e506136. Code stops working in commit 0cdfb0b.
By slimming down my code, this is a simple sketch to reproduce the issue:
#include <WiFi.h>
#include <HTTPClient.h>
void setup() {
Serial.begin(115200);
WiFi.persistent(false);
WiFi.setAutoConnect(false);
WiFiCheck();
}
void loop() {
boolean trigger = false;
while (Serial.available() > 0) {
String temp = String(Serial.read());
trigger = true;
}
if (trigger) {
Serial.println(WiFi.scanNetworks());
}
}
void WiFiCheck() {
WiFi.mode(WIFI_STA);
if (!connectToAP()) return;
getPayloadFromServer();
WiFi.disconnect(true);
}
boolean connectToAP() {
Serial.printf("Attempting AP connection (10 sec timeout).\n");
WiFi.begin("YourSSID", "YourPass");
for (uint8_t i = 0; i < 10; i++) {
if (WiFi.status() == WL_CONNECTED) break;
delay(1000);
}
return (WiFi.status() == WL_CONNECTED);
}
void getPayloadFromServer() {
HTTPClient http;
Serial.printf("Sending HTTP GET request to remote server.\n");
http.begin("http://ip-api.com/json/");
if (http.GET() == HTTP_CODE_OK) {
Serial.printf("Server responded with requested payload: %s\n", http.getString().c_str());
} else {
Serial.printf("Server could not be reached or server did not reply to GET request with a valid payload.\n");
}
http.end();
}
It initially attempts to send a HTTP GET request to a server and get the payload (successfully). After that it calls WiFi.disconnect(true) to disconnect and set WiFi mode to WIFI_OFF. It then waits for anything in the Serial to call WiFi.scanNetworks() and output the number of found networks.
That call fails in recent core commits (after 0cdfb0b) and succeeds in older ones, hence the change in behavior.
Now, one could argue that this is a fix and not an issue, because WiFi is off after disconnect(true), so it's normal for scanNetworks() to fail. But it seems that scanNetworks() fails even if you try to change the WiFi mode to WIFI_STA again, which should not happen.
Just replace the loop() above with this one:
void loop() {
boolean trigger = false;
while (Serial.available() > 0) {
String temp = String(Serial.read());
trigger = true;
}
if (trigger) {
WiFi.mode(WIFI_STA);
Serial.println(WiFi.scanNetworks());
WiFi.mode(WIFI_OFF);
}
}
This still fails.
I also tried WiFi.mode(WIFI_MODE_STA) and WiFi.enableSTA(true) instead of WiFi.mode(WIFI_STA) but WiFi.scanNetworks() always fails with the above message in the Serial.
Now, until that time, I thought this issue was specific to WiFi.scanNetworks(). Then I replaced it in the loop() with a call to WiFiCheck() - the same function that is called successfully in setup().
void loop() {
boolean trigger = false;
while (Serial.available() > 0) {
String temp = String(Serial.read());
trigger = true;
}
if (trigger) {
WiFiCheck();
}
}
These calls to WiFiCheck() fail.
It seems that after the initial disconnect(true), there is no way to change the WiFi mode again.