To avoid missing my updates, remember to check the public account in the upper right corner and set it as a star to send me a star.



Features of the Raspberry Pi Security System

System Hardware/Software Components
1) Raspberry Pi with a camera interface, this project uses Raspberry Pi 1 Model A+ (can be replaced with a newer version of Raspberry Pi);
2) Raspberry Pi camera module;
3) USB Wi-Fi/WLAN adapter that supports monitor mode, this project uses an adapter based on RT5370, mainly due to their low price and easy availability;
4) The external frame is made of Makerbeam (an open-source aluminum DIY mechanical frame previously introduced in other projects), using 4 aluminum pillars of 66mm and 8 aluminum pillars of 54mm. The Raspberry Pi and camera module are fixed to the external aluminum frame with 3D printed structures.
For software, you need to use Telegram bot, an open-source, non-profit, cloud-based lightweight instant messaging software.Telegram supports clients for Android, iOS, Windows Phone, macOS, and Linux, currently with very few users in China, it is recommended to use a more common communication method.

System Operating Modes
The goal of automatic presence detection is to fully automate the system, not just to arm or disarm when leaving or returning home. The simplest way to achieve this is to try to detect the mobile phones of family members. Conceptually, this is simple, but in practice, it is the most challenging part for several reasons:
1) Capturing all packets on the Wi-Fi interface consumes a lot of resources.
2) Currently, there are no high-quality 5GHz USB Wi-Fi adapters that support monitor mode. This means packet monitoring is limited to 2.4GHz, while most modern phones now use the 5GHz band.
3) Mobile phones are not always online and sending packets via Wi-Fi. Sometimes they can be offline for 15 minutes or longer.
4) Even with an accuracy of 99%, false alarms can be annoying.
After extensive testing, a method based on known mobile phone MAC addresses is used, combining active (ARP scanning) and passive (packet capturing) detection via the Wi-Fi adapter.
The MAC addresses of mobile phones are set in the configuration, and the rpi-security application uses the following filters to capture packets in monitor mode:
a) Wi-Fi probe requests from any configured MAC address; b) all packets from the configured MAC address to the host running rpi-security.
When packets are detected, the application resets the counter, and if the counter exceeds about 10 minutes, it indicates the system is armed. To eliminate many false alarms, when transitioning from armed to disarmed (or from disarmed to armed), the application performs ARP scans for each configured MAC address to ensure they are indeed online or offline. In cases where ICMP ping is very unreliable, both iOS and Android will respond to this ARP scan with 99% accuracy. By combining captured Wi-Fi probe requests and using ARP scans, the Wi-Fi frequency becomes irrelevant at this time, as phones send probe requests on both frequencies, and ARP scans can work on both frequencies as well.
Notification Items
The Telegram bot is used to send notifications with captured images. They have a good mobile app and a good API. Messages can be viewed in a browser, and messages are synchronized across devices.
If the system is armed and motion is detected, a message with the captured image will be sent to the phone from the Telegram bot. Notifications are also sent when the alarm state changes.
Remote Control
Telegram bot commands can be sent to trigger certain actions:
/disable: Disable the service until re-enabled;
/enable: Enable the service after disabling;
/status: Send a status report;
/photo: Capture and send a photo;
/gif: Capture and send a gif.

Python
The application is written in Python 3, and the following pip modules provide most of the functionality: picamera, Scapy, python-telegram-bot. The application uses multithreading to handle events asynchronously, with 4 threads:
telegram_bot: Responds to commands.
monitor_alarm_state: Arms and disarms the system.
capture_packets: Captures packets from mobile devices.
process_photos: Sends captured images via SMS.

Installation, Configuration, and Running

Install rpi-security, reload the systemd configuration, and enable the service:

Add one or more MAC addresses, the Telegram bot API key, and any other changes to /etc/rpi-security.conf. Ensure that the camera module is enabled using raspi-config and start the service:

You need to send at least one message to the Telegram bot; otherwise, it will not be able to send messages to the phone. This way, the service can save the chat_id of the SMS. So just send the /status command.
It runs as a service and logs to syslog. To view the logs, check /var/log/syslog. There is also a debug option that logs to stdout:
root@raspberrypi:~# iw phy phy0 interface add mon0 type monitorroot@raspberrypi:~# ifconfig mon0 uproot@raspberrypi:~# rpi-security.py -d2016-05-28 14:43:30 DEBUG rpi-security.py:73 MainThread State file read: /var/lib/rpi-security/state.yaml2016-05-28 14:43:30 DEBUG rpi-security.py:44 MainThread Calculated network: 192.168.178.0/242016-05-28 14:43:41 INFO rpi-security.py:214 monitor_alarm_state thread running2016-05-28 14:43:41 INFO rpi-security.py:196 capture_packets thread running2016-05-28 14:43:41 INFO rpi-security.py:259 telegram_bot thread running2016-05-28 14:43:41 INFO rpi-security.py:154 process_photos thread running2016-05-28 14:43:43 INFO rpi-security.py:392 MainThread rpi-security running2016-05-28 14:43:43 INFO rpi-security.py:112 MainThread Telegram message Sent: "rpi-security running"2016-05-28 14:44:29 DEBUG rpi-security.py:191 capture_packets Packet detected from aa:aa:aa:bb:bb:bb2016-05-28 14:44:29 DEBUG rpi-security.py:191 capture_packets Packet detected from aa:aa:aa:bb:bb:bb2016-05-28 14:44:48 DEBUG rpi-security.py:280 Dummy-1 Motion detected but current_state is: disarmed

You can use interfaces with different names, just ensure to change the network_interface parameter in /etc/rpi-security.conf and the reference to mon0 in rpi-security.service.
Restart After Losing Connection
My Raspberry Pi loses WLAN connection about every month or two. I created a cron job to check connectivity and restart if the check fails.

Compiled by: Wang Tongxue

More Practical Projects Recommended:
STM32 IoT Smart Home Project
Project Sharing | Step-by-Step Face Recognition with MATLAB + Raspberry Pi
Raspberry Pi + Compute Stick 2 for Real-Time Face Recognition Project
Cloud Computing Platform Setup for Embedded Development Boards
STM32 Realizing the Simplest Air Mouse
Arduino Rubik’s Cube Robot
STM32 Version “AI Soul Painter”
STM32 Electronic Photo Album Production
STM32 + DDS Homemade Signal Generator
Using Raspberry Pi and Web Interface to Remotely Control Household Appliances
STM32 “Cloud” Music Player
Raspberry Pi Remote Monitoring
Design of a Glow Tube Clock Based on STM32
Homemade FPGA Minimal System Board (PCB can be directly made)
Raspberry Pi 4 Building NAS, Easily Networking Hard Drives
ESP32 Car Hardware and Software Practical Sharing
Only 79 Lines of Code to Complete Unlimited Creative Gesture Recognition
IoT + Electronic Ink Screen to Create Custom Display Screen
Build a Road Robot Worth Over a Thousand with Just Dozens of Yuan
Glasses with No Arc: Visual Persistence POV Project
DIY Gesture Recognition Module
Practical Small Designs That Can Be DIYed with Zero Basics
Raspberry Pi Creates a Smart Doorbell + Smart Door Lock with Video
Strange! My Development Board Can Automatically Play Games
Homemade Breathing Machine
ESP8266 + Zigbee Networking Transformation of Wall Switch
Wireless Home Monitoring System
DIY Bionic Arm, A Tool to Free Hands
Handmade Air Purifier, All Design Materials Open Source
Tech Toy: Bluetooth Turret that Can Be Remotely Controlled
Using ST Sensors to Create LittleBee Monitoring System, Let Bees “Talk”
Homemade Cat Toy
STM32 + Raspberry Pi Realizing a 6s Rubik’s Cube Robot
Blind Modification of Drone Controller, DIY “Foam” Drone
Making a Smart Relay Without Arc Using STM32
Making a Practical New Model Necklace with Only 5 Components
The Simplest Method for Heart Rate Measurement (Suitable for Secondary Development)
Breaking the Magnetic Levitation Globe
The Amazing Light Can Transmit Video? Revealing the Production Process
DIY Smart Watch with STM32 60FPS Animation
Saluting the Classic Radio, Observing the Production Process
DIY Third Eye: “Special” Glasses with Bluetooth/OLED/Lens Function
Homemade Solar Charger that Tracks Maximum Power Point
No MCU Night Vision Goggles, Just Connect Wires
The Correct Way to DIY a Flow Meter, Interest Transformer
Homemade High-Difficulty Racing Timer (Lap/Heat Sensitive Print)
DIY Mobile WIFI Controlled LED Dice
A Gas Charging Treasure
Recommended Reading:
