Problem with esp32 delayed ping response

hi im writing a code with c++ using arduino platform for esp32 wroom. i wrote a simple code that connects to wifi and gets ping from a server. in normal situation it works correctly but the problem i have is that when i turn off the wifi router while the esp is running and turn it back on it takes about 15 seconds for esp to connect to wifi and then it takes about 1.5 minute to get a successful ping response. the odd thing is that if i use mobile phone hotspot internet it pings instantly.
how can i resolve this issue ? by the way i checked the server ping with command prompt of windows its fine.
thanks in advance

#include <WiFi.h>  // use Arduino's standard WiFi library
#include "ESP32Ping.h"
#include <WiFi.h>
#include <ArduinoHttpClient.h>
const char* ssid = "a";         // Replace with your Wi-Fi SSID
const char* password = "87654321"; // Replace with your Wi-Fi password
const char* pingAddress = "socket.mydnspanel.com"; // Domain to ping
IPAddress dns1IP = IPAddress(8, 8, 8, 8);
        IPAddress dns2IP = IPAddress(8, 8, 4, 4);
unsigned long previousMillis = 0;
const long interval = 1000; // Check Wi-Fi connection every 1 second
WiFiClient wifi;
HttpClient client = HttpClient(wifi, "socket.mydnspanel.com", 8593);



void connectToWiFi();
void resetWiFiModule();
void setup() {
  Serial.begin(115200);
  connectToWiFi();
}

void loop() {
  unsigned long currentMillis = millis();

  // Aggressively check for Wi-Fi connection status every second
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    if (WiFi.status() != WL_CONNECTED) {
      Serial.println("Wi-Fi disconnected, resetting Wi-Fi module...");
      resetWiFiModule();
      connectToWiFi();
    } else {
      Serial.println("Wi-Fi connected!");
      if (Ping.ping("socket.mydnspanel.com",1)) {
        Serial.println("ping was successful!");
      } else {
        Serial.println("ping failed.");
      }
    }
  }
}

void connectToWiFi() {
  WiFi.begin(ssid, password);
  Serial.print("Connecting to Wi-Fi");
  WiFi.waitForConnectResult(2000);
            // Serial.printf("wifi status is %hhu",wifi_status_check);
  WiFi.config(WiFi.localIP(), WiFi.gatewayIP(), WiFi.subnetMask(),dns1IP,dns2IP);

  unsigned long startAttemptTime = millis();
  const long connectionTimeout = 5000; // Set a 5-second timeout for Wi-Fi connection

  while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < connectionTimeout) {
    Serial.print(".");
    delay(500);
  }

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("\nConnected to Wi-Fi!");
  } else {
    Serial.println("\nFailed to connect to Wi-Fi.");
  }
}

void resetWiFiModule() {
  // Disconnect and turn off Wi-Fi to reset the module
  WiFi.disconnect(true); // true means also disable the WiFi
  delay(1000); // Small delay to ensure proper reset
  WiFi.mode(WIFI_STA); // Reinitialize WiFi as station mode
}

I think the issue is not with the device it is with the router. Mostly router take specific period of time to reconnect to internet when they restart or when they boot up. In mobile hotspot that wont be an issue because your sim is providing the network

Just check the lights on the router how long it takes the indicators turn on to give you full internet connection

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