Lien Firebase OK, mais : Erreur si trop de log, led qui ne clignotte pas à la bonne vitesse
This commit is contained in:
parent
f5d62be017
commit
719ff30199
103
AlarmeESP32.ino
103
AlarmeESP32.ino
@ -7,80 +7,64 @@
|
|||||||
#include <branchements.h>
|
#include <branchements.h>
|
||||||
#include <NTPClient.h>
|
#include <NTPClient.h>
|
||||||
|
|
||||||
|
// WiFi Configuration
|
||||||
#define WIFI_SSID "ratio"
|
#define WIFI_SSID "ratio"
|
||||||
#define WIFI_PASSWORD "123456789"
|
#define WIFI_PASSWORD "123456789"
|
||||||
|
|
||||||
|
// Firebase Configuration
|
||||||
#define API_KEY "AIzaSyB8qf7aeI7F5l7d1NDhRQrNNLW8aPaOkl4"
|
#define API_KEY "AIzaSyB8qf7aeI7F5l7d1NDhRQrNNLW8aPaOkl4"
|
||||||
#define DATABASE_URL "https://alarmeesp32-2ca19-default-rtdb.europe-west1.firebasedatabase.app"
|
#define DATABASE_URL "https://alarmeesp32-2ca19-default-rtdb.europe-west1.firebasedatabase.app"
|
||||||
#define USER_EMAIL "alarm@alarm.bip"
|
#define USER_EMAIL "alarm@alarm.bip"
|
||||||
#define USER_PASSWORD "123456"
|
#define USER_PASSWORD "123456"
|
||||||
|
#define FIREBASE_USE_PSRAM
|
||||||
|
|
||||||
// Déclaration des objets Firebase
|
// Déclaration Firebase
|
||||||
FirebaseData stream;
|
FirebaseData fbdo, stream;
|
||||||
FirebaseData fbdo;
|
|
||||||
FirebaseAuth auth;
|
FirebaseAuth auth;
|
||||||
FirebaseConfig config;
|
FirebaseConfig config;
|
||||||
|
|
||||||
// Variables de gestion de l'alarme
|
// Variables Alarme
|
||||||
volatile bool motionDetected = false;
|
volatile bool motionDetected = false;
|
||||||
bool armed = false;
|
bool armed = false, triggered = false;
|
||||||
bool triggered = false;
|
|
||||||
|
|
||||||
|
// NTP pour la gestion des logs
|
||||||
WiFiUDP ntpUDP;
|
WiFiUDP ntpUDP;
|
||||||
NTPClient timeClient(ntpUDP, "pool.ntp.org");
|
NTPClient timeClient(ntpUDP, "pool.ntp.org");
|
||||||
|
|
||||||
// Stream Firebase
|
// Gestion de l'alarme
|
||||||
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() {
|
void IRAM_ATTR onMotionDetected() {
|
||||||
motionDetected = true;
|
motionDetected = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ajout de logs Firebase
|
||||||
void addLog(const char* messageLog) {
|
void addLog(const char* messageLog) {
|
||||||
FirebaseJson logData;
|
FirebaseJson logData;
|
||||||
|
|
||||||
timeClient.update();
|
timeClient.update();
|
||||||
time_t rawTime = timeClient.getEpochTime();
|
|
||||||
struct tm* timeinfo;
|
|
||||||
timeinfo = gmtime(&rawTime);
|
|
||||||
char timestampISO[25];
|
char timestampISO[25];
|
||||||
strftime(timestampISO, sizeof(timestampISO), "%Y-%m-%dT%H:%M:%SZ", timeinfo);
|
time_t epochTime = timeClient.getEpochTime();
|
||||||
|
strftime(timestampISO, sizeof(timestampISO), "%Y-%m-%dT%H:%M:%SZ", gmtime(&epochTime));
|
||||||
|
|
||||||
logData.add("timestamp", timestampISO);
|
logData.add("timestamp", timestampISO);
|
||||||
logData.add("sender", "ALARM");
|
logData.add("sender", "ALARM");
|
||||||
logData.add("message", messageLog);
|
logData.add("message", messageLog);
|
||||||
|
|
||||||
if (Firebase.RTDB.pushJSON(&fbdo, "/Alarm/logs", &logData)) {
|
if (!Firebase.RTDB.pushJSON(&fbdo, "/Alarm/logs", &logData)) {
|
||||||
Serial.println("Log ajouté avec succès à Firebase avec timestamp");
|
Serial.println("❌ Erreur ajout log Firebase : " + fbdo.errorReason());
|
||||||
} else {
|
}
|
||||||
Serial.print("Erreur lors de l'ajout du log avec timestamp à Firebase: ");
|
}
|
||||||
Serial.println(fbdo.errorReason());
|
|
||||||
|
// 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 (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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,30 +77,34 @@ void setup() {
|
|||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
delay(500);
|
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.api_key = API_KEY;
|
||||||
|
config.database_url = DATABASE_URL;
|
||||||
auth.user.email = USER_EMAIL;
|
auth.user.email = USER_EMAIL;
|
||||||
auth.user.password = USER_PASSWORD;
|
auth.user.password = USER_PASSWORD;
|
||||||
config.database_url = DATABASE_URL;
|
|
||||||
|
|
||||||
Firebase.begin(&config, &auth);
|
Firebase.begin(&config, &auth);
|
||||||
Firebase.reconnectWiFi(true);
|
Firebase.reconnectWiFi(true);
|
||||||
|
|
||||||
// Initialisation du streaming Firebase
|
// Stream Firebase
|
||||||
if (!Firebase.RTDB.beginStream(&stream, "/Alarm"))
|
if (!Firebase.RTDB.beginStream(&stream, "/Alarm")) {
|
||||||
Serial.printf("🔥 Erreur de stream: %s\n", stream.errorReason().c_str());
|
Serial.printf("🔥 Erreur stream: %s\n", stream.errorReason().c_str());
|
||||||
Firebase.RTDB.setStreamCallback(&stream, streamCallback, streamTimeoutCallback);
|
}
|
||||||
|
Firebase.RTDB.setStreamCallback(&stream, streamCallback, nullptr);
|
||||||
|
Firebase.RTDB.setBool(&fbdo, "/Alarm/triggered", false);
|
||||||
|
Firebase.RTDB.setBool(&fbdo, "/Alarm/armed", false);
|
||||||
|
|
||||||
addLog("Alarme connectée");
|
addLog("Alarme connectée");
|
||||||
|
|
||||||
|
// Configuration NTP
|
||||||
timeClient.begin();
|
timeClient.begin();
|
||||||
timeClient.update();
|
|
||||||
|
|
||||||
|
// Configuration des capteurs
|
||||||
pinMode(A2, INPUT);
|
pinMode(A2, INPUT);
|
||||||
pinMode(D2, OUTPUT);
|
pinMode(D2, OUTPUT);
|
||||||
attachInterrupt(digitalPinToInterrupt(A2), onMotionDetected, RISING);
|
attachInterrupt(digitalPinToInterrupt(A2), onMotionDetected, RISING);
|
||||||
|
|
||||||
blinkSlowGreen();
|
blinkSlowGreen();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,23 +113,20 @@ void loop() {
|
|||||||
|
|
||||||
if (armed) {
|
if (armed) {
|
||||||
blinkSlowRed();
|
blinkSlowRed();
|
||||||
if (motionDetected) {
|
if (motionDetected && !triggered) {
|
||||||
motionDetected = false;
|
motionDetected = false;
|
||||||
Serial.println("🎯 Mouvement détecté !");
|
Serial.println("🎯 Mouvement détecté !");
|
||||||
|
|
||||||
blinkFastRed();
|
blinkFastRed();
|
||||||
startSirene();
|
startSirene();
|
||||||
|
|
||||||
Firebase.RTDB.setBool(&fbdo, "/Alarm/triggered", true);
|
Firebase.RTDB.setBool(&fbdo, "/Alarm/triggered", true);
|
||||||
triggered = true;
|
triggered = true;
|
||||||
|
|
||||||
addLog("Mouvement détecté, alarme déclenchée");
|
addLog("Mouvement détecté, alarme déclenchée");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!triggered) {
|
if (!triggered) {
|
||||||
stopSirene();
|
stopSirene();
|
||||||
blinkSlowGreen();
|
if (!armed) blinkSlowGreen();
|
||||||
} else {
|
} else {
|
||||||
startSirene();
|
startSirene();
|
||||||
blinkFastRed();
|
blinkFastRed();
|
||||||
|
@ -35,7 +35,7 @@ void updateLed() {
|
|||||||
break;
|
break;
|
||||||
case SLOW_GREEN:
|
case SLOW_GREEN:
|
||||||
leds.setColorRGB(0, 0, ledOn ? 255 : 0, 0);
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user