diff --git a/AlarmeESP32.ino b/AlarmeESP32.ino index cba2713..d39089b 100644 --- a/AlarmeESP32.ino +++ b/AlarmeESP32.ino @@ -7,81 +7,65 @@ #include #include +// 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 -// Déclaration des objets Firebase -FirebaseData stream; -FirebaseData fbdo; +// Déclaration Firebase +FirebaseData fbdo, stream; FirebaseAuth auth; FirebaseConfig config; -// Variables de gestion de l'alarme +// Variables Alarme volatile bool motionDetected = false; -bool armed = false; -bool triggered = false; +bool armed = false, triggered = false; +// NTP pour la gestion des logs 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 +// Gestion de l'alarme void IRAM_ATTR onMotionDetected() { motionDetected = true; } - +// Ajout de logs Firebase void addLog(const char* messageLog) { - FirebaseJson logData; + FirebaseJson logData; + timeClient.update(); + char timestampISO[25]; + time_t epochTime = timeClient.getEpochTime(); + strftime(timestampISO, sizeof(timestampISO), "%Y-%m-%dT%H:%M:%SZ", gmtime(&epochTime)); + + logData.add("timestamp", timestampISO); + logData.add("sender", "ALARM"); + logData.add("message", messageLog); - 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); + if (!Firebase.RTDB.pushJSON(&fbdo, "/Alarm/logs", &logData)) { + Serial.println("❌ Erreur ajout log Firebase : " + fbdo.errorReason()); + } +} - logData.add("timestamp", timestampISO); - logData.add("sender", "ALARM"); - logData.add("message", messageLog); +// Gestion des mises à jour Firebase +void streamCallback(FirebaseStream data) { + Serial.printf("Firebase Update - Path: %s, Data: %s\n", data.dataPath().c_str(), data.stringData().c_str()); - 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()); - } + if (data.dataPath() == "/armed") { + armed = data.boolData(); + Serial.printf("🔒 Alarme %s\n", armed ? "activée" : "désactivée"); + 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 setup() { @@ -93,30 +77,34 @@ void setup() { Serial.print("."); delay(500); } - Serial.println("\n✅ WiFi connecté ! C'est super"); + Serial.println("\n✅ WiFi connecté !"); - // Configuration Firebase + // Initialisation Firebase config.api_key = API_KEY; + config.database_url = DATABASE_URL; 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); - + // Stream Firebase + if (!Firebase.RTDB.beginStream(&stream, "/Alarm")) { + Serial.printf("🔥 Erreur stream: %s\n", stream.errorReason().c_str()); + } + Firebase.RTDB.setStreamCallback(&stream, streamCallback, nullptr); + Firebase.RTDB.setBool(&fbdo, "/Alarm/triggered", false); + Firebase.RTDB.setBool(&fbdo, "/Alarm/armed", false); + addLog("Alarme connectée"); + // Configuration NTP timeClient.begin(); - timeClient.update(); + // Configuration des capteurs pinMode(A2, INPUT); pinMode(D2, OUTPUT); attachInterrupt(digitalPinToInterrupt(A2), onMotionDetected, RISING); + blinkSlowGreen(); } @@ -124,28 +112,25 @@ void loop() { Firebase.ready(); if (armed) { - blinkSlowRed(); - if (motionDetected) { - motionDetected = false; - Serial.println("🎯 Mouvement détecté !"); - + blinkSlowRed(); + if (motionDetected && !triggered) { + 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(); - } + stopSirene(); + if (!armed) blinkSlowGreen(); + } else { + startSirene(); + blinkFastRed(); + } updateLed(); updateSirene(); diff --git a/ctrlLed.h b/ctrlLed.h index e55fe69..5f39518 100644 --- a/ctrlLed.h +++ b/ctrlLed.h @@ -35,7 +35,7 @@ void updateLed() { break; case SLOW_GREEN: leds.setColorRGB(0, 0, ledOn ? 255 : 0, 0); - blinkInterval = ledOn ? 200 : 800; // Court allumage, longue extinction + blinkInterval = ledOn ? 200 : 1800; // Court allumage, longue extinction break; default: break;