153 lines
3.9 KiB
C++
153 lines
3.9 KiB
C++
#include <Arduino.h>
|
|
#include <WiFi.h>
|
|
#include <Firebase_ESP_Client.h>
|
|
#include <ctrlLed.h>
|
|
#include <ctrlSpeaker.h>
|
|
#include <ctrlRfid.h>
|
|
#include <branchements.h>
|
|
#include <NTPClient.h>
|
|
|
|
#define WIFI_SSID "ratio"
|
|
#define WIFI_PASSWORD "123456789"
|
|
|
|
#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"
|
|
|
|
// Déclaration des objets Firebase
|
|
FirebaseData stream;
|
|
FirebaseData fbdo;
|
|
FirebaseAuth auth;
|
|
FirebaseConfig config;
|
|
|
|
// Variables de gestion de l'alarme
|
|
volatile bool motionDetected = false;
|
|
bool armed = false;
|
|
bool triggered = false;
|
|
|
|
WiFiUDP ntpUDP;
|
|
NTPClient timeClient(ntpUDP, "pool.ntp.org");
|
|
|
|
// Stream Firebase
|
|
void streamCallback(FirebaseStream data) {
|
|
Serial.printf("Firebase Update - Path: %s, Data: %s\n",
|
|
data.dataPath().c_str(),
|
|
data.stringData().c_str());
|
|
|
|
if (data.dataPath() == "/armed") {
|
|
armed = data.boolData();
|
|
Serial.printf("🔒 Alarme armée: %s\n", armed ? "OUI" : "NON");
|
|
addLog(armed ? "Alarme armée" : "Alarme désarmée");
|
|
}
|
|
|
|
if (data.dataPath() == "/triggered") {
|
|
triggered = data.boolData();
|
|
Serial.printf("🚨 Alarme déclenchée: %s\n", triggered ? "OUI" : "NON");
|
|
if(!armed){
|
|
addLog("Alarme désactivée à distance");
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
void streamTimeoutCallback(bool timeout) {
|
|
if (timeout)
|
|
Serial.println("⏳ Stream timeout, reconnexion...");
|
|
}
|
|
|
|
// Détection mouvement
|
|
void IRAM_ATTR onMotionDetected() {
|
|
motionDetected = true;
|
|
}
|
|
|
|
|
|
void addLog(const char* messageLog) {
|
|
FirebaseJson logData;
|
|
|
|
timeClient.update();
|
|
time_t rawTime = timeClient.getEpochTime();
|
|
struct tm* timeinfo;
|
|
timeinfo = gmtime(&rawTime);
|
|
char timestampISO[25];
|
|
strftime(timestampISO, sizeof(timestampISO), "%Y-%m-%dT%H:%M:%SZ", timeinfo);
|
|
|
|
logData.add("timestamp", timestampISO);
|
|
logData.add("sender", "ALARM");
|
|
logData.add("message", messageLog);
|
|
|
|
if (Firebase.RTDB.pushJSON(&fbdo, "/Alarm/logs", &logData)) {
|
|
Serial.println("Log ajouté avec succès à Firebase avec timestamp");
|
|
} else {
|
|
Serial.print("Erreur lors de l'ajout du log avec timestamp à Firebase: ");
|
|
Serial.println(fbdo.errorReason());
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
|
|
// Connexion WiFi
|
|
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
Serial.print(".");
|
|
delay(500);
|
|
}
|
|
Serial.println("\n✅ WiFi connecté ! C'est super");
|
|
|
|
// Configuration Firebase
|
|
config.api_key = API_KEY;
|
|
auth.user.email = USER_EMAIL;
|
|
auth.user.password = USER_PASSWORD;
|
|
config.database_url = DATABASE_URL;
|
|
|
|
Firebase.begin(&config, &auth);
|
|
Firebase.reconnectWiFi(true);
|
|
|
|
// Initialisation du streaming Firebase
|
|
if (!Firebase.RTDB.beginStream(&stream, "/Alarm"))
|
|
Serial.printf("🔥 Erreur de stream: %s\n", stream.errorReason().c_str());
|
|
Firebase.RTDB.setStreamCallback(&stream, streamCallback, streamTimeoutCallback);
|
|
|
|
addLog("Alarme connectée");
|
|
|
|
timeClient.begin();
|
|
timeClient.update();
|
|
|
|
pinMode(A2, INPUT);
|
|
pinMode(D2, OUTPUT);
|
|
attachInterrupt(digitalPinToInterrupt(A2), onMotionDetected, RISING);
|
|
blinkSlowGreen();
|
|
}
|
|
|
|
void loop() {
|
|
Firebase.ready();
|
|
|
|
if (armed) {
|
|
blinkSlowRed();
|
|
if (motionDetected) {
|
|
motionDetected = false;
|
|
Serial.println("🎯 Mouvement détecté !");
|
|
|
|
blinkFastRed();
|
|
startSirene();
|
|
|
|
Firebase.RTDB.setBool(&fbdo, "/Alarm/triggered", true);
|
|
triggered = true;
|
|
|
|
addLog("Mouvement détecté, alarme déclenchée");
|
|
}
|
|
}
|
|
|
|
if (!triggered) {
|
|
stopSirene();
|
|
blinkSlowGreen();
|
|
}else{
|
|
startSirene();
|
|
blinkFastRed();
|
|
}
|
|
|
|
updateLed();
|
|
updateSirene();
|
|
}
|