Firebase Cloud Functions
Relevant source files
The following files were used as context for generating this wiki page:
The Firebase Cloud Function serves as a serverless backend component in the HomeSecurity system. Its primary role is to act as a secure gateway between the ESP32 microcontroller and the Firebase Cloud Messaging (FCM) service. Instead of the ESP32 communicating directly with FCM, it sends a simple HTTP request to the Cloud Function, which then orchestrates the delivery of a push notification to the user's Android device.
This serverless architecture simplifies the logic on the ESP32, offloads authentication and complex API interactions to the cloud, and enhances security by abstracting away sensitive service credentials from the edge device.
Sources: HomeSecurity/README.md:29-53, 303-311
System Architecture
The Cloud Function is a critical component of the Cloud Tier, bridging the Edge Tier (ESP32) and the Client Tier (Android App). The ESP32 does not directly send push notifications; it triggers the Cloud Function, which in turn uses FCM to deliver the alert.
This diagram illustrates the notification data flow from the ESP32 to the mobile application.
ESP32 Implementation
On the ESP32, interaction with the Firebase Cloud Function is managed by the FirebaseNotifier class. This class encapsulates the logic for making secure HTTP POST requests to the function's endpoint.
Sources: HomeSecurity/include/firebase_notifier.h, HomeSecurity/src/firebase_notifier.cpp
FirebaseNotifier Class
The FirebaseNotifier class provides a straightforward interface for sending notifications.
This class diagram shows the structure of the FirebaseNotifier class.
Configuration
The FirebaseNotifier is instantiated in motion_logic.cpp with a hardcoded URL for the deployed Cloud Function. The FCM token for the target device is also hardcoded in this file.
// File: HomeSecurity/src/motion_logic.cpp
FirebaseNotifier notifier("https://trimitenotificare-k4cqa7es6q-uc.a.run.app");
const char* fcmToken = "d-OlFNUNSaSStUUsL05ofB:APA91bEd8ifGV-IIX15CJZHAbQovskC4MzL32mM5iDDON5GwLeoFdeWnYgYar0-lrmNf4lxb0AK54yycQB8TD_DMpMU4LOOp-UwBM_8Bi4DTAi1pYVpuNbA";
Sources: HomeSecurity/src/motion_logic.cpp:4, 7
HTTP Request Logic
The sendNotification method constructs and sends the HTTP request.
- It establishes a secure connection using
WiFiClientSecure. Note that it usesclient.setInsecure()which is not recommended for production environments. - It initializes an
HTTPClientinstance with the function URL. - It sets the
Content-Typeheader toapplication/json. - It constructs a JSON payload containing the notification
title,body, and the devicetoken. - It sends the payload via an HTTP POST request.
- It returns
trueif the HTTP response code is 200 (OK), indicating success.
// File: HomeSecurity/src/firebase_notifier.cpp
bool FirebaseNotifier::sendNotification(const String& title, const String& body, const String& token) {
WiFiClientSecure client;
client.setInsecure(); // ⚠️ folosește cu grijă în producție
HTTPClient https;
if (!https.begin(client, url)) {
// ... error handling
return false;
}
https.addHeader("Content-Type", "application/json");
String jsonPayload = "{\"title\":\"" + title + "\",\"body\":\"" + body + "\",\"token\":\"" + token + "\"}";
int httpCode = https.POST(jsonPayload);
if (httpCode > 0) {
// ... success handling
return (httpCode == 200);
} else {
// ... error handling
return false;
}
}
Sources: HomeSecurity/src/firebase_notifier.cpp:6-28
Payload Structure
The HTTP POST request sent to the Cloud Function contains a JSON body with the following structure:
| Field | Type | Description |
|---|---|---|
title | String | The title of the push notification. |
body | String | The main content/message of the notification. |
token | String | The FCM registration token of the target device. |
Sources: HomeSecurity/src/firebase_notifier.cpp:16, HomeSecurity/README.md:313-317
Notification Triggers
Notifications are triggered from two separate FreeRTOS tasks based on sensor events, implementing a two-stage alert system.
Sources: HomeSecurity/src/main.cpp:12-13, HomeSecurity/src/motion_logic.cpp
Stage 1: Initial Alert
The possibleHumanMovementDetectedTask sends an initial warning notification when either the PIR sensor or the Hall effect sensor is triggered.
- Title: "Mișcare detectată"
- Body: "Sistemul a detectat o posibilă mișcare umană."
Sources: HomeSecurity/src/motion_logic.cpp:73-77
Stage 2: Confirmed Alert
The handleRadarMovementTask sends a confirmation notification if the microwave radar sensor detects continuous movement for more than 2 seconds.
- Title: "Mișcare confirmată"
- Body: "Radarul a detectat o mișcare continuă."
Sources: HomeSecurity/src/motion_logic.cpp:111-115
Sequence Diagram
This diagram shows the sequence of events when a PIR or Hall sensor triggers an initial alert.
Summary
The Firebase Cloud Function is a vital architectural element that decouples the ESP32 from the complexities of the Firebase Cloud Messaging service. It provides a simple, scalable, and secure HTTP endpoint for the embedded device to trigger push notifications. This design keeps the device-side code minimal and delegates the responsibility of notification delivery to a robust, serverless cloud environment.