Home Security Email Alert System Using Raspberry Pi

Intro: Home Security Email Alert System Using Raspberry Pi

In this tutorial we will look at how to setup up your raspberry pi to take picture of the intruder who enters your home and send an email to you whenever you are not at your home.

Step 1: What You’ll Need

  1. Raspberry Pi running Raspbian
  2. USB Camera
  3. PIR Sensor
  4. USB Wi-Fi Module for internet access ( optional, if you connect your Ethernet cable for internet, USB Wi-Fi Module is not needed )
  5. Power Adapter to power the Rsapberry Pi

Step 2: Setting Up Your Raspberry Pi With Raspbian

First you need to install Raspbian on your micro sd card that you will be using in your Raspberry Pi.

You can download the latest image of Raspbian from Raspberry Pi website at:

http://www.raspberrypi.org/downloads/

You will need to use an image writing tool to install the image on your SD card. You can find the steps to do that at:

http://www.raspberrypi.org/documentation/installat...

Step 3: Install OpenCV and Required Libraries

There are lots of methods to do this. But I would prefer using the simplest method that is available in OpenCV website at http://docs.opencv.org/doc/tutorials/introduction/...

Open the terminal of your raspberry pi and execute the following commands...

Install compiler

$ sudo apt-get install build-essential

Install required packages

$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

Install optional packages

$ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

Now download OpenCV to wherever you want to compile the source.

$ mkdir xxx

$ cd xxx

$ git clone https://github.com/Itseez/opencv.git

Create and build directory and onfigure OpenCV with cmake. Don't forget the .. part at the end of

$ cd OpenCV-2.4.*

$ mkdir build

$ cd build

$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..

Now compile it

$ make

And finally install OpenCV

$ sudo make install

Step 4: Python Code

Use the attached Python code after editing the following lines

gmail_user = "xxxx@gmail.com" #Sender email address

Enter your email address in the above line using which the Photo captured will be sent

gmail_pwd = "xxxx" #Sender email password

Enter the password of the sender email address

to = "xxxx@gmail.com" #Receiver email address

Enter the email address to which the email with the picture captured will be sent...

Step 5: Make Necessary Connections and Test the Setup

Now connect the USB camera and PIR sensor to the Raspberry Pi.

Using three female-to-female jumper cables, you'll need to connect each of the PIR sensor's connectors to the appropriate pins on the Raspberry Pi.

Connect the top one labelled VCC on the PIR sensor to the 5V pin on the Raspberry Pi, connect the middle one labelled OUT to GPIO pin 4, and connect the bottom one labelled GND to a ground pin also marked GND.

Now you run the python code with sudo to check the setup. Whenever there is a movement it detected by the PIR sensor, an image is captured and email is set the to email address you entered as the receivers email address.

Step 6: How the Final Setup Looks and Output

Now, if you put the setup infront of your home door and run your python code and leave your home. If someone opens the door, you will get an email alert.

Step 7: Adding Wi-Fi Support to Raspberry Pi

Clear steps to setup Wi-Fi on Raspberry Wi-Fi is given at the official guide at

http://www.raspberrypi.org/documentation/configura...

Step 8: Raspberry Pi Auto Login and Auto Start and Run the Python Code

To auto run the python code at start up just by powering up the raspberry pi use the following steps.

Auto Login:

In Terminal:

sudo nano /etc/inittab

Scroll down to:

1:2345:respawn:/sbin/getty 115200 tty1

and change to

#1:2345:respawn:/sbin/getty 115200 tty1

Under that line add:

1:2345:respawn:/bin/login -f pi tty1 /dev/tty1 2>&1

Ctrl+X to exit, Y to save followed by enter twice

Auto run the python code

sudo nano /etc/rc.local

Scroll to the bottom and add the following above exit 0:

sudo python /directory/pythonfile.py

Press Ctrl+X to exit, Y to save followed by Enter twice.

If you face any issues in the steps, look at http://elinux.org/RPi_Debian_Auto_Login

Now reboot the system and you will see that the python code has started running automatically and will send you an email if it detects a movement using the PIR sensor.

Now you can turn on the setup whenever you leave your home locked to make sure you get an email alert whenever someone enters your home when you are not there...

Share

Recommendations

  • Plastics Contest

    Plastics Contest
  • Electronics Tips & Tricks Challenge

    Electronics Tips & Tricks Challenge
  • Audio Contest 2018

    Audio Contest 2018

25 Discussions

0
None
JohnK580

Question 4 months ago on Introduction

Hi, The code is working for me. I receive the email alerts, but I can't view the images. In the email alerts I get the message:

Could not load image '18-05-30-20-17.jpg' unrecognized image file format.

Any help greatly appreciated,

2 more answers
0
None
arjunbboiJohnK580

Answer 2 months ago

Made it work with Raspberry Pi Camera.

Here is the code .

import RPi.GPIO as GPIO
import time
import numpy as np
import cv2
from datetime import datetime
import os
import smtplib
import picamera
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
gmail_user = "abc@gmail.com" #Sender email address
gmail_pwd = "12345" #Sender email password
to = "123@gmail.com" #Receiver email address
subject = "Security Breach"
text = "There is some activity in your home."

sensor = 4

GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)

previous_state = False
current_state = False

while True:
previous_state = current_state
current_state = GPIO.input(sensor)

if current_state != previous_state:
new_state = "HIGH" if current_state else "LOW"
print("GPIO pin %s is %s" % (sensor, new_state))
if current_state:
camera = picamera.PiCamera()
picname = datetime.now().strftime("%y-%m-%d-%H-%M")
picname1 = camera.capture(picname+'.jpg')
print "Saving Photo"
print "Sending email"
attach = (picname+'.jpg')

msg = MIMEMultipart()

msg['From'] = gmail_user
msg['To'] = to
msg['Subject'] = subject

msg.attach(MIMEText(text))

part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition',
'attachment; filename="%s"' % os.path.basename(attach))
msg.attach(part)

mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
# Should be mailServer.quit(), but that crashes...
mailServer.close()
print "Email Sent"
os.remove(attach)
print "Picture removed from saved location"
camera.close()
time.sleep(3)

0
None
arjunbboiJohnK580

Answer 3 months ago

Hi, Even i am getting the same error. Did you find a solution for this ?

0
None
Rohan5210

Question 6 months ago

Can you help me in running the Python code

0
None
Rohan5210

Question 6 months ago on Step 3

Hey where to write the c-make part

0
None
MohamadH3

3 years ago on Introduction

hello..i'm following your step and stuck in installing OpenCV

$ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

i got some error...and proceed by ignoring the error...then all the next step i cant complete it.

2 replies
0
None
HimanshuS176beziered

Reply 11 months ago

From opencv folder, delete build folder and then try again from 3rd step

0
None
papashok

2 years ago

Wonderful project but code was not working for me and giving lot of errors and also e-mails are not instant and many times 15 min late So i write code and integrated with pushbullet.com

So Its python script For raspberry pi with pir sensor and raspi-cam or usb
cam
Working

..................................................

Whenever motion is detected by PIR sensor Its get the usb camera to
take photos ( about 5 photos by default ) And Send the photos to
pusbullet app with instant alert on smart phone

Any one need help in setting up and running script

contact

papashok64@gmail.com

https://www.fiverr.com/papashok/help-you-with-rasp...

1 reply
0
None
harmeet03papashok

Reply 1 year ago

heyy hiii papashok

i am trying to integrate the code with pushbullet app , but i am not able to do so,

giving lot of errors??

can your share your code at harmeetmonty11@gmail.com

0
None
SuthaA1

2 years ago

Project Working..I use raspberry pi 3 model B ...try to use new version of opencv

how to add multiple email receiver

2 replies
0
None
adi_rahardjoSuthaA1

Reply 1 year ago

Sutha1 : please share your code to me, i need this

0
None
AnupO1SuthaA1

Reply 2 years ago

can u share me the source code???

0
None
OmkarP26

2 years ago

after entering make, getting error

make: *** No targets sepecified and no makefile found. stop

0
None
balaji_esakki

2 years ago

can anyone help me with the algorithm for the above provided python codes???
thank you

0
None
C1115

2 years ago

Hello

I am trying to do complete this with a Pi Cam instead of a USB camera, but I don't really know where to start. Are there any hints, tips or tutorials available?

0
None
C1115

2 years ago

Hello

I am trying to do complete this with a Pi Cam instead of a USB camera, but I don't really know where to start. Are there any hints, tips or tutorials available?

0
None
sharon c jhon

2 years ago

Is it possible to connect raspberry pie camera module instead of USB camera

1 reply
0
None
HariM7

2 years ago

can i chaange with raspberry camera module ? must change the python source code ? please help me. i'm newbie. thanks