Amélioration performances et fix bug seul le premier tag récupérer
This commit is contained in:
parent
c50c1077dd
commit
b0796f698b
199
AlarmeESP32.ino
199
AlarmeESP32.ino
@ -8,8 +8,8 @@
|
||||
#include "PN532_HSU.h"
|
||||
#include "PN532.h"
|
||||
#include "NfcAdapter.h"
|
||||
#include "NfcTag.h"
|
||||
#include <Wire.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
// WiFi Configuration
|
||||
#define WIFI_SSID "ratio"
|
||||
@ -22,136 +22,133 @@
|
||||
#define USER_PASSWORD "123456"
|
||||
#define FIREBASE_USE_PSRAM
|
||||
|
||||
// Cooldown en millisecondes pour la vérification NFC
|
||||
#define NFC_COOLDOWN 1000
|
||||
// Chemins Firebase
|
||||
#define PATH_STATE "/Alarm/state"
|
||||
#define PATH_TRIGGERED PATH_STATE "/triggered"
|
||||
#define PATH_ARMED PATH_STATE "/armed"
|
||||
#define PATH_LOGS "/Alarm/logs"
|
||||
#define PATH_BADGES "/Alarm/badges"
|
||||
|
||||
// Déclaration Firebase
|
||||
FirebaseData fbdo, stream;
|
||||
FirebaseAuth auth;
|
||||
FirebaseConfig config;
|
||||
// Cooldown pour le badge NFC
|
||||
#define NFC_COOLDOWN 1000
|
||||
|
||||
// NFC
|
||||
PN532_HSU pn532hsu(Serial1);
|
||||
NfcAdapter nfc(pn532hsu);
|
||||
|
||||
// Variables Alarme
|
||||
volatile bool motionDetected = false;
|
||||
bool armed = false, triggered = false;
|
||||
// Firebase
|
||||
FirebaseData fbdo, stream;
|
||||
FirebaseAuth auth;
|
||||
FirebaseConfig config;
|
||||
|
||||
// NTP pour la gestion des logs
|
||||
// NTP
|
||||
WiFiUDP ntpUDP;
|
||||
NTPClient timeClient(ntpUDP, "pool.ntp.org");
|
||||
|
||||
// Variable pour le cooldown NFC
|
||||
// Variables
|
||||
volatile bool motionDetected = false;
|
||||
bool armed = false, triggered = false;
|
||||
unsigned long lastNfcCheck = 0;
|
||||
|
||||
void IRAM_ATTR onMotionDetected() {
|
||||
motionDetected = true;
|
||||
}
|
||||
|
||||
void addLog(const char* messageLog) {
|
||||
void addLog(const char* message) {
|
||||
timeClient.update();
|
||||
char timestampISO[25];
|
||||
time_t epochTime = timeClient.getEpochTime();
|
||||
strftime(timestampISO, sizeof(timestampISO), "%Y-%m-%dT%H:%M:%SZ", gmtime(&epochTime));
|
||||
|
||||
FirebaseJson logData;
|
||||
logData.add("timestamp", timestampISO);
|
||||
logData.add("sender", "ALARM");
|
||||
logData.add("message", messageLog);
|
||||
FirebaseJson log;
|
||||
log.set("timestamp", timestampISO);
|
||||
log.set("sender", "ALARM");
|
||||
log.set("message", message);
|
||||
Firebase.RTDB.pushJSON(&fbdo, PATH_LOGS, &log);
|
||||
}
|
||||
|
||||
Firebase.RTDB.pushJSON(&fbdo, "/Alarm/logs", &logData);
|
||||
void updateAlarmState(bool newArmed, bool newTriggered) {
|
||||
FirebaseJson stateUpdate;
|
||||
stateUpdate.set("armed", newArmed);
|
||||
stateUpdate.set("triggered", newTriggered);
|
||||
Firebase.RTDB.updateNode(&fbdo, PATH_STATE, &stateUpdate);
|
||||
}
|
||||
|
||||
void streamCallback(FirebaseStream data) {
|
||||
if (data.dataType() == "boolean") {
|
||||
if (data.dataPath() == "/armed") {
|
||||
armed = data.boolData();
|
||||
addLog(armed ? "Alarme armee" : "Alarme desarmee");
|
||||
} else if (data.dataPath() == "/triggered") {
|
||||
triggered = data.boolData();
|
||||
if (!armed)
|
||||
addLog("Alarme desactivee a distance");
|
||||
}
|
||||
if (data.dataType() != "boolean") return;
|
||||
|
||||
if (data.dataPath() == "/armed") {
|
||||
armed = data.boolData();
|
||||
addLog(armed ? "Alarme armée" : "Alarme désarmée");
|
||||
} else if (data.dataPath() == "/triggered") {
|
||||
triggered = data.boolData();
|
||||
if (!armed)
|
||||
addLog("Alarme désactivée à distance");
|
||||
}
|
||||
}
|
||||
|
||||
// Cette fonction interroge Firebase pour vérifier si le badge existe
|
||||
void handleRFIDTag(String scannedID) {
|
||||
|
||||
void handleRFIDTag(const String& scannedID) {
|
||||
// Récupérer la liste des badges depuis Firebase
|
||||
if (!Firebase.RTDB.getJSON(&fbdo, "/Alarm/badges")) {
|
||||
// En cas d'erreur, on n'intervient pas
|
||||
if (!Firebase.RTDB.getJSON(&fbdo, PATH_BADGES)) return;
|
||||
|
||||
String jsonStr = fbdo.jsonObject().raw();
|
||||
|
||||
// Estimez la capacité en fonction de la taille de votre JSON
|
||||
const size_t capacity = JSON_OBJECT_SIZE(10) + 512;
|
||||
DynamicJsonDocument doc(capacity);
|
||||
DeserializationError error = deserializeJson(doc, jsonStr);
|
||||
if (error) {
|
||||
addLog("Erreur parsing JSON badges");
|
||||
return;
|
||||
}
|
||||
|
||||
FirebaseJson badgesJson = fbdo.jsonObject();
|
||||
size_t count = badgesJson.iteratorBegin();
|
||||
|
||||
bool badgeFound = false;
|
||||
String foundMsg = "";
|
||||
|
||||
int type;
|
||||
String key;
|
||||
String value;
|
||||
FirebaseJsonData jsonData;
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
if (badgesJson.iteratorGet(i, type, key, value) == 0) {
|
||||
FirebaseJson badgeObj;
|
||||
badgeObj.setJsonData(value);
|
||||
|
||||
if (badgeObj.get(jsonData, "id")) {
|
||||
String badgeID = jsonData.stringValue;
|
||||
if (badgeID == scannedID) {
|
||||
if (triggered) {
|
||||
triggered = false;
|
||||
Firebase.RTDB.setBool(&fbdo, "/Alarm/state/triggered", false);
|
||||
}
|
||||
|
||||
badgeFound = true;
|
||||
FirebaseJsonData nameData;
|
||||
badgeObj.get(nameData, "name");
|
||||
if (armed) {
|
||||
foundMsg = "Alarme désactivée par " + nameData.stringValue;
|
||||
} else {
|
||||
foundMsg = "Alarme activée par " + nameData.stringValue;
|
||||
}
|
||||
armed = !armed;
|
||||
Firebase.RTDB.setBool(&fbdo, "/Alarm/state/armed", armed);
|
||||
}
|
||||
String logMessage;
|
||||
// Itération sur l'objet JSON
|
||||
for (JsonPair kv : doc.as<JsonObject>()) {
|
||||
JsonObject badge = kv.value().as<JsonObject>();
|
||||
const char* id = badge["id"];
|
||||
const char* name = badge["name"];
|
||||
if (scannedID == String(id)) {
|
||||
badgeFound = true;
|
||||
// Si triggered est vrai, réinitialise-le
|
||||
if (triggered) {
|
||||
triggered = false;
|
||||
Firebase.RTDB.setBool(&fbdo, PATH_TRIGGERED, false);
|
||||
}
|
||||
// Inverse l'état armed
|
||||
armed = !armed;
|
||||
Firebase.RTDB.setBool(&fbdo, PATH_ARMED, armed);
|
||||
logMessage = String(armed ? "Alarme activée par " : "Alarme désactivée par ") + name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
badgesJson.iteratorEnd();
|
||||
|
||||
|
||||
if (!badgeFound) {
|
||||
foundMsg = String("Badge inconnu : ") + scannedID;
|
||||
logMessage = String("Badge inconnu : ") + scannedID;
|
||||
}
|
||||
addLog(foundMsg.c_str());
|
||||
|
||||
addLog(logMessage.c_str());
|
||||
}
|
||||
|
||||
|
||||
void processNFCTag() {
|
||||
// Cooldown pour éviter des vérifications trop fréquentes
|
||||
if (millis() - lastNfcCheck < NFC_COOLDOWN) {
|
||||
return;
|
||||
}
|
||||
if (millis() - lastNfcCheck < NFC_COOLDOWN) return;
|
||||
lastNfcCheck = millis();
|
||||
|
||||
if (nfc.tagPresent()) {
|
||||
NfcTag tag = nfc.read();
|
||||
String uid = tag.getUidString();
|
||||
handleRFIDTag(uid);
|
||||
handleRFIDTag(nfc.read().getUidString());
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
void connectToWiFi() {
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
}
|
||||
Serial.println("WiFi connecte");
|
||||
while (WiFi.status() != WL_CONNECTED) delay(500);
|
||||
Serial.println("WiFi connecté");
|
||||
}
|
||||
|
||||
void setupFirebase() {
|
||||
config.api_key = API_KEY;
|
||||
config.database_url = DATABASE_URL;
|
||||
auth.user.email = USER_EMAIL;
|
||||
@ -159,22 +156,29 @@ void setup() {
|
||||
Firebase.begin(&config, &auth);
|
||||
Firebase.reconnectWiFi(true);
|
||||
|
||||
if (!Firebase.RTDB.beginStream(&stream, "/Alarm/state")) {
|
||||
Serial.printf("Erreur stream: %s\n", stream.errorReason().c_str());
|
||||
if (!Firebase.RTDB.beginStream(&stream, PATH_STATE)) {
|
||||
Serial.printf("Erreur stream : %s\n", stream.errorReason().c_str());
|
||||
}
|
||||
Firebase.RTDB.setStreamCallback(&stream, streamCallback, nullptr);
|
||||
Firebase.RTDB.setBool(&fbdo, "/Alarm/state/triggered", false);
|
||||
Firebase.RTDB.setBool(&fbdo, "/Alarm/state/armed", false);
|
||||
addLog("Alarme connectee");
|
||||
|
||||
updateAlarmState(false, false);
|
||||
addLog("Alarme connectée");
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
connectToWiFi();
|
||||
setupFirebase();
|
||||
|
||||
timeClient.begin();
|
||||
Serial1.begin(9600, SERIAL_8N1, RX, TX);
|
||||
nfc.begin();
|
||||
Serial.println("NFC initialise");
|
||||
Serial.println("NFC initialisé");
|
||||
|
||||
pinMode(A2, INPUT);
|
||||
pinMode(D2, OUTPUT);
|
||||
attachInterrupt(digitalPinToInterrupt(A2), onMotionDetected, RISING);
|
||||
|
||||
blinkSlowGreen();
|
||||
}
|
||||
|
||||
@ -188,17 +192,18 @@ void loop() {
|
||||
motionDetected = false;
|
||||
blinkFastRed();
|
||||
startSirene();
|
||||
Firebase.RTDB.setBool(&fbdo, "/Alarm/state/triggered", true);
|
||||
triggered = true;
|
||||
addLog("Mouvement detecte, alarme declenchee");
|
||||
Firebase.RTDB.setBool(&fbdo, PATH_TRIGGERED, true);
|
||||
addLog("Mouvement détecté, alarme déclenchée");
|
||||
}
|
||||
} else if (!triggered) {
|
||||
stopSirene();
|
||||
if (!armed)
|
||||
blinkSlowGreen();
|
||||
} else {
|
||||
startSirene();
|
||||
blinkFastRed();
|
||||
stopSirene();
|
||||
if (!triggered)
|
||||
blinkSlowGreen();
|
||||
else {
|
||||
startSirene();
|
||||
blinkFastRed();
|
||||
}
|
||||
}
|
||||
|
||||
updateLed();
|
||||
|
Loading…
x
Reference in New Issue
Block a user