Skip to content

Instantly share code, notes, and snippets.

@aliraza1944
Created November 26, 2024 09:40
Show Gist options
  • Select an option

  • Save aliraza1944/a203ec418aa8fd0880b8973c72b905ee to your computer and use it in GitHub Desktop.

Select an option

Save aliraza1944/a203ec418aa8fd0880b8973c72b905ee to your computer and use it in GitHub Desktop.
void wifiReconnectionTask(void *pvParameters)
{
while (1){
if (wifiDisconnected){
Serial.println("Reconnecting to WiFi...");
WiFi.disconnect(false, false); // Same as WiFi.Discon
int retryCount = 0;
while (WiFi.status() != WL_CONNECTED && retryCount < 4) {
Serial.print("Attempting to reconnect, try ");
Serial.println(retryCount + 1);
WiFi.begin(ssid, pass);
delay(2000); // Delay for 2 seconds between retr
retryCount++;
}
if (WiFi.status() == WL_CONNECTED)
{
Serial.println("Reconnected to WiFi successfully");
wifiDisconnected = false;
} else {
Serial.println("Failed to reconnect to WiFi after 4 attempts.");
Serial.println("Retry count exceeded. Restarting the device...");
wifiDisconnected = true;
esp.restart(); //check the arduino restart function.
}
// Add a delay to avoid flooding the serial output
delay(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment