- Views: 3
- Report Article
- Articles
- Computers
- Programming
Build home security system with email notification

Posted: Sep 27, 2019
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
The Internet of Things (IoT) is the network of things (physical devices, vehicles, home appliances, and other items embedded with electronics, software, sensors, actuators) connected through internet which enables these things to exchange data. This creates opportunities for more direct integration of the physical world into computer-based systems, resulting in efficiency improvements, economic benefits, and reduced human exertions.
In simple terms we can say IoT is a system of things integrated with sensors, softwares, electronics which are connected to each other & can exchange data or Information with other connected devices.
The Sensors & devices can be like temperature sensor, motion sensor, camera, GPS etc. and to connect these devices each others there should be a medium like Internet ( WiFi), Bluetooth, satellite, Cellular etc. And last the data send between the devices need to be stored & analyzed like in cloud, data analytics software etc.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.a
Program 1 : This is the extended version which I used for normal email alert when motion detected.import RPi.GPIO as GPIO
import picamerafrom picamera import PiCameraimport timeimport smtplibfrom email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encodersfrom 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 datacamera.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 0while GPIO.input(PIRSensor)==1:
Current_State = 0
print " Ready"
Loop until users quits with CTRL-Cwhile True :Read PIR stateCurrent_State = GPIO.input(PIRSensor)
if Current_State==1 and Previous_State==0:
PIR is triggeredprint " 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 statePrevious_State=1
elif Current_State==0 and Previous_State==1:
PIR has returned to ready stateprint " Ready"
Previous_State=0
Wait for 10 millisecondstime.sleep(0.05)
except KeyboardInterrupt:print " Quit"
Reset GPIO settingsGPIO.cleanup()
About the Author
Freelance Blogger www.thetips4you.com
Rate this Article
Leave a Comment
