Replacing WiFi.disconnect() with esp_wifi_disconnect() produces the expected result i.e. WiFi.status() returns disconnected state and I get a corresponding onEvent() call. I've tried this with two different routers to try and rule out external factors. I have copied the serial output with verbose debug level to show that the core library is indeed behaving differently between the two disconnect functions.
Really appreciate any insight on this as it has had me occupied for way longer than I'd hoped!
Code: Select all
#include <WiFi.h>
extern "C" {
#include <esp_wifi.h>
}
void WiFiEventHandler(WiFiEvent_t event) { Serial.printf("Got Event: %d\n", event); }
void setup() {
Serial.begin(115200);
Serial.println("CONNECTING...");
WiFi.onEvent(WiFiEventHandler);
WiFi.begin("ssid", "password");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println(WiFi.status() == WL_CONNECTED ? "CONNECTED" : "FAILED");
Serial.println("DISCONNECTING...");
//esp_wifi_disconnect(); // This works
WiFi.disconnect(); // This doesnt
while (WiFi.status() == WL_CONNECTED) {
delay(500);
}
Serial.println(WiFi.status() != WL_CONNECTED ? "DISCONNECTED" : "FAILED");
}
void loop() {}
Code: Select all
CONNECTING...
[D][WiFiGeneric.cpp:182] _eventCallback(): Event: 2 - STA_START
Got Event: 2
[D][WiFiGeneric.cpp:182] _eventCallback(): Event: 4 - STA_CONNECTED
Got Event: 4
[D][WiFiGeneric.cpp:182] _eventCallback(): Event: 7 - STA_GOT_IP
Got Event: 7
CONNECTED
DISCONNECTING...
Code: Select all
CONNECTING...
[D][WiFiGeneric.cpp:182] _eventCallback(): Event: 2 - STA_START
Got Event: 2
[D][WiFiGeneric.cpp:182] _eventCallback(): Event: 4 - STA_CONNECTED
Got Event: 4
[D][WiFiGeneric.cpp:182] _eventCallback(): Event: 7 - STA_GOT_IP
Got Event: 7
CONNECTED
DISCONNECTING...
[D][WiFiGeneric.cpp:182] _eventCallback(): Event: 5 - STA_DISCONNECTED
[W][WiFiGeneric.cpp:187] _eventCallback(): Reason: 8 - ASSOC_LEAVE
Got Event: 5
DISCONNECTED