ESP32 is not connecting to WiFi

Hi, I'm doing a project in which I have to connect to a WiFi network. So, I've used this program for ESP32 to connect to WiFi network -

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

void setup() {
  Serial.begin(115200);
  WiFi.begin("XYZXYZ", "asdfghjkl");

  // Wait for wifi to be connected
  uint32_t notConnectedCounter = 0;
  while (WiFi.status() != WL_CONNECTED) {
      delay(100);
      Serial.println("Wifi connecting...");
      notConnectedCounter++;
      if(notConnectedCounter > 50) { // Reset board if not connected after 5s
          Serial.println("Resetting due to Wifi not connecting...");
          ESP.restart();
      }
  }
  Serial.print("Wifi connected, IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // put your main code here, to run repeatedly:
}

A month ago the above one worked well but for some reason it is not working now. Now when I power the ESP32, it throws out this (and similar to this) -

}⸮~⸮}⸮n~⸮~ֶ⸮}⸮⸮\⸮n⸮~n⸮⸮]⸮⸮⸮⸮⸮⸮D⸮b⸮⸮z⸮}⸮⸮⸮1]⸮⸮}⸮⸮~⸮⸮|⸮⸮~}⸮]⸮}⸮=~]⸮⸮]⸮⸮]⸮]⸮⸮n⸮⸮
֎UvHX⸮.,⸮⸮a⸮⸮⸮]⸮⸮>]⸮>⸮]⸮]⸮n>⸮⸮⸮>>⸮]⸮⸮>]⸮]⸮⸮6⸮

I've tried other codes but the result is same, I don't understand why. Can you please help me regarding this

Your post was MOVED to its current location as it is more suitable.

The obvious first question is ….

Have you setup the Serial Monitor at the correct speed etc ?

The strange characters in the serial output look like you have not adjusted the baudrate to the value you set it to in yur code 115200.

Your code is waiting 5 seconds until

ESP.restart();

here is a demo-code that has three useful additional functions

// 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>

const char *ssid     = "Your SSID";
const char *password = "your password";


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;


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 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);

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED && myCount < 31) {
    BlinkHeartBeatLED(OnBoard_LED, 50); // blink LED fast during attempt to connect

    if ( TimePeriodIsOver(MyTestTimer, 500) ) { // once every 500 miliseconds
      Serial.print(".");                        // print a dot
       myCount++; 
       
      if (myCount > 30) { // after 30 dots = 15 seconds 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

 */

best regards Stefan