Motion Logic Fusion
Relevant source files
The following files were used as context for generating this wiki page:
The Motion Logic Fusion system is the core of the HomeSecurity project's intrusion detection capability. It utilizes a multi-sensor approach and a two-stage alert mechanism to provide reliable detection while minimizing false alarms. The logic is implemented using a concurrent, real-time architecture based on FreeRTOS, where multiple tasks run in parallel to monitor sensors, process events, and trigger responses.
This system fuses data from a PIR sensor, a Hall effect sensor, and a Microwave (Radar) sensor. An initial alert is triggered by the PIR or Hall sensor, followed by a confirmation alert if the Radar sensor detects sustained movement. Alerts consist of a local audible alarm and remote push notifications sent via Firebase.
System Architecture
The motion detection logic is distributed across several FreeRTOS tasks, each with a specific responsibility and priority. This ensures that critical sensor monitoring and alert processing are handled concurrently and in real-time. The tasks are initialized in main.cpp.
Sources: src/main.cpp:11-16, README.md
FreeRTOS Task Structure
The following table details the tasks involved in the motion logic fusion, their priorities, and their functions.
| Task Name | Priority | Stack Size | Description | Source File |
|---|---|---|---|---|
handleHallMovementTask | 4 (High) | 2048 bytes | Monitors the Hall effect sensor and controls the yellow LED. | src/main.cpp:13 |
handlePirMovementTask | 4 (High) | 2048 bytes | Monitors the PIR sensor and controls the blue LED. | src/main.cpp:14 |
possibleHumanMovementDetectedTask | 4 (High) | 6144 bytes | Implements Stage 1 alert logic. Triggers the alarm and initial notification if PIR or Hall sensor is active. | src/main.cpp:16 |
calibratePirSensorTask | 3 (Medium) | 1536 bytes | Manages the 30-second warm-up period for the PIR sensor. | src/main.cpp:12 |
handleRadarMovementTask | 3 (Medium) | 6144 bytes | Implements Stage 2 alert logic. Monitors the Radar sensor for sustained motion to confirm an intrusion. | src/main.cpp:15 |
This diagram illustrates the creation and scheduling of the tasks by the FreeRTOS scheduler.
Sources: src/main.cpp:11-16
Alert Logic Flow
The system employs a two-stage alert mechanism to enhance accuracy.
Sources: src/motion_logic.cpp, README.md
Stage 1: Initial Detection
The first stage is triggered when either the PIR sensor detects motion or the Hall effect sensor detects a door/window opening. This is handled by the possibleHumanMovementDetectedTask.
- The task continuously checks the status of
isPirActive()andisHallSensorTriggered(). - If either sensor is triggered, the system activates a local, non-blocking audible alarm for 3 seconds (
triggerAlarm()). - Simultaneously, it sends a "Warning" push notification via Firebase with the message "Mișcare detectată".
- A flag (
notificareTrimisa) is set totrueto prevent sending multiple notifications for a single continuous event. This flag is reset once the sensors are no longer active.
Sources: src/motion_logic.cpp:52-80, src/alarm.cpp:19-26, src/firebase_notifier.cpp:7-10
Stage 2: Presence Confirmation
The second stage uses the RCWL-0516 Radar sensor to confirm if the motion is sustained, indicating a higher probability of human presence. This logic resides in handleRadarMovementTask.
- When the radar sensor (
isRadarActive()) becomes active, a timer is started. - If the sensor remains active for a continuous period of 2000 milliseconds, the system confirms the intrusion.
- The red LED is turned on to provide a local visual indicator of the confirmed threat.
- A "Confirmation" push notification is sent via Firebase with the message "Mișcare confirmată".
- Similar to Stage 1, a flag prevents repeated notifications for the same event.
Sources: src/motion_logic.cpp:113-139, src/microwave_sensor.cpp:8-17
Overall Flow Diagram
This flowchart shows the complete decision-making process from initial calibration to a confirmed alert.
Components and Implementation
Sensor Handling
Each sensor's state is read by a dedicated function. The core logic relies on these boolean functions to make decisions.
| Function | Sensor | Description | Source File |
|---|---|---|---|
isPirActive() | PIR (HC-SR505) | Returns true if motion is detected, but only after the 30-second calibration is complete. | src/pir_sensor.cpp:20-30 |
isHallSensorTriggered() | Hall Effect (KY-003) | Returns true if the magnetic field is detected (door/window is likely open). Uses INPUT_PULLUP. | src/hall_sensor.cpp:7-17 |
isRadarActive() | Microwave (RCWL-0516) | Returns true if the Doppler radar detects movement. | src/microwave_sensor.cpp:8-17 |
PIR Sensor Calibration
The system will not process PIR events until a 30-second calibration period has passed. The calibratePirSensorTask manages this, and upon completion, it sets the pirCalibrated flag to true and turns on the green LED to indicate the system is armed and ready.
Sources: src/pir_sensor.cpp:11-18, include/pir_sensor.h:11
Non-Blocking Alarm
The audible alarm is designed to be non-blocking to avoid halting other critical tasks.
triggerAlarm(): When called, it sets theisAlarmActiveflag, records themillis()start time, and turns the buzzerHIGH. It does not wait.handleAlarm(): This function is called repeatedly within thepossibleHumanMovementDetectedTaskloop. It checks if the alarm is active and if 3000ms (ALARM_DURATION) have elapsed since it started. If so, it turns the buzzerLOWand resets the flag.
This implementation ensures the buzzer sounds for a fixed duration without using delay() or vTaskDelay().
Sources: src/alarm.cpp:6-30, include/alarm.h:7
Firebase Notification
Notifications are sent by making an HTTPS POST request to a Google Cloud Function.
The FirebaseNotifier class encapsulates this functionality.
- Constructor:
FirebaseNotifier(const String& functionUrl)takes the cloud function URL. - Method:
sendNotification(const String& title, const String& body, const String& token)builds a JSON payload and sends it.
The client uses WiFiClientSecure but disables strict certificate validation with client.setInsecure() for simplicity.
Sources: src/firebase_notifier.cpp:4-31, include/firebase_notifier.h:9-13
The following sequence diagram illustrates the process of sending a notification upon initial motion detection.
Summary
The Motion Logic Fusion system is a robust, multi-tasking implementation for home security. By leveraging FreeRTOS, it achieves concurrent sensor monitoring and responsive, non-blocking alerts. The two-stage verification process, which combines fast-reacting PIR/Hall sensors with a confirmation-based Radar sensor, is a key design choice aimed at reducing false positives. The integration with Firebase for push notifications extends the system's utility by providing immediate remote alerts to the user.