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

View File

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