WiFi connection with 6.5.0 / 2.0.14

I just upgraded my PlatformIO configuration to 6.5.0 / Arduino Core 2.0.14 and after compilation and uploading of a previouly working application, felt into a huge problem of WiFi connection.
After two days of investigation, I finally found that during WiFi connection, the event handler received an SYSTEM_EVENT_STA_DISCONNECTED event before the SYSTEM_EVENT_STA_GOT_IP event, causing an infinite loop of connection/reconnection in my software. I put a simple workaround, trying to reconnect only after checking that WiFi was really disconnected, which should be the case when this event is triggered, but obviously is not with this new release.
Furthermore, even with this workaround, the initial WiFi connection takes about 5s from powerON, and it takes more than 20s after subsequent reset(s).

Things change with newer versions…

If you use WiFi.onEvent(), SYSTEM_EVENT_xxx is incorrect. You must use ARDUINO_EVENT_WIFI_xxx instead. Please see documentation: Wi-Fi API - - — Arduino ESP32 latest documentation

Glad you’ve found a way that works for you!

I can’t confirm that. My connections need between 180 and 250 ms for both a start and a restart.

Here’s my test project:

#include <Arduino.h>
#include <WiFi.h>

const char* wifi_ssid = "";
const char* wifi_pass = "";

unsigned long wifiBeginConnecting = 0;

bool wifiWasConnected = false;
bool wifiIsConnected = false;

void setupWiFi() {
    WiFi.onEvent([](arduino_event_id_t event){
        if (!wifiWasConnected) Serial.println("WiFi: Disconnected!");
        wifiIsConnected = false;
    }, ARDUINO_EVENT_WIFI_STA_DISCONNECTED);

    WiFi.onEvent([](arduino_event_id_t event){
        wifiWasConnected = true;
        wifiIsConnected = true;
        Serial.println("WiFi connected!");
        unsigned long wifiConnectingDuration = millis() - wifiBeginConnecting;
        Serial.printf("WiFi connection took %lu ms\r\n", wifiConnectingDuration);
    }, ARDUINO_EVENT_WIFI_STA_GOT_IP);

    wifiBeginConnecting = millis();
    WiFi.begin(wifi_ssid, wifi_pass);
}

void setup() {
    Serial.begin(115200);
    setupWiFi();
}

void loop() {
}

Thanks for answering. Fortunatelly SYSTEM_EVENT_xxx constants are exactly the same than ARDUINO_EVENT_xxx (in our case SYSTEM_EVENT_STA_GOT_IP = 7 and SYSTEM_EVENT_STA_DISCONNECTED = 5 just as ARDUINO_EVENT_ equivalent). So the problem is not there. My test project is similar to yours:

#include <Arduino.h>
#include <WiFi.h>
#include "Arduino_DebugUtils.h"

static TimerHandle_t wifiReconnectTimer;      // Reconnect timer for WiFi

void connectToWiFi(){
  Debug.print(DBG_INFO,"[WiFi] Connecting to WiFi...");
  WiFi.mode(WIFI_STA);
  WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
  WiFi.setHostname("WIFI_TEST"); 
  WiFi.begin("SSID", "PWD");
}

void reconnectToWiFi(){
  if(!WL_CONNECTED) {
    Debug.print(DBG_INFO,"[WiFi] Reconnecting to WiFi...");
    WiFi.reconnect();
    while(WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(":");
    }
  } else Debug.print(DBG_INFO,"[WiFi] Not reconnecting...");  
}

void WiFiEvent(WiFiEvent_t event){
  switch(event){
    case ARDUINO_EVENT_WIFI_STA_CONNECTED:
      Debug.print(DBG_INFO,"[WiFi] Connected to: %s",WiFi.SSID().c_str());
      break;
    case ARDUINO_EVENT_WIFI_STA_GOT_IP:
      Debug.print(DBG_INFO,"[WiFi] IP address: %s",WiFi.localIP().toString().c_str());
      Debug.print(DBG_INFO,"[WiFi] Hostname: %s",WiFi.getHostname());
      break;
    case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
      Debug.print(DBG_WARNING,"[WiFi] Disconnection");
      xTimerStart(wifiReconnectTimer,0);
      break;    
    default:
      break;  
  }
}

void setup() {

  Serial.begin(115200);
  Debug.setDebugLevel(DBG_DEBUG);
  Debug.timestampOn();

  wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(reconnectToWiFi));

  WiFi.onEvent(WiFiEvent);
  //WiFi.disconnect(true);

  connectToWiFi();

  delay(500);    // let task start-up and wait for connection
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  // Debug.print(DBG_INFO,"[WiFi] Connected to: %s",WiFi.SSID().c_str());
  // Debug.print(DBG_INFO,"[WiFi] IP address: %s",WiFi.localIP().toString().c_str());
  // Debug.print(DBG_INFO,"[WiFi] Hostname: %s",WiFi.getHostname());

  }
}

void loop() {
    if(WiFi.status() == WL_CONNECTED){
      Serial.print("RSSI: ");
      Serial.println(WiFi.RSSI());
      delay(1000);
    } else {
    }
}

And I confirm that a DISCONNECTED event is received just before GOT_IP, which results in reconnection attempt if not filtered by my workaround.

Correct, but there is no problem at all. Things have simply changed. With the next Espressif32 Arduino version 3.x, things will change even more. This time, however, Espressif seems to provide better documentation: Migration from 2.x to 3.0 - - — Arduino ESP32 latest documentation

Fortunately, it seems that there will be no changes to WiFi.onEvent.

This is also correct. If you use invalid WiFi credentials, you will see that the DISCONNECT event is triggered every 2421 ms.

OK, the spurious DISCONNECTED event being managed, there still remains the abnormally long connection time: with this simple test above, it takes around 20s to get IP. Here are the WIFI events timeline during connection:
46ms READY
116ms STA_START
21608ms STA_DISCONNECTED
21679ms STA_CONNECTED
21700ms STA_GOT_IP

With the previous version it took less than 5s.

Which version number was your previous version?

Here are my results (your sketch):

11:38:32.708 > ets Jun  8 2016 00:22:57
11:38:32.715 > 
11:38:32.715 > rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
11:38:32.720 > configsip: 0, SPIWP:0xee
11:38:32.720 > clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
11:38:32.726 > mode:DIO, clock div:2
11:38:32.732 > load:0x3fff0030,len:1184
11:38:32.732 > load:0x40078000,len:13232
11:38:32.738 > load:0x40080400,len:3028
11:38:32.738 > entry 0x400805e4
11:38:33.078 > [ 49 ] [WiFi] Connecting to WiFi...
11:38:33.242 > [ 214 ] [WiFi] Connected to: I-LINK
11:38:33.265 > [ 236 ] [WiFi] IP address: 192.168.2.123
11:38:33.271 > [ 236 ] [WiFi] Hostname: WIFI_TEST
11:38:33.675 > RSSI: -56
11:38:34.676 > RSSI: -62
11:38:35.677 > RSSI: -62
11:38:36.678 > RSSI: -63

Even with platform = espressif32@6.1.0 (Arduino 2.0.7):

12:16:19.196 > ets Jun  8 2016 00:22:57
12:16:19.201 >
12:16:19.201 > rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
12:16:19.208 > configsip: 0, SPIWP:0xee
12:16:19.208 > clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
12:16:19.214 > mode:DIO, clock div:2
12:16:19.219 > load:0x3fff0030,len:1184
12:16:19.219 > load:0x40078000,len:13192
12:16:19.225 > load:0x40080400,len:3028
12:16:19.225 > entry 0x400805e4
12:16:19.562 > [ 48 ] [WiFi] Connecting to WiFi...
12:16:19.797 > [ 284 ] [WiFi] Connected to: I-LINK
12:16:19.816 > [ 302 ] [WiFi] IP address: 192.168.2.123
12:16:19.822 > [ 302 ] [WiFi] Hostname: WIFI_TEST

Incredible!
I write down every time I update versions:
March 19th: espressif32@6.5.0 Arduino 2.0.14
Sept. 9th: espressif32@6.4.0 Arduino 2.0.11

Tried to go back to 6.4.0:

[ 47 ] WiFi event: 0
[ 143 ] WiFi event: 2
E (1784) wifi:Association refused temporarily, comeback time 299 mSec
E (2089) wifi:Association refused temporarily, comeback time 299 mSec 

after a while, sometimes it connects, sometimes not

Same with 6.3.0

I’m afraid that there are some incompatibilities…

Again 6.5.0:

--- Available filters and text transformations: colorize, debug, default, direct, esp32_exception_decoder, hexlify, log2file, nocontrol, 
printable, send_on_enter, time
--- More details at https://bit.ly/pio-monitor-filters
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:1184
load:0x40078000,len:13232
load:0x40080400,len:3028
entry 0x400805e4
[ 28 ] [WiFi] Connecting to WiFi...
[ 46 ] WiFi event: 0
[ 117 ] WiFi event: 2
.........................................[ 21554 ] WiFi event: 5
[ 21554 ] [WiFi] Disconnection
[ 21599 ] WiFi event: 4
[ 21600 ] [WiFi] Connected to: DENIS_HOME1
[ 21626 ] WiFi event: 7
[ 21626 ] [WiFi] IP address: 192.168.1.23
[ 21626 ] [WiFi] Hostname: WIFI_TEST
.RSSI: -66
RSSI: -64

Update to 6.6.0: no change

So I’m not even able to go back to a previous working version, too sad…

Did you try with a different Network?

Same idea at the same moment: I was indeed wondering if it was not coming from my network and tadah! Tried to connect to an extension of the main network and now less than one second. Quite surprising.
Thanks a lot to have shared with me.