Firebase Cloud Messaging (FCM)
Relevant source files
The following files were used as context for generating this wiki page:The Firebase Cloud Messaging (FCM) integration is a core component of the HomeSecurity project, responsible for delivering real-time push notifications to the user's Android mobile application. When the ESP32 detects a potential intrusion event, it triggers a notification that is sent via a serverless Firebase Cloud Function, which then uses FCM to alert the user on their device. This mechanism provides immediate awareness of security events, forming the primary communication channel between the edge device and the client application.
The system is designed to send two distinct types of alerts: an initial warning when motion is first detected, and a confirmation alert if movement persists, which helps in reducing false alarms. This functionality is encapsulated within the FirebaseNotifier class and orchestrated by the motion detection logic tasks running on the ESP32.
Sources: HomeSecurity/README.md, HomeSecurity/src/motion_logic.cpp:70-77, HomeSecurity/src/motion_logic.cpp:111-118
System Architecture
The notification system follows a three-tier architecture, where the ESP32 acts as the edge device, Firebase serves as the cloud backend, and the Android app is the client. The ESP32 does not communicate with FCM directly. Instead, it sends an HTTPS POST request to a specific Firebase Cloud Function, which then orchestrates the delivery of the push notification to the registered mobile device via the FCM service.
Sources: HomeSecurity/README.md
This diagram illustrates the high-level data flow for sending a notification.
FirebaseNotifier Class
The FirebaseNotifier class abstracts the complexity of making HTTPS requests to the Firebase Cloud Function. It is responsible for constructing the JSON payload and handling the HTTP communication.
Sources: HomeSecurity/include/firebase_notifier.h, HomeSecurity/src/firebase_notifier.cpp
Class Diagram
The following diagram shows the structure of the FirebaseNotifier class.
Methods
| Method | Description |
|---|---|
FirebaseNotifier(const String& functionUrl) | Constructor that initializes the notifier with the target Firebase Cloud Function URL. |
bool sendNotification(const String& title, const String& body, const String& token) | Constructs and sends a JSON payload via HTTPS POST to the configured URL. Returns true on success (HTTP 200) and false on failure. |
Sources: HomeSecurity/include/firebase_notifier.h:12-13, HomeSecurity/src/firebase_notifier.cpp:4-28
Configuration
The FirebaseNotifier is instantiated globally in motion_logic.cpp. The configuration requires two key pieces of information: the Cloud Function URL and the target device's FCM token.
| Parameter | File | Description |
|---|---|---|
FirebaseNotifier notifier(...) | motion_logic.cpp | The URL of the Firebase Cloud Function that acts as the notification gateway. |
fcmToken | motion_logic.cpp | The FCM registration token of the client Android device that will receive the notification. |
Sources: HomeSecurity/src/motion_logic.cpp:4, HomeSecurity/src/motion_logic.cpp:7
// File: HomeSecurity/src/motion_logic.cpp:4-7
FirebaseNotifier notifier("https://trimitenotificare-k4cqa7es6q-uc.a.run.app");
// ...
const char* fcmToken = "d-OlFNUNSaSStUUsL05ofB:APA91bEd8ifGV-IIX15CJZHAbQovskC4MzL32mM5iDDON5GwLeoFdeWnYgYar0-lrmNf4lxb0AK54yycQB8TD_DMpMU4LOOp-UwBM_8Bi4DTAi1pYVpuNbA";
Notification Logic
Notifications are triggered from two separate FreeRTOS tasks based on sensor inputs, implementing a two-stage alert system.
Sources: HomeSecurity/src/motion_logic.cpp:60-123, HomeSecurity/src/main.cpp:13-14
Stage 1: Initial Motion Alert
The possibleHumanMovementDetectedTask is responsible for sending the first warning. A notification is sent if either the PIR sensor or the Hall effect sensor is triggered. A flag (notificareTrimisa) ensures that only one notification is sent per event to avoid spamming the user.
Sources: HomeSecurity/src/motion_logic.cpp:60-84
Stage 2: Confirmed Presence Alert
The handleRadarMovementTask sends a confirmation alert. If the microwave radar sensor detects continuous movement for 2 seconds or more, it sends a distinct notification indicating confirmed presence. This helps validate the initial alert and provides a higher degree of certainty about an intrusion.
Sources: HomeSecurity/src/motion_logic.cpp:101-123
Notification Flowchart
This flowchart details the logic for triggering the two stages of notifications.
Communication Protocol
The sendNotification method uses the HTTPClient library to perform a secure POST request to the Firebase Cloud Function.
Sequence Diagram
The sequence of operations when sending a notification is as follows:
JSON Payload
The sendNotification function dynamically creates a JSON string to be sent as the body of the POST request. The payload includes the notification title, body, and the device's FCM token.
// File: HomeSecurity/src/firebase_notifier.cpp:17
String jsonPayload = "{\"title\":\"" + title + "\",\"body\":\"" + body + "\",\"token\":\"" + token + "\"}";
The corresponding JSON structure expected by the cloud function is:
{
"title": "Mișcare detectată",
"body": "Sistemul a detectat o posibilă mișcare umană.",
"token": "your-device-fcm-token"
}
Sources: HomeSecurity/src/firebase_notifier.cpp:17, HomeSecurity/README.md
Summary
The FCM integration is a critical feature that bridges the gap between the hardware-level event detection on the ESP32 and the user-facing mobile application. By leveraging a FirebaseNotifier class and a serverless Cloud Function, the system reliably delivers immediate, two-stage security alerts. This architecture is efficient, scalable, and central to the project's goal of providing real-time remote monitoring.
Sources: HomeSecurity/README.md