Wednesday, December 18, 2024
How ToIoT HardwaresRaspberry PiTutorials/DIY

Simple Raspberry Pi Home Security System

Hello Visitors, Today i am telling you How to make a simple mini project, Raspberry Pi Home Security System with Camera and PIR Sensor.
In this tutorial we are capturing image when motion is detected with Raspberry Pi camera and sending that image as email notification. I have used python to write the script. You should have two email accounts (I have used gmail), to send and receive emails. I have connected PIR sensor on GPIO 23. If your gmail uses SSL certificates you might need another port that 587.

Things used in this project

Hardware components

  • Raspberry Pi 3 Model B
  • Raspberry Pi Camera Module
  • PIR Motion Sensor (generic)

Software apps and online services

  • gmail account
  • ssh connection

Code

It is a simple python code just change the emails as you have.

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_address'
me = 'FROM_EMAIL_address'
Subject='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()

Schematics

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.

pi

picam

I am always looking forward to discussion about the project and comments, suggestions, etc.


You may like also:


 

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin