212 lines
5.2 KiB
C++
212 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 <Wire.h>
|
|
#include <ArduinoJson.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
|
|
|
|
// 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"
|
|
|
|
// Cooldown pour le badge NFC
|
|
#define NFC_COOLDOWN 1000
|
|
|
|
// NFC
|
|
PN532_HSU pn532hsu(Serial1);
|
|
NfcAdapter nfc(pn532hsu);
|
|
|
|
// Firebase
|
|
FirebaseData fbdo, stream;
|
|
FirebaseAuth auth;
|
|
FirebaseConfig config;
|
|
|
|
// NTP
|
|
WiFiUDP ntpUDP;
|
|
NTPClient timeClient(ntpUDP, "pool.ntp.org");
|
|
|
|
// Variables
|
|
volatile bool motionDetected = false;
|
|
bool armed = false, triggered = false;
|
|
unsigned long lastNfcCheck = 0;
|
|
|
|
void IRAM_ATTR onMotionDetected() {
|
|
motionDetected = true;
|
|
}
|
|
|
|
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 log;
|
|
log.set("timestamp", timestampISO);
|
|
log.set("sender", "ALARM");
|
|
log.set("message", message);
|
|
Firebase.RTDB.pushJSON(&fbdo, PATH_LOGS, &log);
|
|
}
|
|
|
|
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") 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");
|
|
}
|
|
}
|
|
|
|
|
|
void handleRFIDTag(const String& scannedID) {
|
|
// Récupérer la liste des badges depuis Firebase
|
|
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;
|
|
}
|
|
|
|
bool badgeFound = false;
|
|
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;
|
|
}
|
|
}
|
|
|
|
if (!badgeFound) {
|
|
logMessage = String("Badge inconnu : ") + scannedID;
|
|
}
|
|
|
|
addLog(logMessage.c_str());
|
|
}
|
|
|
|
|
|
void processNFCTag() {
|
|
if (millis() - lastNfcCheck < NFC_COOLDOWN) return;
|
|
lastNfcCheck = millis();
|
|
|
|
if (nfc.tagPresent()) {
|
|
handleRFIDTag(nfc.read().getUidString());
|
|
}
|
|
}
|
|
|
|
void connectToWiFi() {
|
|
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
|
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;
|
|
auth.user.password = USER_PASSWORD;
|
|
Firebase.begin(&config, &auth);
|
|
Firebase.reconnectWiFi(true);
|
|
|
|
if (!Firebase.RTDB.beginStream(&stream, PATH_STATE)) {
|
|
Serial.printf("Erreur stream : %s\n", stream.errorReason().c_str());
|
|
}
|
|
Firebase.RTDB.setStreamCallback(&stream, streamCallback, nullptr);
|
|
|
|
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 initialisé");
|
|
|
|
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();
|
|
triggered = true;
|
|
Firebase.RTDB.setBool(&fbdo, PATH_TRIGGERED, true);
|
|
addLog("Mouvement détecté, alarme déclenchée");
|
|
}
|
|
} else {
|
|
stopSirene();
|
|
if (!triggered)
|
|
blinkSlowGreen();
|
|
else {
|
|
startSirene();
|
|
blinkFastRed();
|
|
}
|
|
}
|
|
|
|
updateLed();
|
|
updateSirene();
|
|
}
|