wifiiotesp32connectivity

ESP32 event based reconnect to wifi on connection lost/disconnect?


Connectivity handling is a major part of firmware development in ESP32 and I've found event based reconnect to work well. Here is the code that you find very commonly everywhere, but it doesn't directly work -


#include <WiFi.h>
 
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println("Connected to AP successfully!");
}

void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println("Disconnected from WiFi access point");
  Serial.print("WiFi lost connection. Reason: ");
  Serial.println("Trying to Reconnect");
  WiFi.begin(ssid, password);
}

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

  // delete old config
  WiFi.disconnect(true);

  delay(1000);

  WiFi.onEvent(WiFiStationConnected, SYSTEM_EVENT_STA_CONNECTED);
  WiFi.onEvent(WiFiGotIP, SYSTEM_EVENT_STA_GOT_IP);
  WiFi.onEvent(WiFiStationDisconnected, SYSTEM_EVENT_STA_DISCONNECTED);
  WiFi.begin(ssid, password);
    
  Serial.println();
  Serial.println();
  Serial.println("Wait for WiFi... ");
}

void loop(){
  delay(1000);
}

Solution

  • It doesn't directly work because you are using the Arduino framework. The event constants are for ESP-IDF if I am not wrong.

    We need to use the following constants which are specific for Arduino:

    ARDUINO_EVENT_WIFI_STA_CONNECTED: ESP32 station connected to AP

    ARDUINO_EVENT_WIFI_STA_GOT_IP: ESP32 station got IP from connected AP

    ARDUINO_EVENT_WIFI_STA_DISCONNECTED: ESP32 station disconnected from AP

    So the correct working code would be:

    #include <WiFi.h>
     
    const char* ssid = "REPLACE_WITH_YOUR_SSID";
    const char* password = "REPLACE_WITH_YOUR_PASSWORD";
    
    void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info){
      Serial.println("Connected to AP successfully!");
    }
    
    void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info){
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
    }
    
    void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info){
      Serial.println("Disconnected from WiFi access point");
      Serial.print("WiFi lost connection. Reason: ");
      Serial.println("Trying to Reconnect");
      WiFi.begin(ssid, password);
    }
    
    void setup(){
      Serial.begin(115200);
    
      // delete old config
      WiFi.disconnect(true);
    
      delay(1000);
    
      WiFi.onEvent(WiFiStationConnected,ARDUINO_EVENT_WIFI_STA_CONNECTED);
      WiFi.onEvent(WiFiGotIP, ARDUINO_EVENT_WIFI_STA_GOT_IP);
      WiFi.onEvent(WiFiStationDisconnected, ARDUINO_EVENT_WIFI_STA_DISCONNECTED); 
      WiFi.begin(ssid, password);
        
      Serial.println();
      Serial.println();
      Serial.println("Wait for WiFi... ");
    }
    
    void loop(){
      delay(1000);
    }