ESP32 stopped connecting to wi-fi

My project involves connecting to wlfl but it has suddenly stopped connecting. I have tried everything I can think of including running it two different esp32 dev kits run previous project versions that never had a problem connecting ect with no success. Most recently I have run a scanning program to check it can see the network (it can) ; pinned espressif32 to version 5 etc etc!

As a test I am currently using the programme below shown with compile and upload so that you can see the setup I am using.

I am desperate for a lead!

// Demo-Code connect an ESP32 to a WiFi-network using stationmode STA

// stationmode is simply join the WiFi-network as your computer or smartphone does it
// the code has three useful functions one for easy identifiying the code in the flash
// one for non-blocking timing
// a heartbeat-blinker function for indicating the state the code is in

// the code is written with two programming rules:
// 1. put EACH functionality into its OWN function
// 2. give EACH function a SELF-explaining name what the function does
// you should follow these programming rules
#include <WiFi.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PWMServoDriver.h>

const char *ssid = "yourssid";
const char *password = "yourpassword";

void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__));
  Serial.print( F("  compiled ") );
  Serial.print(F(__DATE__));
  Serial.print( F(" ") );
  Serial.println(F(__TIME__));
}

boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - periodStartTime >= TimePeriod )
  {
    periodStartTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  }
  else return false;            // not expired
}

unsigned long MyTestTimer = 0;                   // variables MUST be of type unsigned long
const byte    OnBoard_LED = 2;

uint8_t actualWiFiStatus;
uint8_t lastWiFiStatus;

void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);
  if ( TimePeriodIsOver(MyBlinkTimer, BlinkPeriod) ) {
    digitalWrite(IO_Pin, !digitalRead(IO_Pin) );
  }
}

void printWiFiStatus(uint8_t p_WiFiStatus) {
  switch (p_WiFiStatus) {
    
    case WL_IDLE_STATUS:
      Serial.println("WL_IDLE_STATUS temporary status assigned when WiFi.begin() is called");
      break;
      
    case WL_NO_SSID_AVAIL:
      Serial.println("WL_NO_SSID_AVAIL   when no SSID are available");
      break;
      
    case WL_SCAN_COMPLETED:
      Serial.println("WL_SCAN_COMPLETED scan networks is completed");
      break;
      
    case WL_CONNECTED:
      Serial.println("WL_CONNECTED  when connected to a WiFi network");
      break;
      
    case WL_CONNECT_FAILED:
      Serial.println("WL_CONNECT_FAILED when the connection fails for all the attempts");
      break;
      
    case WL_CONNECTION_LOST:
      Serial.println("WL_CONNECTION_LOST  when the connection is lost");
      break;
      
    case WL_DISCONNECTED:
      Serial.println("WL_DISCONNECTED when disconnected from a network");
      break;
  }
  Serial.println();
  Serial.println();
}

void ConnectToWiFi() {
  WiFi.mode(WIFI_STA);
  Serial.println("WiFi.mode(WIFI_STA)");
  int myCount = 0;
  Serial.print("trying to connect to #");
  Serial.print(ssid);
  Serial.println("#");
  WiFi.begin(ssid, password);
  Serial.println("WiFi.begin() done");
  actualWiFiStatus = WiFi.status();
  
  #define maxCount 120
  // Wait for connection
  while (actualWiFiStatus != WL_CONNECTED && myCount < maxCount) {
    actualWiFiStatus = WiFi.status();
    if (lastWiFiStatus != actualWiFiStatus) {
      Serial.println("WiFiStatus changed from");
      printWiFiStatus(lastWiFiStatus);
      Serial.println("to new status");
      printWiFiStatus(actualWiFiStatus);
      lastWiFiStatus = actualWiFiStatus;
    }

    BlinkHeartBeatLED(OnBoard_LED, 50); // blink LED fast during attempt to connect
    yield();
    if ( TimePeriodIsOver(MyTestTimer, 500) ) { // once every 500 miliseconds
      Serial.print(".");                        // print a dot
      myCount++;
      if (myCount > maxCount) { // after maxCount dots restart
        Serial.println();
        Serial.print("not yet connected executing ESP.restart();");
        ESP.restart();
      }
    }
  }
  
  if (WiFi.status() == WL_CONNECTED ) {
    Serial.println("");
    Serial.print("Connected to #");
    Serial.print(ssid);
    Serial.print("# IP address: ");
    Serial.println(WiFi.localIP());
  }
}

void setup() {
  Serial.begin(115200);
  Serial.println( F("Setup-Start") );
  PrintFileNameDateTime();
  ConnectToWiFi();
}

void PrintHelloMsg() {
  Serial.print( F("Hi there I'm the demo-code my IP address is: ") );
  Serial.println(WiFi.localIP());
}

void loop() {
  BlinkHeartBeatLED(OnBoard_LED, 500); // change blinking to a lower frequency indicating beeing connected
  if ( TimePeriodIsOver(MyTestTimer, 1000) ) {
    PrintHelloMsg();
  }
}
/*
  most ESP32 boards have a blue LED connected to GPIO-pin 2
  This blue LED is used to indicate state connecting to WiFi by blinking fast
  state beeing connected to Wifi by blinking with 1 Hz

  If the WiFi-connection is successfully established the serial monitor shows

  08:44:02.915 -> Setup-Start
  08:44:02.915 -> Code running comes from file
  08:44:02.915 -> your-path\ESP32-connect-to-Wifi-Demo-001\ESP32-connect-to-Wifi-Demo-001.ino
  08:44:02.915 ->   compiled date/time of compiling
  08:44:02.971 -> WiFi.mode(WIFI_STA)
  08:44:02.971 -> trying to connect to #Your SSID#
  08:44:03.362 -> ....
  08:44:04.215 -> Connected to #Your SSID# IP address: given IP-adress NNN.NNN.NNN.NNN
  08:44:04.865 -> Hi there I'm the demo-code my IP address is: NNN.NNN.NNN.NNN

  if there is something wrong with your WiFi, SSID or password
  you will see this in the serial monitor
  08:32:36.598 -> Setup-Start
  08:32:36.598 -> Code running comes from file
  08:32:36.598 -> F:\MyPortable-PRgs\arduino-1.8.16-newb\portable\sketchbook\ESP32-connect-to-Wifi-Demo-001\ESP32-connect-to-Wifi-Demo-001.ino
  08:32:36.598 ->   compiled Dec  1 2021 08:32:18
  08:32:36.684 -> WiFi.mode(WIFI_STA)
  08:32:36.684 -> trying to connect to #your SSID#
  08:32:37.036 -> ...............................
  08:32:52.035 -> not yet connected executing ESP.restart();ets Jun  8 2016 00:22:57
  08:32:52.035 ->
  08:32:52.035 -> rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
  08:32:52.035 -> configsip: 0, SPIWP:0xee
  08:32:52.035 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
  08:32:52.073 -> mode:DIO, clock div:1
  08:32:52.073 -> load:0x3fff0018,len:4
  08:32:52.073 -> load:0x3fff001c,len:1216
  08:32:52.073 -> ho 0 tail 12 room 4
  08:32:52.073 -> load:0x40078000,len:9720
  08:32:52.073 -> ho 0 tail 12 room 4
  08:32:52.073 -> load:0x40080400,len:6352
  08:32:52.073 -> entry 0x400806b8
  08:32:52.309 -> Setup-Start
  08:32:52.309 -> Code running comes from file
  08:32:52.309 -> F:\MyPortable-PRgs\arduino-1.8.16-newb\portable\sketchbook\ESP32-connect-to-Wifi-Demo-001\ESP32-connect-to-Wifi-Demo-001.ino
  08:32:52.309 ->   compiled Dec  1 2021 08:32:18
  08:32:52.405 -> WiFi.mode(WIFI_STA)
  08:32:52.405 -> trying to connect to #Your SSID#
  08:32:52.758 -> ...............................
  08:33:07.788 -> not yet connected executing ESP.restart();ets Jun  8 2016 00:22:57

*/

It compiles and uploads fine…

 *  Executing task in folder ESPSERVOs: platformio run --target upload --upload-port /dev/ttyACM0 
Processing esp32dev (platform: espressif32 @ ^5.0.0; board: esp32dev; framework: arduino)
-----------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32dev.html
PLATFORM: Espressif 32 (5.4.0) > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (cmsis-dap) External (cmsis-dap, esp-bridge, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES: 
 - framework-arduinoespressif32 @ 3.20006.221224 (2.0.6) 
 - tool-esptoolpy @ 1.40400.0 (4.4.0) 
 - tool-mkfatfs @ 2.0.1 
 - tool-mklittlefs @ 1.203.210628 (2.3) 
 - tool-mkspiffs @ 2.230.0 (2.30) 
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch5
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 37 compatible libraries
Scanning dependencies...
Dependency Graph
|-- Adafruit PWM Servo Driver Library @ 3.0.1
|-- Adafruit BusIO @ 1.14.4
|-- SPI @ 2.0.0
|-- WiFi @ 2.0.0
|-- Wire @ 2.0.0
Building in release mode
Compiling .pio/build/esp32dev/src/wifistatus.cpp.o
Building .pio/build/esp32dev/bootloader.bin
esptool.py v4.4
Creating esp32 image...
Merged 1 ELF section
Successfully created esp32 image.........

ECT ECT.....

It creates the following output to indicate that it has not been able to connect…

Code running comes from file 
src/wifistatus.cpp
  compiled Oct 12 2023 10:13:42
WiFi.mode(WIFI_STA)
trying to connect to #BT-THAJX6#
WiFi.begin() done
WiFiStatus changed from
WL_IDLE_STATUS temporary status assigned when WiFi.begin() is calledto new status
WL_DISCONNECTED when disconnected from a network

........................................................................................................................Hi there I'm the demo-code my IP address is: 0.0.0.0
Hi there I'm the demo-code my IP address is: 0.0.0.0 ....

and on and on!

  • I have since run the code on two different ESP32s and on a completely different router and wifi service all to no avail.*

Does it fail when using the Arduino IDE too? If yes, the problem is not PlatformIO specific and you could get help at https://github.com/espressif/arduino-esp32/issues.

Something I can generally recommend is to just trigger a flash erase with esptool.py and flash anew. https://randomnerdtutorials.com/esp32-erase-flash-memory/

Did you enter your SSID and Password in:

And I Think every project have to start with
#include <Arduino.h>

Yes appropriate wifi credentials were added in every case. Well spotted indeed! I slipped when selecting to copy the code and missed out the top line which was #include

Thanks for your attention.

Thank you. I’ve come to a similar conclusion i.e. reflash the esp32 so as to eliminate the possibilty that it is due to some sort of corruption -I will report back in due course.

Simple (as it turned out) solution proved to be erasing the flash memory:

python -m esptool --chip esp32 erase_flash

The issue remains as to how you identify this by receiving error signals and from where? but at least its solved.