Alert Logic Flow
Relevant source files
The following files were used as context for generating this wiki page:The HomeSecurity system employs a multi-layered, two-stage alert logic to detect potential intrusions while minimizing false alarms. This is achieved by fusing data from three different sensors (Hall effect, PIR, and Microwave Radar) and orchestrating responses using a real-time operating system (FreeRTOS). The system is designed to provide an immediate local and remote warning upon initial detection, followed by a confirmation alert if sustained presence is verified.
The core of this logic is managed through several concurrent tasks that monitor sensors, handle alarms, and communicate with a cloud backend for push notifications. This distributed approach ensures that sensor monitoring and alert handling are non-blocking and responsive.
Sources: HomeSecurity/README.md, HomeSecurity/src/motion_logic.cpp
System Initialization and Task Structure
On startup, the system initializes all sensors, the alarm buzzer, and the LEDs. It then connects to WiFi and creates several FreeRTOS tasks to manage the security logic concurrently. A critical step in the initialization is the 30-second calibration period for the PIR sensor, during which the system is not yet armed. The successful completion of this calibration is indicated by the green LED turning on.
Sources: HomeSecurity/src/main.cpp:6-8, HomeSecurity/src/pir_sensor.cpp:12-19
FreeRTOS Task Allocation
The alert system's logic is distributed across multiple tasks with different priorities to ensure real-time responsiveness.
This diagram shows the FreeRTOS tasks responsible for the alert logic and their priorities.
Sources: HomeSecurity/src/main.cpp:10-15
Task Responsibilities
| Task | Priority | Stack Size | Description | Source File |
|---|---|---|---|---|
possibleHumanMovementDetectedTask | 4 | 6144B | Orchestrates the Stage 1 alert by checking PIR and Hall sensors, triggering the buzzer, and sending the initial notification. | main.cpp:15 |
handleHallMovementTask | 4 | 2048B | Monitors the Hall effect sensor and controls the yellow LED. | main.cpp:12 |
handlePirMovementTask | 4 | 2048B | Monitors the PIR sensor and controls the blue LED. | main.cpp:13 |
handleRadarMovementTask | 3 | 6144B | Manages the Stage 2 confirmation logic by monitoring the microwave radar sensor for sustained activity. | main.cpp:14 |
calibratePirSensorTask | 3 | 1536B | Manages the 30-second PIR sensor calibration period and activates the green LED when ready. | main.cpp:11 |
WiFiCheckTask | 2 | 2048B | Periodically checks the WiFi connection and attempts to reconnect if lost. | main.cpp:10 |
Sources: HomeSecurity/src/main.cpp:10-15, HomeSecurity/src/motion_logic.cpp, HomeSecurity/src/pir_sensor.cpp
Two-Stage Alert Mechanism
The system's core strength is its two-stage alert process, which is designed to provide a rapid initial warning and a verified secondary confirmation.
Stage 1: Initial Detection and Alert
The first stage is triggered if either the PIR sensor (detecting infrared motion) or the Hall effect sensor (detecting a door/window opening) becomes active.
- The
possibleHumanMovementDetectedTaskcontinuously checks the status ofisPirActive()andisHallSensorTriggered(). - If either sensor is triggered, the system immediately executes two actions:
- Local Alarm: The
triggerAlarm()function is called, which activates the buzzer for a non-blocking duration of 3000ms. - Remote Notification: The
FirebaseNotifiersends a push notification with the title "Mișcare detectată" to the registered mobile device.
- Local Alarm: The
- A flag (
notificareTrimisa) ensures that this alert is sent only once per trigger event to avoid spamming. - Corresponding LEDs (Blue for PIR, Yellow for Hall) are lit to provide a visual cue of the trigger source.
Sources: HomeSecurity/src/motion_logic.cpp:48-73, HomeSecurity/src/alarm.cpp:17-24, HomeSecurity/include/alarm.h:6
// Snippet from Stage 1 logic
void possibleHumanMovementDetectedTask(void *parameter) {
static bool notificareTrimisa = false;
for (;;) {
handleAlarm(); // Manages non-blocking alarm timeout
bool pirActive = isPirActive();
bool hallActive = isHallSensorTriggered();
if (pirActive || hallActive) {
if (!notificareTrimisa) {
Serial.println("[ALERT] Posibilă mișcare umană detectată!");
triggerAlarm();
notifier.sendNotification(
"Mișcare detectată",
"Sistemul a detectat o posibilă mișcare umană.",
fcmToken
);
notificareTrimisa = true;
}
} else {
notificareTrimisa = false;
}
vTaskDelay(pdMS_TO_TICKS(50));
}
}
Source: HomeSecurity/src/motion_logic.cpp:48-73
Stage 2: Presence Confirmation with Radar
The second stage acts as a verification layer to confirm if the initial trigger corresponds to a sustained human presence, thereby reducing false alarms from brief, non-threatening events.
- The
handleRadarMovementTaskruns in parallel, independently monitoring the RCWL-0516 microwave radar sensor. - If the radar detects motion (
isRadarActive()returns true), a timer (radarStartTime) is initiated. - If the radar detects continuous motion for a threshold of 2000ms, the system concludes that a human presence is confirmed.
- Upon confirmation, the system executes two actions:
- Visual Indicator: The red LED is turned on (
turnRedLedOn()). - Confirmation Notification: A second, distinct push notification is sent with the title "Mișcare confirmată" and the body "Radarul a detectat o mișcare continuă."
- Visual Indicator: The red LED is turned on (
- This confirmation logic is also debounced to ensure only one confirmation notification is sent per continuous event.
Sources: HomeSecurity/src/motion_logic.cpp:103-128, HomeSecurity/src/microwave_sensor.cpp:9-17
// Snippet from Stage 2 logic
void handleRadarMovementTask(void *parameter) {
static unsigned long radarStartTime = 0;
static bool notificareTrimisa = false;
for (;;) {
if (isRadarActive()) {
if (radarStartTime == 0) {
radarStartTime = millis(); // Start timer
}
if (millis() - radarStartTime >= 2000) { // Check for 2s of motion
turnRedLedOn();
if (!notificareTrimisa) {
notifier.sendNotification(
"Mișcare confirmată",
"Radarul a detectat o mișcare continuă.",
fcmToken
);
notificareTrimisa = true;
}
}
} else {
radarStartTime = 0; // Reset timer
notificareTrimisa = false;
turnRedLedOff();
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}
Source: HomeSecurity/src/motion_logic.cpp:103-128
Overall Alert Flow Diagram
The following diagram illustrates the complete alert sequence from initial trigger to final confirmation.
Notification Subsystem
Notifications are sent to a mobile client via Firebase Cloud Messaging (FCM). The ESP32 does not communicate with FCM directly; instead, it makes an HTTP POST request to a serverless Google Cloud Function, which then orchestrates the push notification delivery.
This diagram shows the sequence of events for sending a push notification.
Sources: HomeSecurity/src/firebase_notifier.cpp:6-30, HomeSecurity/src/motion_logic.cpp:4-7
The FirebaseNotifier class encapsulates this functionality. It constructs a JSON payload and sends it to the predefined function URL.
FirebaseNotifier Parameter | Description | Source |
|---|---|---|
functionUrl | The URL of the Google Cloud Function endpoint. | firebase_notifier.cpp:3 |
title | The title of the push notification. | firebase_notifier.cpp:6 |
body | The main content of the push notification. | firebase_notifier.cpp:6 |
token | The FCM registration token of the target mobile device. | firebase_notifier.cpp:6 |
Sources: HomeSecurity/src/firebase_notifier.cpp, HomeSecurity/src/motion_logic.cpp
Summary
The Alert Logic Flow in the HomeSecurity project is a robust, event-driven system that leverages FreeRTOS for concurrent processing and a two-stage sensor fusion approach for reliable intrusion detection. By combining an immediate but general alert (Stage 1) with a delayed but specific confirmation (Stage 2), the system effectively balances responsiveness with the need to reduce false positives. The integration with Firebase for remote notifications makes it a complete and modern IoT security solution.
Sources: HomeSecurity/README.md, HomeSecurity/src/motion_logic.cpp