207 lines
5.2 KiB
C++
207 lines
5.2 KiB
C++
#include <Arduino.h>
|
|
#include <WiFi.h>
|
|
#include <Firebase_ESP_Client.h>
|
|
#include <ctrlLed.h>
|
|
#include <ctrlSpeaker.h>
|
|
#include <branchements.h>
|
|
#include <NTPClient.h>
|
|
#include "PN532_HSU.h"
|
|
#include "PN532.h"
|
|
#include "NfcAdapter.h"
|
|
#include "NfcTag.h"
|
|
#include <Wire.h>
|
|
|
|
// WiFi Configuration
|
|
#define WIFI_SSID "ratio"
|
|
#define WIFI_PASSWORD "123456789"
|
|
|
|
// Firebase Configuration
|
|
#define API_KEY "AIzaSyB8qf7aeI7F5l7d1NDhRQrNNLW8aPaOkl4"
|
|
#define DATABASE_URL "https://alarmeesp32-2ca19-default-rtdb.europe-west1.firebasedatabase.app"
|
|
#define USER_EMAIL "alarm@alarm.bip"
|
|
#define USER_PASSWORD "123456"
|
|
#define FIREBASE_USE_PSRAM
|
|
|
|
// Cooldown en millisecondes pour la vérification NFC
|
|
#define NFC_COOLDOWN 1000
|
|
|
|
// Déclaration Firebase
|
|
FirebaseData fbdo, stream;
|
|
FirebaseAuth auth;
|
|
FirebaseConfig config;
|
|
|
|
// NFC
|
|
PN532_HSU pn532hsu(Serial1);
|
|
NfcAdapter nfc(pn532hsu);
|
|
|
|
// Variables Alarme
|
|
volatile bool motionDetected = false;
|
|
bool armed = false, triggered = false;
|
|
|
|
// NTP pour la gestion des logs
|
|
WiFiUDP ntpUDP;
|
|
NTPClient timeClient(ntpUDP, "pool.ntp.org");
|
|
|
|
// Variable pour le cooldown NFC
|
|
unsigned long lastNfcCheck = 0;
|
|
|
|
void IRAM_ATTR onMotionDetected() {
|
|
motionDetected = true;
|
|
}
|
|
|
|
void addLog(const char* messageLog) {
|
|
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);
|
|
|
|
Firebase.RTDB.pushJSON(&fbdo, "/Alarm/logs", &logData);
|
|
}
|
|
|
|
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");
|
|
}
|
|
}
|
|
}
|
|
|
|
// Cette fonction interroge Firebase pour vérifier si le badge existe
|
|
void handleRFIDTag(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
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
badgesJson.iteratorEnd();
|
|
|
|
if (!badgeFound) {
|
|
foundMsg = String("Badge inconnu : ") + scannedID;
|
|
}
|
|
addLog(foundMsg.c_str());
|
|
}
|
|
|
|
void processNFCTag() {
|
|
// Cooldown pour éviter des vérifications trop fréquentes
|
|
if (millis() - lastNfcCheck < NFC_COOLDOWN) {
|
|
return;
|
|
}
|
|
lastNfcCheck = millis();
|
|
|
|
if (nfc.tagPresent()) {
|
|
NfcTag tag = nfc.read();
|
|
String uid = tag.getUidString();
|
|
handleRFIDTag(uid);
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
|
|
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
}
|
|
Serial.println("WiFi connecte");
|
|
|
|
config.api_key = API_KEY;
|
|
config.database_url = DATABASE_URL;
|
|
auth.user.email = USER_EMAIL;
|
|
auth.user.password = USER_PASSWORD;
|
|
Firebase.begin(&config, &auth);
|
|
Firebase.reconnectWiFi(true);
|
|
|
|
if (!Firebase.RTDB.beginStream(&stream, "/Alarm/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");
|
|
|
|
timeClient.begin();
|
|
Serial1.begin(9600, SERIAL_8N1, RX, TX);
|
|
nfc.begin();
|
|
Serial.println("NFC initialise");
|
|
|
|
pinMode(A2, INPUT);
|
|
pinMode(D2, OUTPUT);
|
|
attachInterrupt(digitalPinToInterrupt(A2), onMotionDetected, RISING);
|
|
blinkSlowGreen();
|
|
}
|
|
|
|
void loop() {
|
|
Firebase.ready();
|
|
processNFCTag();
|
|
|
|
if (armed) {
|
|
blinkSlowRed();
|
|
if (motionDetected && !triggered) {
|
|
motionDetected = false;
|
|
blinkFastRed();
|
|
startSirene();
|
|
Firebase.RTDB.setBool(&fbdo, "/Alarm/state/triggered", true);
|
|
triggered = true;
|
|
addLog("Mouvement detecte, alarme declenchee");
|
|
}
|
|
} else if (!triggered) {
|
|
stopSirene();
|
|
if (!armed)
|
|
blinkSlowGreen();
|
|
} else {
|
|
startSirene();
|
|
blinkFastRed();
|
|
}
|
|
|
|
updateLed();
|
|
updateSirene();
|
|
}
|