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