You are on page 1of 5

21st July 2016 Smart Burglar Alarm system using Raspberry Pi

Howdy and welcome!

What you're going to learn here is a very simple and naive implementation of a burglar alarm system using a Raspberry
Pi.

Things you need:

1. Raspberry Pi (Any model. I used a RasPi 2 Model B).

[https://3.bp.blogspot.com/-yiV-
y7pj2ao/V49xjG7Gy7I/AAAAAAAAAhM/Xp-7M81lI38shZuNapdDOwyY9BRJ6sU6QCLcB/s1600/raspi_2_model%2BB.jpg]

2. A PIR motion detector sensor

[https://3.bp.blogspot.com/-
nNsN9NPCSoo/V49xnPinwwI/AAAAAAAAAhQ/RDJS52lhBjI2p_xSQ0jdAC0a0SciS4S_QCLcB/s1600/pir-motion-sensor-3.JPG]

3. Female-to-Female jumper cables (3 nos).


[https://3.bp.blogspot.com/-
b03UasFh9MU/V49xrMNwJZI/AAAAAAAAAhU/r8MAlBH8TdYISc_lDM09josReHM-FOhVQCLcB/s1600/Female-
Female_Jumper_Wire.jpg]

Programming language: Python.


APIs: Twilio APIs to send out message and call requests (hold your horses, you will learn what it does later).

Set-up:

The RasPi module has a series of pins to the right called the GPIO (General-purpose Input/Output) PINs. We're going to
use 3 of those pins.

PIN no. Location (look at the screenshot) Purpose

PIN 1 Left row, 1st PIN on the top 5V VCC (power supply)
PIN 5 Left row, 5th from the top Ground
PIN 4 Left row, 4th PIN Sensor PIN (To sense the output from the PIR)

[https://4.bp.blogspot.com/-8AsQvj9uL8I/V49wnrNEIoI/AAAAAAAAAhA/5vBj5-
p4VwIgPCH9atDu1t4H84JChgmeQCLcB/s1600/GPIO.jpg]
GPIOs
Use the F-F jumper cables to connect the RasPi and the PIR sensor. PIR module's PIN configuration is as shown
below(be sure to connect to the right pins):

[https://3.bp.blogspot.com/-24dTt-
xQzs0/V495xqNdCEI/AAAAAAAAAhs/c2efzMw97JI7CDy5DbW1vhkqF7Ab1ysQwCLcB/s1600/pir-sensor-0002.jpg]

A little about Twilio:

Twilio is an opensource API provider who lets you send out text messages, calls through their APIs in your program.
You'll need to sign-up for an account here: https://www.twilio.com [https://www.twilio.com/] (Its free!). Once you are
signed in, you will be provided with a twilio number, note it down. You'll use this in your code.

To send a text message:

Now, navigate to the "Verified Caller ID" and add a number to send the 'Burglar Alert' to (as shown below). You can add
as many numbers as you want.

[https://3.bp.blogspot.com/-i9mYXjvt-
M8/V497O4UHt1I/AAAAAAAAAh0/OWYvbt8IfW4mBSkhrF4iAbualTozYKDgACLcB/s1600/twilio_caller_id.jpg]

Go to the home screen and select Dashbord. Copy the 'Account SID' and 'Auth token'.
[https://2.bp.blogspot.com/-USZ7b9-
Vr8Y/V498uVbh9cI/AAAAAAAAAiA/rshmODFJ3PwE5C9iTY1OhMTg4xP3jpndQCLcB/s1600/aut%2526sid.jpg]

To set-up a Call:

Go to this link [https://www.twilio.com/labs/twimlets/voicemail] and enter the message you would like to tell the person
who receives the call after detection. Select 'false' for transcribe. Copy the resulting URL.

IMPORTANT: Install the Twilio API libraries in your RasPi. Follow the steps here
[https://www.twilio.com/docs/libraries/python] .

The Code:

import RPi.GPIO as GPIO


import time
from twilio.rest import TwilioRestClient #libraries are already installed

sensor = 4 #initializing variable sensor to 4

GPIO.setmode(GPIO.BCM) #using Broadcom SOC channel


GPIO.setup(sensor, GPIO.IN,GPIO.PUD_DOWN) #pull up/down parameter

previous_state = False
current_state = False
account_sid="your_sid_goes_here"
auth_token ="your_auth_token_goes_here"
client = TwilioRestClient(account_sid,auth_token) #this is an inbuilt API

while True:
time.sleep(0.5) #delay created after every loop
previos_state = current_state
current_state = GPIO.input(sensor) #checks for input on PIN 4
if current_state != previous_state: # enters loop if PIN 4 is high
new_state = "HIGH" if current_state else "LOW"
print("GPIO pin %s is %s" % (sensor, new_state))
message = client.messages.create(to="any_verified_number", from_="your_twilio_number", body="Burgler Alert!!!! ")
call = client.calls.create(to="any_verified_number", from_="your_twilio_number", url="the_url_from_voicemail")

To run:
1.Open your terminal.
2. change directory to your program's location
3. python your_program_name.py
4. move your hand over the PIR sensor, You should get a text message and a call any moment.

And folks, this will work ONLY if you have an internet connection to your RasPi!!!

Posted 21st July 2016 by Theo


Location: Chennai, Tamil Nadu, India

0 Add a comment

You might also like