

We have seen how to build home security system with email notification in the tutorial with out using Camera. In my previous posts we have also seen other IoT Projects with raspberry Pi and how to use Cayenne to create triggers. Here we will be extending home security email alert system using raspberry pi to the next level . We will add Raspberry Pi Camera in to our setup to capture the image and send to the email when the intruder is detected. The project will be using Python script to capture the image from the camera and send in email when detecting any trespassers movement and .We can call this a IoT project as simple Home Security Email Alert Systems with Camera (smart surveillance monitoring system using raspberry pi and pir sensor).



The components needed:

We will be building this security system using raspberry pi with motion detection camera and above are the required components. PIR sensor will detect the motion (presence of intruder), the image will be captured at the time of sensor detected any motion and that system will alert you immediately by sending an email with the attachment of intruder image .You can install the sensor in front of your door , or any other place and you will be notified about the intruder even if you are any where in the world.

Connection:

First you need to Set up the Raspberry Pi 3 for the project. For setting up the raspberry pi with operating system follow my tutorial. Download the image and install it on the SD card, Connect to WIFI or ethernet cable.

PIR sensor has 3 pins, labelled VCC, OUT and GND. We will be using Raspberry PI pins 4 (5V) , 6 (GND) and 7 (GPIO4).Lets connect the PIR sensor to the Raspberry Pi, use Female to Female Jumper wires or you can use Breadboard in between the connections.Connect the PIN labelled as VCC on the PIR sensor to the 5V pin on the Raspberry Pi, connect the middle one labelled OUT to Pin GPIO4, and connect the Pin labelled as GND to a ground pin 6. Connect the Raspberry pi camera to the Camera port. Depending upon whether you are using Raspberry Pi 3 B+ or Raspberry Pi zero the Camera cable will change, make sure you purchase the right camera for your device.

Raspberry pi camera security cam is setup now and we have write the python code for motion detection camera email alert. To run the program open Python Idle and copy the below code in to it. Change the "your email address" , "To email address" and "your password" to your respective emails and password. We are using gmail here so you should use Gmail email address, otherwise it wont work. If you are using any other email address change the SMTP and port for the respective email configuration.

Enable Raspberry Pi Camera by using Raspberry Pi Software Configuration Tool (raspi-config) once you have the camera connected to pi:

sudo raspi-config

Select Enable camera and Enable it.

After successfully installing Raspbian OS on Raspberry Pi, we need to install Pi camera library files for running this project in Raspberry pi. Follow given commands to install the libraries:

sudo apt-get upgrade

sudo apt-get update

sudo apt-get install python-picamera

sudo apt-get installpython3-picamera

We need to install the libraries for sending mails using SMTP using the below commands:

​sudo apt-get install ssmtp

sudo apt-get install mailutils​

For sending simple email, smtplib is enough but for sending mail with subject line, attachment etc. we need to use MIME (Multipurpose Internet Mail Extensions).

Raspberry Pi Security Camera Motion Detection and Email :

Program 1 : This is the extended version which I used for normal email alert when motion detected.

import RPi.GPIO as GPIO

import picamera

from picamera import PiCamera

import time

import smtplib

from email.MIMEMultipart import MIMEMultipart

from email.MIMEText import MIMEText

from email.MIMEBase import MIMEBase

from email import encoders

from email.mime.image import MIMEImage

fromaddr = "your email address" # change the email address accordingly

toaddr = "To email address" # change the email address accordingly



mail = MIMEMultipart()

mail['From'] = fromaddr

mail['To'] = toaddr

mail['Subject'] = "Intruder Alert!, Motion detected!"

body = "Find the attached for the intruder picture"

PIRSensor = 4

GPIO.setmode(GPIO.BCM)

GPIO.setup(PIRSensor, GPIO.IN, GPIO.PUD_DOWN)

camera = PiCamera()

def capture_image():

data= time.strftime("%d_%b_%Y|%H:%M:%S")

camera.start_preview()

time.sleep(5)

print data

camera.capture('/home/pi/Pictures/img%s.png'%data)

img= '/home/pi/Pictures/img%s.png'%data

camera.stop_preview()

time.sleep(1)

Current_State = 0

Previous_State = 0

try:

print "Waiting for PIR to settle ..."

# Loop until PIR output is 0

while GPIO.input(PIRSensor)==1:

Current_State = 0

print " Ready"

# Loop until users quits with CTRL-C

while True :

# Read PIR state

Current_State = GPIO.input(PIRSensor)

if Current_State==1 and Previous_State==0:

# PIR is triggered

print " Intruder Alert!, Motion detected!"

#Capture Image

capture_image()

#Send mail

mail.attach(MIMEText(body, 'plain'))

attachment = open(img, 'rb')

image=MIMEImage(attachment.read())

attachment.close()

mail.attach(image)

server = smtplib.SMTP('smtp.gmail.com', 587)

server.starttls()

server.login(fromaddr, "your password")

text = mail.as_string()

server.sendmail(fromaddr, toaddr, text)

server.quit()

# Record previous state

Previous_State=1

elif Current_State==0 and Previous_State==1:

# PIR has returned to ready state

print " Ready"

Previous_State=0

# Wait for 10 milliseconds

time.sleep(0.05)

except KeyboardInterrupt:

print " Quit"

# Reset GPIO settings

GPIO.cleanup()



Program 2:

To simplify the above program lets make a function together for capturing the image and sending email when raspberry pi security camera motion detection.

import RPi.GPIO as GPIO

import picamera

from picamera import PiCamera

import time

import smtplib

from email.MIMEMultipart import MIMEMultipart

from email.MIMEText import MIMEText

from email.MIMEBase import MIMEBase

from email import encoders

from email.mime.image import MIMEImage

fromaddr = "your email address" # change the email address accordingly

toaddr = "To email address" # change the email address accordingly



mail = MIMEMultipart()

mail['From'] = fromaddr

mail['To'] = toaddr

mail['Subject'] = "Intruder Alert!, Motion detected!"

body = "Find the attached for the intruder picture"

PIRSensor = 4

GPIO.setmode(GPIO.BCM)

GPIO.setup(PIRSensor, GPIO.IN, GPIO.PUD_DOWN)

camera = PiCamera()

def capture_image():

data= time.strftime("%d_%b_%Y|%H:%M:%S")

camera.start_preview()

time.sleep(5)

print data

camera.capture('/home/pi/Pictures/img%s.png'%data)

img= '/home/pi/Pictures/img%s.png'%data

camera.stop_preview()

time.sleep(1)

mail.attach(MIMEText(body, 'plain'))

attachment = open(img, 'rb')

image=MIMEImage(attachment.read())

attachment.close()

mail.attach(image)

server = smtplib.SMTP('smtp.gmail.com', 587)

server.starttls()

server.login(fromaddr, "your password")

text = mail.as_string()

server.sendmail(fromaddr, toaddr, text)

server.quit()

while True :



# Read PIR state

Current_State = GPIO.input(PIRSensor)

if Current_State==1:

# PIR is triggered

print " Intruder Alert!, Motion detected!"

#Capture Image and send email

capture_image()

elif Current_State==0 :

# PIR has returned to ready state

print " Ready"

# Wait for 10 milliseconds

time.sleep(0.05)

except KeyboardInterrupt:

print " Quit"

# Reset GPIO settings

GPIO.cleanup()

