ujjval rathod
Published

Raspberry Pi Home Security System with Camera and PIR Sensor

Detecting motion with PIR sensor and sending the Raspberry Pi camera image as email.

IntermediateWork in progress2 hours26,644
Raspberry Pi Home Security System with Camera and PIR Sensor

Things used in this project

Story

Read more

Custom parts and enclosures

Photos

screenshot_from_2018-10-18_11-39-25_IKrEi0RZxT.png

20181025_133617_USayrqvRVV.jpg

Schematics

Schematic

Connect Pin 3 of Raspberry Pi(5V Power) to the Power pin of the sensor.
Pin 5 of Raspberry Pi (GND) to the GND of the Sensor
And Pin GPIO23 of Raspberry Pi to the output Pin of the sensor(of course one can use different GPIO but then one should change to Python code)

Code

Untitled file

Python
It is a simple python code just change the emails as you have. I have kept 10 seconds of delay after the image is captured. so the next movement will only be detected after 10 seconds. one can change to time with their requirements.
from picamera import PiCamera
from time import sleep
import smtplib
import time
from datetime import datetime
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
import RPi.GPIO as GPIO
import time

toaddr = 'TO_EMAIL'
me = 'FROM_EMAIL'
Subject='security alert'

GPIO.setmode(GPIO.BCM)

P=PiCamera()
P.resolution= (1024,768)
P.start_preview()
    
GPIO.setup(23, GPIO.IN)
while True:
    if GPIO.input(23):
        print("Motion...")
        #camera warm-up time
        time.sleep(2)
        P.capture('movement.jpg')
        time.sleep(10)
        subject='Security allert!!'
        msg = MIMEMultipart()
        msg['Subject'] = subject
        msg['From'] = me
        msg['To'] = toaddr
        
        fp= open('movement.jpg','rb')
        img = MIMEImage(fp.read())
        fp.close()
        msg.attach(img)

        server = smtplib.SMTP('smtp.gmail.com',587)
        server.starttls()
        server.login(user = 'FROM_EMAIL',password='PASSWORD')
        server.send_message(msg)
        server.quit()

Credits

ujjval rathod

ujjval rathod

3 projects • 7 followers

Comments