Navigation

Friday, 12 July 2013

Raspberry Pi Surveillance System: An Overview of What I Did

For my final year project I made a surveillance system using the Raspberry Pi. I aim for this post to just be an overview of what I did.

I'm just going to dive into this. This first thing I did was order a Raspberry Pi. When that came I tried to look for some tutorials, I messed around with it for awhile but after a few days I found the MagPi. MagPi is a free online magazine with all the latest news on the Raspberry Pi including tutorials. One of those tutorials showed you how to make a door sensor. So this is the first prototype I made for the system:
The IDC connector is where the Raspberry Pi connects into. On the board there are 2 indicator LED's which shows the active or inactive states of the system, a buzzer and an input button. The white wire connects to the reed switch but I'd cut it off by this point as I needed it for another model.

Now that I had a greater understanding on how to build the hardware for this project and a good foundation to start, I set out to make a new, better, smaller model. This time it had a PIR sensor. A PIR sensor is very easy to use because all you need to do is check to see if the data pin is high or low. If it is high, it has detected something. I wanted to keep to the 'hobbyist theme' so I put it in an Altoids mint tin. Here it is:




The Circuitry was pretty much the same as the older model, I'd just learnt how to mount it all closer together and be more space efficient. The only difference this model had that the prototype didn't was the PIR sensor that used the 5v pin and an extra IO pin. So obviously the next task was to write the software for it. In the time it had taken me to get from the prototype to this model, I'd also gotten better at programming in Python. I will try and find a way to post my code... I might just post it at the end of this post :/.

The following thing I did was create the user interface. Now for this I made an overly fancy website with multiple user log in and a nice graphical design. I didn't need to do this but I wanted to show that I had those skills. I used the .NET framework with C#. The website had multiple user log in and a way to bind the hardware to the website. All of the users and their details where stored in a SQL database. Here are some screenshots:
I used session cookies for persistent log in 

The next fundamental aspect of the system was to be able to control the hardware from the website. For this I used a python library called WebIOPi. It's a REST based framework designed for the Raspberry Pi and when you install the package it sets up a web server. On that web server is a HTML file linked with a JavaScript file. In the JavaScript file I re wrote it to have two buttons. I then tied it to my Python script so that the buttons would call my Python functions. Its a really good tool I would recommend having a play with it. I then presented this HTML file with the JavaScript buttons on the website via an iframe and gave it the src to the webserver in the C# file like this:
Buttons.Attributes["src"] = "http://192.168.0.11:9898/";
My final important requirement was to make the user aware via email. For this my python code took advantage of the smtplib library. This library allows you to send emails from your script. To do this I used Googles SMTP server which meant I had to make another email account. The code is as follows:
def sendEmail(sensor):
try:
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
try:
smtpserver.login("rpi.alert.infor@gmail.com", "PASSWORD HERE")
except (smtplib.SMTPException), e:
print e
smtpserver.close()
except (socket.gaierror, socket.error, socket.herror, smtplib.SMTPException), e:
print e

to = "rabostock92@googlemail.com"
sub = "Your alarm has been triggered!"
body = "The " + sensor + " sensor was tripped. You should assess the situation!"
head = "AN URGANT MESSAGE FROM YOUR RASPBERRY PI"
msg = head + "\n\n" + body + "\n\n\n"

try:
smtpserver.sendmail("rpi.alert.infor@gmail.com", "rabostock92@googlemail.com", msg)
smtpserver.close()
except (smtplib.SMTPException), e:
print e
smtpserver.close() 

Finally I will just post the whole Python script:
import time
import webiopi
import smtplib
import socket
import sys
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

#LED
GPIO.setup(16, GPIO.OUT)
#Button
GPIO.setup(11, GPIO.IN)
#Reed
GPIO.setup(13, GPIO.IN)
#Buzzer
GPIO.setup(12, GPIO.OUT)
#Motion
GPIO.setup(15, GPIO.IN)

#Variables
armed = False
alert = False

#Functions
def sendEmail(sensor):
try:
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
try:
smtpserver.login("rpi.alert.infor@gmail.com", "PASSWORD HERE")
except (smtplib.SMTPException), e:
print e
smtpserver.close()
except (socket.gaierror, socket.error, socket.herror, smtplib.SMTPException), e:
print e

sub = "Your alarm has been triggered!"
body = "The " + sensor + " sensor was tripped. You should assess the situation!"
head = "AN URGANT MESSAGE FROM YOUR RASPBERRY PI"
msg = head + "\n\n" + body + "\n\n\n"
try:
smtpserver.close()
except (smtplib.SMTPException), e:
print e
smtpserver.close()

def configurePinStatus():
GPIO.output(16, False)
GPIO.output(12, False)

def waitForButtonPress():
print "Press the button to arm me. I will give you 10 seconds to leave the room."
while GPIO.input(11): #Will be true when no button is pressed because we are using a pull up resistor
time.sleep(.2)
return True

def armToggle(t):
global armed
if t == True:
armed = True
GPIO.output(16, True)
else:
armed = False
GPIO.output(16, False)

def armedCountDown():
for i in range(0, 10):
GPIO.output(12, True)
time.sleep(.5)
GPIO.output(12, False)
time.sleep(.5)

def alertAlarm():
print "\nAlert!"
global alert
alert = True
GPIO.output(12, True)
armToggle(False)

def stopAlarm():
global alert
alert = False
GPIO.output(12, False)

def checkDoor():
if GPIO.input(13):
print "\nThe door sensor was triggered!"
sendEmail("Door")
alertAlarm()

def checkMotion():
if GPIO.input(15):
print "\nMotion was detected"
sendEmail("Motion")
alertAlarm()

def checkDisarm():
if not GPIO.input(11):
armToggle(False)
time.sleep(.25)
print "The system has been disarmed"
time.sleep(.5) #Need to slow the system down so that it doesn't arm the system again whilst the user presses the button

def arm():
armedCountDown()
armToggle(True)

def disarm():
stopAlarm()
armToggle(False)

server = webiopi.Server(port=9898)
server.addMacro(arm)
server.addMacro(disarm)

configurePinStatus()
print "*********************************************************************************************************"
print "*                                                                                                       *"
print "* I was intended for a headless Raspberry Pi. Please use the hardware to interface with me. *"
print "*                                                                                                       *"
print "*********************************************************************************************************"

def loop():
if armed:
checkDoor()
checkMotion()

webiopi.runLoop(loop)
server.stop() 
There is a lot more to the system than I have explained but I wanted to keep it a shortish post. Any thanks!

1 comment: