Firebase Project Setup
Relevant source files
The following files were used as context for generating this wiki page:The HomeSecurity project utilizes Firebase as its cloud tier for user authentication, serverless logic, and orchestrating real-time push notifications. The ESP32 device communicates with a Firebase Cloud Function, which in turn uses Firebase Cloud Messaging (FCM) to send alerts to the companion Android application. This serverless architecture provides a scalable and robust backend for the IoT system.
Sources: HomeSecurity/README.md
System Architecture
The project is built on a three-tier architecture: Edge (ESP32), Cloud (Firebase), and Client (Android App). Firebase acts as the central hub, decoupling the hardware from the user-facing application. When the ESP32 detects a potential intrusion, it does not communicate with the mobile app directly. Instead, it sends an HTTP request to a specific Firebase Cloud Function endpoint. This function is responsible for constructing and dispatching a push notification via FCM to the registered mobile device.
Sources: HomeSecurity/README.md
The following diagram illustrates the notification data flow:
This diagram shows the flow of a notification from the ESP32 to the Android App via Firebase.
Sources: HomeSecurity/README.md, HomeSecurity/src/firebase_notifier.cpp:11
ESP32 Firebase Client (FirebaseNotifier)
Communication with Firebase from the ESP32 is managed by the FirebaseNotifier class. This class encapsulates the logic for sending secure HTTP POST requests to the designated Firebase Cloud Function.
Sources: HomeSecurity/include/firebase_notifier.h, HomeSecurity/src/firebase_notifier.cpp
Class Definition
The FirebaseNotifier class provides a simple interface for sending notifications.
This diagram shows the structure of the FirebaseNotifier class.
Sources: HomeSecurity/include/firebase_notifier.h:8-14
Configuration
The configuration for the Firebase integration is hardcoded within the firmware. To set up the project, two key values must be defined in src/motion_logic.cpp.
| Parameter | File | Line | Description |
|---|---|---|---|
| Cloud Function URL | src/motion_logic.cpp | 4 | The endpoint URL for the Firebase Cloud Function that handles notifications. |
| FCM Token | src/motion_logic.cpp | 7 | The Firebase Cloud Messaging token of the target device to receive notifications. |
Sources: HomeSecurity/src/motion_logic.cpp:4-7
The FirebaseNotifier object is instantiated with the specific Cloud Function URL:
// File: src/motion_logic.cpp:4
FirebaseNotifier notifier("https://trimitenotificare-k4cqa7es6q-uc.a.run.app");
// File: src/motion_logic.cpp:7
const char* fcmToken = "d-OlFNUNSaSStUUsL05ofB:APA91bEd8ifGV-IIX15CJZHAbQovskC4MzL32mM5iDDON5GwLeoFdeWnYgYar0-lrmNf4lxb0AK54yycQB8TD_DMpMU4LOOp-UwBM_8Bi4DTAi1pYVpuNbA";
Sources: HomeSecurity/src/motion_logic.cpp:4-7
Communication and Payload
The sendNotification method constructs and sends an HTTPS POST request. It uses WiFiClientSecure but disables certificate validation via client.setInsecure(), which is noted in the source code as a potential security risk for production environments.
The body of the POST request is a JSON object containing the notification's title, body, and the target device's FCM token.
// File: src/firebase_notifier.cpp:7-8
WiFiClientSecure client;
client.setInsecure(); // ⚠️ folosește cu grijă în producție
Sources: HomeSecurity/src/firebase_notifier.cpp:7-8
The JSON payload has the following structure:
{
"title": "Mișcare detectată",
"body": "Sistemul a detectat o posibilă mișcare umană.",
"token": "your-device-fcm-token"
}
This structure is dynamically created by the sendNotification function.
Sources: HomeSecurity/README.md, HomeSecurity/src/firebase_notifier.cpp:16
Notification Trigger Logic
Notifications are not sent for every sensor event. The logic is embedded in the FreeRTOS tasks defined in motion_logic.cpp and created in main.cpp. There are two distinct events that trigger a Firebase notification.
Sources: HomeSecurity/src/motion_logic.cpp, HomeSecurity/src/main.cpp:12-13
Stage 1: Initial Motion Alert
The possibleHumanMovementDetectedTask task monitors the PIR and Hall sensors. If either sensor is triggered, it calls notifier.sendNotification to send an initial warning. A flag (notificareTrimisa) ensures that only one notification is sent per continuous event to avoid spamming the user.
- Title: "Mișcare detectată"
- Body: "Sistemul a detectat o posibilă mișcare umană."
Sources: HomeSecurity/src/motion_logic.cpp:69-75
Stage 2: Radar Confirmation Alert
The handleRadarMovementTask task provides a second layer of validation. If the microwave radar sensor detects continuous movement for at least 2 seconds, it sends a confirmation notification. This helps distinguish sustained presence from a brief, potentially false, trigger.
- Title: "Mișcare confirmată"
- Body: "Radarul a detectat o mișcare continuă."
Sources: HomeSecurity/src/motion_logic.cpp:113-117
The following sequence diagram illustrates the process for an initial motion alert.
This diagram shows the sequence of events from sensor detection to a successful call to the Firebase Cloud Function.
Sources: HomeSecurity/src/motion_logic.cpp:60-82, HomeSecurity/src/firebase_notifier.cpp:6-26
Dependencies
The project relies on the Firebase Arduino Client Library for ESP8266 and ESP32. This library is specified in the platformio.ini file and is automatically installed by PlatformIO.
- Library:
Firebase Arduino Client Library for ESP8266 and ESP32 v4.4.17
Sources: HomeSecurity/README.md