118 lines
3.0 KiB
C++
118 lines
3.0 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>
|
|
|
|
#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;
|
|
|
|
// 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");
|
|
}
|
|
if (data.dataPath() == "/triggered") {
|
|
triggered = data.boolData();
|
|
Serial.printf("🚨 Alarme déclenchée: %s\n", triggered ? "OUI" : "NON");
|
|
}
|
|
}
|
|
|
|
void streamTimeoutCallback(bool timeout) {
|
|
if (timeout)
|
|
Serial.println("⏳ Stream timeout, reconnexion...");
|
|
}
|
|
|
|
// Détection mouvement
|
|
void IRAM_ATTR onMotionDetected() {
|
|
motionDetected = true;
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
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;
|
|
|
|
String logEntry = "Mouvement détecté à " + String(millis() / 1000) + "s";
|
|
Firebase.RTDB.setString(&fbdo, "/Alarm/logs", logEntry);
|
|
}
|
|
}
|
|
|
|
if (!triggered) {
|
|
stopSirene();
|
|
blinkSlowGreen();
|
|
}else{
|
|
startSirene();
|
|
blinkFastRed();
|
|
}
|
|
|
|
updateLed();
|
|
updateSirene();
|
|
}
|