final details on field testing

This commit is contained in:
Guillermo Marcel 2025-05-17 00:28:37 -03:00
parent 04f9d36da3
commit 57a6cdac5d
4 changed files with 152 additions and 58 deletions

View File

@ -3,7 +3,6 @@
#include <MFRC522DriverPinSimple.h> #include <MFRC522DriverPinSimple.h>
#include <MFRC522Debug.h> #include <MFRC522Debug.h>
#include "alarm.h" #include "alarm.h"
// #include <Dictionary.h>
#include "Dict.h" #include "Dict.h"
#include "Times.h" #include "Times.h"
@ -14,8 +13,6 @@
MFRC522DriverPinSimple ss_pin(5); MFRC522DriverPinSimple ss_pin(5);
class CardReader{ class CardReader{
private: private:
String user1="0438768a2c6a80";
String user2="23141f2d";
MFRC522DriverSPI driver{ss_pin}; // Create SPI driver MFRC522DriverSPI driver{ss_pin}; // Create SPI driver
//MFRC522DriverI2C driver{}; // Create I2C driver //MFRC522DriverI2C driver{}; // Create I2C driver
@ -89,7 +86,8 @@ public:
return; return;
} }
// Serial.print("Card reader - Restart ok: "); // Serial.print("Card reader - Restart ok: ");
MFRC522Debug::PCD_DumpVersionToSerial(mfrc522, Serial); // No logs for successful restart
//MFRC522Debug::PCD_DumpVersionToSerial(mfrc522, Serial);
return; return;
} }

View File

@ -1,8 +1,7 @@
#include "freertos/portable.h"
#include "alarm.h" #include "alarm.h"
#include "Times.h" #include "Times.h"
#include <HTTPClient.h> #include <HTTPClient.h>
// #include <Dictionary.h>
// #include <typeinfo>
#include <iostream> #include <iostream>
#include <ArduinoJson.h> #include <ArduinoJson.h>
@ -11,7 +10,7 @@ class ServerConnector
private: private:
AlarmStatus *status; AlarmStatus *status;
long lastUpdate; long lastUpdate;
WiFiClient *wifiClient; // WiFiClient wifiClient;
CardReader *_cardReader; CardReader *_cardReader;
const String AUTHORIZED_ENTRANCE = "AUTHORIZE_ENTRANCE"; const String AUTHORIZED_ENTRANCE = "AUTHORIZE_ENTRANCE";
@ -25,16 +24,44 @@ private:
const String FIELD_ALLOWED_CARDS = "cards"; const String FIELD_ALLOWED_CARDS = "cards";
int failedAttempts; int failedAttempts;
bool running;
// long lastHeapSize=0;
// long initHeapSize=0;
void HandTask() void HandTask()
{ {
// ServerConnector * this = (ServerConnector *) pvParameters; // if(initHeapSize == 0)
// {
// initHeapSize=xPortGetFreeHeapSize();
// }
// long currentHeapSize=0;
running = true;
for (;;) for (;;)
{ {
// every 15 seconds ask for update: DISSARM & CARDS // every 15 seconds ask for update: DISSARM & CARDS
if (lastUpdate + FromSeconds(15) < millis()) unsigned long currentMillis = millis();
if (lastUpdate + FromSeconds(15) < currentMillis)
{ {
lastUpdate = millis(); lastUpdate = currentMillis;
// currentHeapSize= xPortGetFreeHeapSize();
// if(abs(currentHeapSize - lastHeapSize) > 32)
// {
// Serial.print("[MEMORY] xPortGetFreeHeapSize: "); Serial.print(currentHeapSize);
// Serial.print( "| Total Variation: "); Serial.print(currentHeapSize - initHeapSize);
// Serial.print(" | Variation: "); Serial.println(currentHeapSize - lastHeapSize);
// lastHeapSize=currentHeapSize;
// }
if(failedAttempts == 5)
{
Serial.println("[HTTP] 5 fail connections, restarting thread.");
failedAttempts=6;
running=false;
vTaskDelete(NULL);
return;
}
if(!CheckWifiConnection()){ if(!CheckWifiConnection()){
Serial.println("[HTTP] could not recconect, trying later"); Serial.println("[HTTP] could not recconect, trying later");
@ -50,7 +77,7 @@ private:
} }
if (status->sendNotif) if (status->sendNotif)
{ {
status->eventId = millis(); status->eventId = currentMillis;
} }
SendAlarm(); SendAlarm();
status->sendNotif = false; status->sendNotif = false;
@ -60,8 +87,10 @@ private:
bool CheckWifiConnection() bool CheckWifiConnection()
{ {
if(failedAttempts > 5)
if(failedAttempts >= 10)
{ {
Serial.println("[HTTP] Too many faliures, restarting system");
ESP.restart(); ESP.restart();
} }
@ -80,6 +109,7 @@ private:
{ {
String eventtype = status->sendNotif ? EVEN_TYPE_FIRED : EVENT_TYPE_EVENT_UPDATE; String eventtype = status->sendNotif ? EVEN_TYPE_FIRED : EVENT_TYPE_EVENT_UPDATE;
String url = "http://" + ServerAddress + "/?eventId=" + String(status->eventId) + "&eventType=" + eventtype + "&eventData=t"; String url = "http://" + ServerAddress + "/?eventId=" + String(status->eventId) + "&eventType=" + eventtype + "&eventData=t";
Serial.println("[HTTP] Contacting Alarm Server: " + url);
String response = MakeHttpCall(url); String response = MakeHttpCall(url);
if (response == "") if (response == "")
{ {
@ -98,7 +128,24 @@ private:
void RequestUpdate() void RequestUpdate()
{ {
String url = "http://" + ServerAddress + "/?eventId=0&eventType=" + EVENT_TYPE_UPDATE + "&eventData=t"; StaticJsonDocument<200> docStatus;
docStatus["doorStatus"] = status->doorStatus;
docStatus["armed"] = status->isArmed;
docStatus["fireing"] = status->isFired;
docStatus["lastUser"] = status->userAllowed;
docStatus["lastEntrance"] = status->lastEntrance;
docStatus["millis"] = millis();
String jsonString;
serializeJson(docStatus, jsonString);
String encodedJson = standardUrlEncode(jsonString);
String url = "http://" + ServerAddress + "/?eventId=0&eventType=" + EVENT_TYPE_UPDATE + "&eventData=" + encodedJson;
// Serial.println(url);
String response = MakeHttpCall(url); String response = MakeHttpCall(url);
if (response == "") if (response == "")
{ {
@ -106,9 +153,8 @@ private:
return; return;
} }
Dictionary &d = *(new Dictionary());
JsonDocument doc; JsonDocument doc;
// deserializeJson(doc, response); deserializeJson(doc, response);
DeserializationError error = deserializeJson(doc, response); DeserializationError error = deserializeJson(doc, response);
@ -140,36 +186,32 @@ private:
} }
String MakeHttpCall(String url) String MakeHttpCall(String url)
{ {
if (!wifiClient) HTTPClient http;
{ http.setReuse(false);
Serial.printf("[HTTP] Unable to connect\n");
failedAttempts= failedAttempts + 1;
return "";
}
HTTPClient https; // if (!http.begin(wifiClient, url))
https.setReuse(false); if (!http.begin(url))
{
Serial.println("[HTTP] " + url);
if (!https.begin(*wifiClient, url))
{ // HTTPS
failedAttempts= failedAttempts + 1; failedAttempts= failedAttempts + 1;
Serial.println("[HTTP] not able to start http call"); Serial.println("[HTTP] not able to start http call");
https.end(); http.end();
return ""; return "";
} }
// Serial.print("[HTTPS] GET...\n");
// start connection and send HTTP header int httpCode = http.GET();
int httpCode = https.GET();
// httpCode will be negative on error // httpCode will be negative on error
// Serial.printf("[HTTP] wifi.available: %d, connected: %d \r\n", wifiClient.available(), wifiClient.connected());
// Serial.printf("[HTTP] http.connected: %d \r\n", http.connected());
if (httpCode <= 0) if (httpCode <= 0)
{ {
failedAttempts= failedAttempts + 1; failedAttempts= failedAttempts + 1;
Serial.printf("[HTTP] GET... failed, error: %s\r\n", https.errorToString(httpCode).c_str()); Serial.printf("[HTTP] Failed connetion - code: %d , error: %s\r\n", httpCode, http.errorToString(httpCode).c_str());
https.end();
http.end();
return ""; return "";
} }
@ -180,29 +222,67 @@ private:
{ {
failedAttempts= failedAttempts + 1; failedAttempts= failedAttempts + 1;
Serial.println("[HTTP] Server responded non 200 code"); Serial.println("[HTTP] Server responded non 200 code");
https.end(); http.end();
return ""; return "";
} }
// print server response payload // print server response payload
failedAttempts=0; failedAttempts=0;
String payload = https.getString();
https.end(); String payload = http.getString();
http.end();
return payload; return payload;
} }
String standardUrlEncode(const String &str) {
String encoded = "";
char c;
char code0;
char code1;
for (size_t i = 0; i < str.length(); i++) {
c = str.charAt(i);
// Check if character is unreserved per RFC 3986
if ((c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') ||
c == '-' || c == '_' || c == '.' || c == '~') {
encoded += c;
} else {
encoded += '%';
code0 = (c >> 4) & 0xF;
code1 = c & 0xF;
encoded += (code0 < 10) ? char(code0 + '0') : char(code0 - 10 + 'A');
encoded += (code1 < 10) ? char(code1 + '0') : char(code1 - 10 + 'A');
}
}
return encoded;
}
public: public:
ServerConnector(AlarmStatus *statusObj, CardReader *cardReader) ServerConnector(AlarmStatus *statusObj, CardReader *cardReader)
{ {
status = statusObj; status = statusObj;
wifiClient = new WiFiClient(); running = false;
// wifiClient = new WiFiClient();
_cardReader = cardReader; _cardReader = cardReader;
failedAttempts=0; failedAttempts=0;
} }
bool IsRunning()
{
return running;
}
void StartNotifierAsync() void StartNotifierAsync()
{ {
xTaskCreate(this->asynTask, "sendNotif", 6000, (void *)this, 2, NULL); running = true;
BaseType_t xReturned;
TaskHandle_t xHandle = NULL;
xReturned = xTaskCreate(this->asynTask, "sendNotif", 3000, (void *)this, 2, &xHandle);
failedAttempts=0;
Serial.print("Start: "); Serial.println(xReturned == pdPASS ? "Pass" : "Not pass");
Serial.print("Status: "); Serial.println(eTaskGetState(xHandle));
} }
static void asynTask(void *pvParameter) static void asynTask(void *pvParameter)

View File

@ -1,4 +1,5 @@
#include "alarm.h" #include "alarm.h"
#include "Times.h"
AlarmStatus* status; AlarmStatus* status;
@ -6,7 +7,8 @@ class DoorSensor {
private: private:
int DOOR_SENSOR_PIN = 14; int DOOR_SENSOR_PIN = 14;
AlarmStatus* status; AlarmStatus* status;
int lastDoorState=1; int lastDoorState = 1; // Last instantaneous reading
unsigned long lastDebounceTime = 0; // Time when a change was first detected
public: public:
@ -23,6 +25,7 @@ public:
void Init(){ void Init(){
pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP);
status->doorStatus = digitalRead(DOOR_SENSOR_PIN); status->doorStatus = digitalRead(DOOR_SENSOR_PIN);
lastDoorState = status->doorStatus;
} }
bool IsDoorOpen(){ bool IsDoorOpen(){
@ -33,19 +36,27 @@ public:
} }
void HandleDoor(){ void HandleDoor(){
// Read the current state of the door sensor
int reading = digitalRead(DOOR_SENSOR_PIN);
//status->doorStatus = digitalRead(DOOR_SENSOR_PIN); // If the reading has changed from the previous instantaneous reading,
lastDoorState = status->doorStatus; // save the last state // reset the debounce timer.
status->doorStatus = digitalRead(DOOR_SENSOR_PIN); // read new state if (reading != lastDoorState) {
// state change: LOW -> HIGH lastDebounceTime = millis();
if (lastDoorState == DOOR_CLOSED && status->doorStatus == DOOR_OPEN) {
//handleOpen();
status->doorChanged=true;
} }
else
if (lastDoorState == DOOR_OPEN && status->doorStatus == DOOR_CLOSED) { // state change: HIGH -> LOW // If the new reading has remained stable longer than the debounce delay,
// handleClose(); // then update the door state.
status->doorChanged=true; if ((millis() - lastDebounceTime) >= FromSeconds(1)) {
// If the reading is different from the last stable door state,
// update the status and mark that a change has occurred.
if (reading != status->doorStatus) {
status->doorStatus = reading;
status->doorChanged = true;
}
} }
// Save the current reading for the next iteration.
lastDoorState = reading;
} }
}; };

View File

@ -7,6 +7,7 @@
#include "Siren.h" #include "Siren.h"
#include "CardReader.h" #include "CardReader.h"
#include "soc/rtc_wdt.h" #include "soc/rtc_wdt.h"
//#include <rtc_wdt.h>
#include <WiFi.h> #include <WiFi.h>
#include "ServerConnector.h" #include "ServerConnector.h"
// #include <WiFiClientSecure.h> // #include <WiFiClientSecure.h>
@ -70,7 +71,7 @@ void setup()
void addUsers(){ void addUsers(){
cardReader.AddUser("0438768a2c6a80", "pin verizure"); cardReader.AddUser("0438768a2c6a80", "pin verizure");
// cardReader.AddUser("23141f2d", "pin azul"); cardReader.AddUser("23141f2d", "pin azul");
cardReader.AddUser("91cf3e02", "card access"); cardReader.AddUser("91cf3e02", "card access");
} }
@ -107,18 +108,22 @@ void loop()
} }
led.Update(); led.Update();
// delay(1000); if(!serverConnector.IsRunning())
{
Serial.println("Restarting communication thread.");
serverConnector.StartNotifierAsync();
}
} }
void DoorEvent() void DoorEvent()
{ {
if (doorSensor.IsDoorOpen()) if (doorSensor.IsDoorOpen())
{ {
Serial.println("The door-opening"); Serial.print("[Door] The door-opening: ");
if (!s.isArmed) if (!s.isArmed)
{ {
Serial.println("Alarm is dissarmed, not fireing"); Serial.println("Alarm not armed");
return; return;
} }
if(s.isFired){ // Already fired. if(s.isFired){ // Already fired.
@ -131,7 +136,7 @@ void DoorEvent()
else else
{ {
// delay(500); // delay(500);
Serial.println("The door-closing"); Serial.println("[Door] The door-closing");
s.isArmed = true; s.isArmed = true;
} }
} }
@ -156,7 +161,7 @@ void AutoRearm(){
//Auto Rearm if door is closed. //Auto Rearm if door is closed.
if(s.doorStatus == DOOR_CLOSED){ if(s.doorStatus == DOOR_CLOSED){
Serial.println("5m Passed, AutoArming again"); Serial.println("[Door] 5m Passed, AutoArming again");
s.isArmed= true; s.isArmed= true;
} }