fix: logs & reconnect wifi logic

This commit is contained in:
Guillermo Marcel 2025-05-08 11:48:06 -03:00
parent 1745121942
commit 1086fea07f
2 changed files with 33 additions and 15 deletions

View File

@ -41,7 +41,7 @@ public:
void Init(){
initialized = mfrc522.PCD_Init();
if(!initialized) return;
Serial.print("Card Reader init: ");
Serial.print("[Card Reader] init: ");
MFRC522Debug::PCD_DumpVersionToSerial(mfrc522, Serial); // Show details of PCD - MFRC522 Card Reader details.
lastRestart=millis();
lastCardRead=millis();
@ -64,10 +64,10 @@ public:
mfrc522.PCD_Reset();
auto version = mfrc522.PCD_GetVersion();
if(version == PCD_Version::Version_Unknown) {
Serial.println("restart attempt unsuccessful");
Serial.println("[Card Reader] restart attempt unsuccessful");
return;
}
Serial.print("Card reader: Restart attempt ok: ");
Serial.print("[Card Reader] Restart attempt ok: ");
MFRC522Debug::PCD_DumpVersionToSerial(mfrc522, Serial);
failed=false;
mfrc522.PCD_Init();
@ -85,7 +85,7 @@ public:
if(!mfrc522.PCD_Init()){
failed = true;
restartAttempt=millis();
Serial.print("Card reader - Restart failure");
Serial.print("[Card Reader] Restart failure");
return;
}
// Serial.print("Card reader - Restart ok: ");
@ -109,11 +109,11 @@ public:
auto sc = mfrc522.PICC_HaltA();
if(sc == StatusCode::STATUS_ERROR){
Serial.println("Connection with card reader failed");
Serial.println("[Card Reader] Connection with card reader failed");
failed=true;
restartAttempt=millis();
}else{
Serial.println("Card Reader: Could not read");
Serial.println("[Card Reader] Could not read");
}
status->showError =true;
@ -135,7 +135,7 @@ public:
}
uidString += String(mfrc522.uid.uidByte[i], HEX);
}
Serial.print("Card Read: ");Serial.println(uidString);
Serial.print("[Card Reader] ");Serial.println(uidString);
for (int i = 0; i < users.count(); i++) {
if(users(i) == uidString){
@ -145,7 +145,7 @@ public:
}
}
Serial.println("access not granted");
Serial.println("[Card Reader] access not granted");
status->showError =true;
}
};

View File

@ -33,6 +33,11 @@ private:
if (lastUpdate + FromSeconds(15) < millis())
{
lastUpdate = millis();
if(!CheckWifiConnection()){
Serial.println("[HTTP] could not recconect, trying later");
continue;
}
RequestUpdate();
}
@ -51,6 +56,19 @@ private:
}
}
bool CheckWifiConnection()
{
if(WiFi.status() == WL_CONNECTED){
return true;
}
Serial.println("[HTTP] Reconnecting to WiFi...");
WiFi.disconnect();
bool response = WiFi.reconnect();
return (WiFi.status() == WL_CONNECTED);
}
void SendAlarm()
{
String eventtype = status->sendNotif ? EVEN_TYPE_FIRED : EVENT_TYPE_EVENT_UPDATE;
@ -58,7 +76,7 @@ private:
String response = MakeHttpCall(url);
if (response == "")
{
Serial.println("Alarm Server not recheable");
Serial.println("[HTTP] Alarm Server not recheable");
return;
}
@ -77,7 +95,7 @@ private:
String response = MakeHttpCall(url);
if (response == "")
{
Serial.println("Update Server not recheable");
Serial.println("[HTTP] Update Server not recheable");
return;
}
@ -89,7 +107,7 @@ private:
if (error)
{
Serial.print("Serialize error: ");
Serial.print("[HTTP] Serialize error: ");
Serial.println(error.c_str());
return;
}
@ -99,7 +117,7 @@ private:
// disarm requested
// Todo Extract to Disarm method, and add to alarm.h
// same in card reader
Serial.println("Disarm request by server");
Serial.println("[HTTP] Disarm request by server");
status->isArmed = false;
status->isFired = false;
status->lastEntrance = millis();
@ -118,17 +136,17 @@ private:
{
if (!wifiClient)
{
Serial.printf("[HTTPS] Unable to connect\n");
Serial.printf("[HTTP] Unable to connect\n");
return "";
}
HTTPClient https;
Serial.println("[HTTPS] " + url);
Serial.println("[HTTP] " + url);
if (!https.begin(*wifiClient, url))
{ // HTTPS
Serial.println("not able to start http call");
Serial.println("[HTTP] not able to start http call");
return "";
}
// Serial.print("[HTTPS] GET...\n");