|Software apps and online services:|
Raspberry Pi Security System
A simple security system to run on a Raspberry Pi.
- Motion triggered image capture.
- Mobile notifications with photos.
- Detects when you are home and arms or disarms automatically.
- Can be remotely disabled or queried for status using Telegram.
Similar to these products:
You will need this hardware:
- Raspberry Pi with camera interface. I use a model A+.
- Raspberry Pi camera module.
- PIR sensor module. Any generic HC SR501 (or similar) module should work. Example from Adafruit.
- USB Wi-Fi that supports monitor mode. I used a RT5370 based adapter, they are cheap at about €6 and easy to find.
- An enclosure of some sort. Details of the hardware I made is here.
- A Telegram bot. It’s free and easy to setup.
- Raspbian distribution installed. I used Jessie lite. You could possibly use a different OS but I haven’t tried it.
- Python 2.7.
How it works
Automatic presence detection
One of my main goals was to have the system completely automatic. I didn’t want to have to arm or disarm it when leaving or arriving home. I figured the easiest way to achieve this was to try and detect the mobile phones of the home occupants. Conceptually this was quite simple but in practice it was the most challenging part because:
- Capturing all packets on a Wi-Fi interface is too resource intensive.
- There are presently no good 5Ghz USB Wi-Fi adapters that support monitor mode. This means packet monitoring is restricted to 2.4Ghz where most modern mobile phones use 5Ghz now.
- Mobile phones are not always online and sending packets over Wi-Fi. Sometimes they stay unconnected for 15 minutes or longer.
- Even with 99% accuracy false alarms are annoying.
After much testing I used an approach that mixes active (ARP scan) and passive (packet capture) detection over the Wi-Fi adapter based on knowing the MAC addresses of the mobile phones. The mobile phone MAC addresses are set in the configuration and the rpi-security application captures packets on a monitor mode interface with the following filter: 1. Wi-Fi probe requests from any of the configured MACs. 2. Any packets sent from the configured MACs to the host running rpi-security.
The application resets a counter when packets are detected and if the counter goes longer than ~10 minutes the system is armed. To eliminate the many false alarms, when transitioning from armed to disarmed state or vice versa, the application performs an ARP scan directed at each of the configured MAC addresses to be sure they are definitely online or offline. Both iOS and Android will respond to this ARP scan 99% of the time where a ICMP ping is quite unreliable. By combining the capture of Wi-Fi probe requests and using ARP scanning, the Wi-Fi frequency doesn’t matter because mobile phones send probe requests on both frequencies and ARP scan works across both frequencies too.
A Telegram bot is used to send notifications with the captured images. They have good mobile applications and a nice API. You can also view the messages in a browser and messages are synced across devices.
If the system is in an armed state and motion is detected then a message with the captured image is sent to you from the Telegram bot.
Notifications are also sent on any alarm state change.
You can send the Telegram bot commands that trigger certain actions.
- /disable: Disables the service until re-enabled.
- /enable: Enables the service after it being disabled.
- /status: Sends a status report.
I wrote the whole application in python. Large parts of the functionality are provided by the following pip modules:
The application uses multithreading in order to process events asynchronously. There are 4 threads:
- telegram_bot: Responds to commands.
- monitor_alarm_state: Arms and disarms the service when packets are detected.
- capture_packets: Captures packets from the mobile devices.
- process_photos: Sends captured images via Telegram messages.
Installation, configuration and Running
The interface used to connect to your WiFi network must be the same interface that supports monitor mode. And this must be the same WiFi network that the mobile phones connect to.
root@raspberrypi:~# iw phy phy0 interface add mon0 type monitor root@raspberrypi:~# ifconfig mon0 up root@raspberrypi:~# rpi-security.py -d 2016-05-28 14:43:30 DEBUG rpi-security.py:73 MainThread State file read: /var/lib/rpi-security/state.yaml 2016-05-28 14:43:30 DEBUG rpi-security.py:44 MainThread Calculated network: 192.168.178.0/24 2016-05-28 14:43:41 INFO rpi-security.py:214 monitor_alarm_state thread running 2016-05-28 14:43:41 INFO rpi-security.py:196 capture_packets thread running 2016-05-28 14:43:41 INFO rpi-security.py:259 telegram_bot thread running 2016-05-28 14:43:41 INFO rpi-security.py:154 process_photos thread running 2016-05-28 14:43:43 INFO rpi-security.py:392 MainThread rpi-security running 2016-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:bb 2016-05-28 14:44:29 DEBUG rpi-security.py:191 capture_packets Packet detected from aa:aa:aa:bb:bb:bb 2016-05-28 14:44:48 DEBUG rpi-security.py:280 Dummy-1 Motion detected but current_state is: disarmed