Tested on:Raspbian Jessie; RPi 3
Prerequisites:Raspberry Pi with Raspbian Jessie and internet connection (see steps 1-7 in the Introduction)
Hardware:5V or 3.3V passive infrared sensor (PIR)
Led with a resistor fitting for 3.3V and 20mA
NPN-Transistor BC547 or similar
1k Ohm resistor
breadboard and jumper wires
self-oscillating Piezo buzzer
2

Intrusion detection with a Raspberry Pi and a smartphone over bluetooth

The Raspberry Pi enables building sophisticated alarm systems for a low price and with comparably low effort. The alarm system presented herein detects if a specific bluetooth device (e.g. smartphone) is present. If a PIR (passive infrared) motion sensor detects movement and the smartphone is not present, an piezo-buzzer is activated. This is easily extendable, for example by triggering sms alerts. Thanks to Rob Zwetsloot and the MagPi for the idea!
As is, this tutorial uses the bluetooth capabilities of the Raspberry Pi 3; other models would require an USB bluetooth dongle.

Installation
After installing Raspbian Jessie on the SD card and booting the RPi, the following steps have to be performed:

In the now open file, replace

with

Save, then reboot with

sudo reboot

Log in again and enter

in the terminal. Now, the bluetooth drivers and the demo script are installed. Shutdown the Raspberry Pi and assemble the circuit from the next section.

Breadboard schematic
Here is the breadboard schematic for the intrusion detection system. The PIR sensor used in this tutorial runs natively on 5V; with the 3.3V supplied in this schematic, the sensors range is smaller.

Mobile Bluetooth Alarm System

Smartphone Bluetooth alarm system – schematic

Running the script
Attach the circuit and boot the Raspberry Pi. Enable Bluetooth on your device and make the device visible. Then, start the script with

After the script prints the MAC address of your device next to its name like in the image below, kill the script with CTRL+C.

Mobile MAC address

Mobile MAC address

Open the script with

and enter your MAC address in the line

Save and exit, make your device visible and rerun the script. After a few seconds, the LED turns on and stays that way, as long as the smartphone is detected. Moving something in front of the PIR sensor shows no result. But, if you disable the mobiles bluetooth, the LED goes off after a few seconds and movements in front of the sensor yields a beep from the buzzer.

Code details
For the PIR sensor, event detection is applied with

This sends an interrupt if a raising edge (resulting from a movement in front of the sensor) is detected on pin 7 and calls the function action then. The global variable at_home is true if your mobile was detected; if it is false, an alarm is fired.
The bluetooth interface itself is very comfortable:

The function search returns a list of MAC addresses and the corresponding device names.

The full script
Click on Full Script to show all of the code.

Demo

Links
Motion sensor tutorial: http://www.knight-of-pi.org/a-passive-infrared-sensorpir-lets-your-raspberry-pi-detect-motion/
Rob Zwetsloot on MagPi: https://www.raspberrypi.org/blog/author/robzwetsloot/
MagPi website: https://www.raspberrypi.org/magpi/

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.

2 Comments

Comment

Time limit is exhausted. Please reload CAPTCHA.