If you have watched my previous tutorial motion detection email alert, you may wondering if we can send email notification when it detects motion. It will be great if we can send directly picture of intruder. Yes, we can do that. In this tutorial we are going to make burglar detection with image capture email notification. As usual we are going to use PIR sensor HC-SR501 for detecting motion. In this tutorial we are using Pi Camera, which will capture image as soon as it detects intruder and send that captured image to owner via email. So, let’s get started.
- Raspberry pi 4,
- Push Button,
- Pi Cam V2.1,
- PIR sensor (HC-SR501),
- 220 ohms Resistor,
- Jumper Wire.
Enable Camera of Raspberry Pi:
Go to Menu-> Preferences -> Raspberry pi configuration -> interfaces -> Enable Camera
Create New Gmail Account (For sending Notification):
I suggest you to create new Gmail or Yahoo account for raspberry pi notification sender email. Because you will be giving less secure apps access to the account you send from. Make sure you give access to less secure apps, otherwise email won’t be sent.
Find SMTP Server Setting for Email Account:
Simple Mail Transfer Portocol (SMTP): For sending email through code, You will need a SMTP server setting. Each email provider has different SMTP server. Here I am using gmail account so, I am using gmail SMTP server setting, If you are not using gmail for sending notification, you have to use your email provider’s SMTP server setting.
Gmail SMTP Server Settings:
Gmail SMTP server address: smtp.gmail.com
Gmail SMTP Port TLS: 587
Gmail SMTP Port SSL: 465
Yahoo Mail SMTP Server Settings:
Yahoo Mail SMTP server address: smtp.mail.yahoo.com
Yahoo Mail SMTP port TLS: 587
Yahoo Mail SMTP port SSL: 465
If PIR sensor is not working:
It happens when you buy new sensor, It is not working. But it is not sensor fault, it is trim port which is in by default position. if you adjust sensor sensitivity and trigger time port. It will start to work as expected. Make sure knob of trigger time port is to the left side as low trigger time and sensitivity port at middle.
Circuit Diagram for Burglar Detector with photo captured email alert.
PIR Sensor – GPIO20
LED – GPIO17
Push Button – GPIO16
Code for Burglar Detector with photo captured email notification.
from gpiozero import MotionSensor, Button, LED from picamera import PiCamera from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart import datetime import smtplib import time from time import sleep pir = MotionSensor(20) camera = PiCamera() led = LED(17) button = Button(16) #replace the next three lines with your credentials from_email_addr = 'Sender_Email@gmail.com' from_email_password = 'Sender_Password' to_email_addr = 'Receiver_Email@gmail.com' #Create Alarm State by default it off. Alarm_state = False while True: if button.is_pressed: Alarm_state = True print('Alarm ON') if Alarm_state == True: led.on() sleep(1) if pir.motion_detected: print("Motion Detected") led.off() #capture images Captured1 = '/home/pi/Desktop/image' + datetime.datetime.now().strftime('%Y-%m-%d%H:%M:%S') + '.png' camera.rotation = 180 camera.capture(Captured1) sleep(1) Captured2 = '/home/pi/Desktop/image' + datetime.datetime.now().strftime('%Y-%m-%d%H:%M:%S') + '.png' camera.rotation = 180 camera.capture(Captured2) print("Image Captured") #Message msg = MIMEMultipart() msg[ 'Subject'] = 'INTRUDER ALERT..!!' msg['From'] = from_email_addr msg['To'] = to_email_addr #Attach the files File = open(Captured1, 'rb') img1 = MIMEImage(File.read()) File.close() msg.attach(img1) File = open(Captured2, 'rb') img2 = MIMEImage(File.read()) File.close() msg.attach(img2) print("attach successful") #send Mail server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(from_email_addr, from_email_password) server.sendmail(from_email_addr, to_email_addr, msg.as_string()) server.quit() print('Email sent') Alarm_state = False
Adding PIR sensor, Button & LED from gpiozero library
from gpiozero import MotionSensor, Button, LED
Importing Pi Camera library.
from picamera import PiCamera
STMP library and email related library for sending image.
from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart import smtplib
Datetime and time library for naming unique name for captured image.
import datetime import time
Sleep imported for time delay in program execution. We want time delay between two captured images.
from time import sleep
Creating an object that refer to PIR sensor, LED, PiCamera and button.
pir = MotionSensor(20) camera = PiCamera() led = LED(17) button = Button(16)
Modify this with your sender email address and password. Enter email address where you want to receive email.
from_email_addr = 'Sender_email@gmail.com' from_email_password = 'Sender_email_Password' to_email_addr = 'Receiver_email@gmail.com'
When program execute, alarm mode will be off by default.
Alarm_state = False
This will make script run in loop.
For turning ON alarm mode, we need to push the button, if button pressed, it will make alarm true and print alarm ON.
if button.is_pressed: Alarm_state = True print('Alarm ON')
After pressing the button, LED will light up, shows the alarm mode is ON.
if Alarm_state == True: led.on() sleep(1)
LED will be off after motion detected and shows alarm mode is OFF.
if pir.motion_detected: print("Motion Detected") led.off()
captured1 store directory and file name with date & time. This directory is for captured photo 1. we will capture two photos with one second delay. You can add photos as many as possible. But adding more photos will take more time to send the email. Because each photo get 2.5MB space. It will require more time to upload that photos.
Captured1 = '/home/pi/Desktop/image' + datetime.datetime.now().strftime('%Y-%m-%d%H:%M:%S') + '.png'
Depends on your camera orientation, you can change rotation degree.
camera.rotation = 180
Capture the image and save it in Captured1 directory.
similarly it is done for second photo.
Creating an email message.
msg = MIMEMultipart() msg[ 'Subject'] = 'INTRUDER ALERT..!!' msg['From'] = from_email_addr msg['To'] = to_email_addr
Now we are attaching the captured images to the email. Similarly it is done for image 2.
File = open(Captured1, 'rb') img1 = MIMEImage(File.read()) File.close() msg.attach(img1)
If you are using gmail account for sending notification, You don’t need to change this. If you are using other email provider, you need to change SMTP server address and Port. It will establish connection to SMTP server.
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls() is necessary for email provider to use Transport Layer Security (TLS) to encrypt messages. If email provider doesn’t use TLS then remove or comment out this line.
login with provided email id and password and send the mail. Finally stop communication with server.
server.login(from_email_addr, from_email_password) server.sendmail(from_email_addr, to_email_addr, msg.as_string()) server.quit()
After sending email, it will turn off alarm mode by making Alarm_state False. We are using this alarm mode ON/OFF to prevent sending large number of emails in short period of time. If it sends large number of email in short period of time, google may block/suspend your account. You have to again press push button to turn ON alarm mode.
Alarm_state = False
This is how it will detect intruder/burglar and send his photo via email. If you like my content please subscribe to my YouTube Channel. Till then Keep Learning Keep Making.