ESP32 logger wifi issues

My ESP32 based logger is showing problems displaying results on a web page, or connecting via FTP.
After reading this topic

I suspect the wifi is not being handled correctly, and the WiFI stack is getting corrupted.
Following a reset it works fine often for several weeks., then I get "The server at 192.168.1.34 is taking too long to respond."

Here are the most relevant bits of code:

#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ESP8266FtpServer.h>

#include "SPIFFS.h"
#include "time.h"
#include "sl2.h"

void setup() {
  Serial.begin(115200);
 
  WiFi.disconnect(true);
  //added in V2.18 to set a hostname
  WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
  WiFi.setHostname(hostname.c_str()); //define hostname
  delay(200);

  WiFi.onEvent(WiFiStationConnected, SYSTEM_EVENT_STA_CONNECTED);
  WiFi.onEvent(WiFiGotIP, SYSTEM_EVENT_STA_GOT_IP);
  WiFi.onEvent(WiFiStationDisconnected, SYSTEM_EVENT_STA_DISCONNECTED);

  initWiFi();
...  


//wifi handling
// event handlers from https://techtutorialsx.com/2019/09/22/esp32-soft-ap-station-disconnected-event/
void initWiFi() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("initWiFi: Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  //Serial.println(WiFi.localIP());
}
...

void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info) {
  Serial.println("Connected to AP successfully!");
  WiFiState = 1;
  digitalWrite(LED_WiFi, WiFiState); //indicate wifi connected -  BLUE LED
}

void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
  Serial.println("WiFi connected - hostname & IP address: ");
  Serial.print(WiFi.getHostname());
  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(info.disconnected.reason);
  Serial.println("Trying to Reconnect");
  WiFiState = 0;
  digitalWrite(LED_WiFi, WiFiState); //indicate wifi not connected -  BLUE LED
  WiFi.begin(ssid, password);
}

If I'm right the sketch needs a WiFi.disconnect(true); - to reset the stack; should I include that in the WiFiStationDisconnected() routine?
Also the program does housekeeping daily at midnight - should I also call the WiFiStationDisconnected() routine at that time?

My workings is with ESP32-CAM and ESP-WROOM-32, not with the 8266.

I mostly use canned code and it does what I need.

This being said, I always program these devices with a Static IP Address like 192.168.1.88

Would you creating a Static address like this be applicable :confused: ?

Hi Larry, yes I assign fixed IP addresses to the MAC addresses in my router as I've foiund setting fixed addresses in the arduino code isnt always successful.

This does use the ESP-WROOM-32 (- the ESP8266FTP library works with both.)

How I do the disconnect() thingy.

void connectToWiFi()
{
  int TryCount = 0;
  while ( WiFi.status() != WL_CONNECTED )
  {
    TryCount++;
    WiFi.disconnect();
    WiFi.begin( SSID, PASSWORD );
    vTaskDelay( 4000 );
    if ( TryCount == 10 )
    {
      ESP.restart();
    }
  }
  WiFi.onEvent( WiFiEvent );
}

Thanks @Idahowalker I saw that but wasnt sure how to implement it on my sketch as yours is using RTOS. vTaskDelay( 4000 );

However I've included the WiFi.disconnect(); in the InitWifi() and WiFiStationDisconnected() routines, as above; and a daily reconnect after midnight.

I havent included the ESP.restart as yet - not sure if that would affect my stored data, so lets see if it works without it first.

void initWiFi() {
 WiFi.disconnect(true);
 WiFi.mode(WIFI_STA);
 WiFi.begin(ssid, password);
 Serial.print("initWiFi: Connecting to WiFi ..");
 while (WiFi.status() != WL_CONNECTED) {
   Serial.print('.');
   delay(1000);
 }
 //Serial.println(WiFi.localIP());
}


void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info) {
 Serial.println("Connected to AP successfully!");
 WiFiState = 1;
 digitalWrite(LED_WiFi, WiFiState); //indicate wifi connected -  BLUE LED
}

void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
 Serial.println("WiFi connected - hostname & IP address: ");
 Serial.print(WiFi.getHostname());
 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(info.disconnected.reason);
 // added in V2.20:  call disconnect before reconnecting to reset wifi stack
 
 WiFi.disconnect(true);
 Serial.println("Trying to Reconnect");
 WiFiState = 0;
 digitalWrite(LED_WiFi, WiFiState); //indicate wifi not connected -  BLUE LED
 WiFi.begin(ssid, password);
}

Its early days as yet, but so far the web page connects instantly and the response to FTP is much better. I'll update again in a few weeks, especially if problems recur.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.