Evan Rust
Published © GPL3+

The Pi Guardian

See who just entered your room remotely with this Raspberry Pi Camera-powered device.

IntermediateFull instructions provided2 hours2,553
The Pi Guardian

Things used in this project

Hardware components

DFRobot Raspberry Pi 3
×1
DFRobot Raspberry Pi Camera Module
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1

Software apps and online services

Python IDE of Choice

Story

Read more

Schematics

Schematic

Code

Python Code

Python
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
import picamera
import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)

subject = "An intruder has been detected"
to = "Your Google email address"
UserName = to
UserPassword = "Your Google Password"

camera = picamera.PiCamera()

def SendMail(ImgFileName):
    img_data = open(ImgFileName, 'rb').read()
    msg = MIMEMultipart()
    msg['Subject'] = subject
    msg['From'] = to
    msg['To'] = to

    text = MIMEText("Here is an attached image of the intruder")
    msg.attach(text)
    image = MIMEImage(img_data, name=os.path.basename(ImgFileName))
    msg.attach(image)

    s = smtplib.SMTP("smtp.gmail.com:587")
    s.ehlo()
    s.starttls()
    s.ehlo()
    s.login(UserName, UserPassword)
    s.sendmail(to, to, msg.as_string())
    print("Sending mail")
    s.quit()

def takePicture(channel):
    camera.capture('image.jpg')
    SendMail('image.jpg')  
    os.remove('image.jpg')
    sleep(20)
    

GPIO.add_event_detect(4, GPIO.RISING, callback=takePicture, bouncetime=500)

while True:
    pass

Credits

Evan Rust

Evan Rust

120 projects • 1053 followers
IoT, web, and embedded systems enthusiast. Contact me for product reviews or custom project requests.

Comments