restart logic after many failed connections

This commit is contained in:
Guillermo Marcel 2025-05-11 13:10:13 -03:00
parent 1086fea07f
commit b85e6fec90

View File

@ -24,6 +24,8 @@ private:
const String FIELD_DISARM = "disarm";
const String FIELD_ALLOWED_CARDS = "cards";
int failedAttempts;
void HandTask()
{
// ServerConnector * this = (ServerConnector *) pvParameters;
@ -58,13 +60,18 @@ private:
bool CheckWifiConnection()
{
if(failedAttempts > 5)
{
ESP.restart();
}
if(WiFi.status() == WL_CONNECTED){
return true;
}
Serial.println("[HTTP] Reconnecting to WiFi...");
WiFi.disconnect();
bool response = WiFi.reconnect();
WiFi.reconnect();
return (WiFi.status() == WL_CONNECTED);
}
@ -137,16 +144,20 @@ private:
if (!wifiClient)
{
Serial.printf("[HTTP] Unable to connect\n");
failedAttempts= failedAttempts + 1;
return "";
}
HTTPClient https;
https.setReuse(false);
Serial.println("[HTTP] " + url);
if (!https.begin(*wifiClient, url))
{ // HTTPS
failedAttempts= failedAttempts + 1;
Serial.println("[HTTP] not able to start http call");
https.end();
return "";
}
// Serial.print("[HTTPS] GET...\n");
@ -156,7 +167,9 @@ private:
if (httpCode <= 0)
{
Serial.printf("[HTTP] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
failedAttempts= failedAttempts + 1;
Serial.printf("[HTTP] GET... failed, error: %s\r\n", https.errorToString(httpCode).c_str());
https.end();
return "";
}
@ -165,11 +178,14 @@ private:
// file found at server
if (httpCode != HTTP_CODE_OK)
{
failedAttempts= failedAttempts + 1;
Serial.println("[HTTP] Server responded non 200 code");
https.end();
return "";
}
// print server response payload
failedAttempts=0;
String payload = https.getString();
https.end();
return payload;
@ -181,6 +197,7 @@ public:
status = statusObj;
wifiClient = new WiFiClient();
_cardReader = cardReader;
failedAttempts=0;
}
void StartNotifierAsync()