{"id":43175,"date":"2018-06-09T07:31:50","date_gmt":"2018-06-09T07:31:50","guid":{"rendered":"http:\/\/electronicsmaker.com\/?p=43175"},"modified":"2018-06-09T07:31:50","modified_gmt":"2018-06-09T07:31:50","slug":"iot-based-raspberry-pi-home-security-system-with-email-alert","status":"publish","type":"post","link":"https:\/\/electronicsmaker.com\/iot-based-raspberry-pi-home-security-system-with-email-alert","title":{"rendered":"IOT based Raspberry Pi Home Security System with Email Alert"},"content":{"rendered":"<p><a href=\"\/\/electronicsmaker.com\/wp-content\/uploads\/2018\/06\/abc.jpg\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-43176\" src=\"\/\/electronicsmaker.com\/wp-content\/uploads\/2018\/06\/abc.jpg\" alt=\"abc\" width=\"650\" height=\"467\" srcset=\"https:\/\/electronicsmaker.com\/wp-content\/uploads\/2018\/06\/abc.jpg 650w, https:\/\/electronicsmaker.com\/wp-content\/uploads\/2018\/06\/abc-300x215.jpg 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a>In the world of\u00a0Internet of Things (IoT)\u00a0when we have all the technologies to revolutionize our life, it&#8217;s a great idea to develop a\u00a0system which can be controlled and monitored from anywhere.\u00a0 There are many types of good security systems and cameras out there for home security but they are much expensive so today we will build a low cost simple\u00a0<strong>Raspberry Pi based Intruder Alert System<\/strong>, which not only alert you through an email but also sends the picture of Intruder when\u00a0it detects any.<\/p>\n<p>In this\u00a0IoT based Project, we will build a<strong>\u00a0Home Security System using PIR Sensor and PI Camera<\/strong>. This system will detect the presence of Intruder and quickly alert the user by sending him an alert mail. This mail will also contain the Picture of the Intruder, captured by Pi camera. Raspberry Pi is used to control the whole system. This system can be installed at the main door of your home or office and you can monitor it from anywhere in the world using your Email over internet.<\/p>\n<h3>Components Required:<\/h3>\n<ul>\n<li>Raspberry Pi<\/li>\n<li>Pi Camera<\/li>\n<li>PIR Sensor<\/li>\n<li>LED<\/li>\n<li>Bread Board<\/li>\n<li>Resistor (1k)<\/li>\n<li>Connecting wires<\/li>\n<li>Power supply<\/li>\n<\/ul>\n<h3>Working Explanation:<\/h3>\n<p>Working of this Project is very simple. A PIR sensor is used to detect the presence of any person and a Pi Camera is used to capture the images when the presence it detected.<\/p>\n<p>Whenever anyone or intruder comes in range of PIR sensor,\u00a0<strong>PIR Sensor triggers the Pi Camera through Raspberry Pi<\/strong>. Raspberry pi sends commands to Pi camera to click the picture and save it. After it, Raspberry Pi creates a mail and sends it to the defined mail address with recently clicked images. The mail contains a message and picture of intruder as attachment. Here we have used the message \u201cPlease find the attachment\u201d, you can change it accordingly in the Code given at the end.<\/p>\n<p>Here the pictures are saved in Raspberry Pi with the name which itself contains the time and date of entry. So that we can check the time and date of intruder entry by just looking at the Picture name, check the images below. If you are new with Pi Camera then check our previous tutorial on\u00a0Visitor Monitoring System with Pi Camera.<\/p>\n<p>You can also adjust the detection range or distance of this system using PIR sensor\u2019s potentiometers. Learn more about\u00a0PIR sensor here\u00a0to adjust the range also check\u00a0PIR sensor based Burglar alarm.<\/p>\n<h3><a href=\"\/\/electronicsmaker.com\/wp-content\/uploads\/2018\/06\/22.jpg\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-43177\" src=\"\/\/electronicsmaker.com\/wp-content\/uploads\/2018\/06\/22.jpg\" alt=\"2\" width=\"792\" height=\"855\" srcset=\"https:\/\/electronicsmaker.com\/wp-content\/uploads\/2018\/06\/22.jpg 792w, https:\/\/electronicsmaker.com\/wp-content\/uploads\/2018\/06\/22-278x300.jpg 278w\" sizes=\"(max-width: 792px) 100vw, 792px\" \/><\/a>Circuit Description:<\/h3>\n<p>In this\u00a0<strong>Intruder Alert System<\/strong>, we only need to\u00a0<strong>connect Pi Camera module and PIR sensor to Raspberry Pi 3<\/strong>. Pi Camera is connected at the camera slot of the Raspberry Pi and PIR is connected to GPIO pin 18. \u00a0A LED is also connected to GPIO pin 17 through a 1k resistor.<\/p>\n<h3>Raspberry Pi Configuration and Programming Explanation:<\/h3>\n<p>We are using\u00a0<strong>Python language<\/strong>\u00a0here for the Program. Before coding, user needs to configure Raspberry Pi. You should below two tutorials for\u00a0Getting Started with Raspberry Pi\u00a0and\u00a0Installing &amp; Configuring Raspbian Jessie OS\u00a0in Pi:<\/p>\n<ul>\n<li>Getting Started with Raspberry Pi &#8211; Introduction<\/li>\n<li>Getting Started with Raspberry Pi &#8211; Configuration<\/li>\n<\/ul>\n<p>After successfully installing Raspbian OS on Raspberry Pi, we need to\u00a0<strong>install Pi camera library files<\/strong>for run this project in Raspberry pi. To do this we need to follow given commands.<\/p>\n<p>After it, user needs to\u00a0<strong>enable Raspberry Pi Camera<\/strong>\u00a0by using Raspberry Pi Software Configuration Tool\u00a0(raspi-config)<\/p>\n<p>Then user needs to\u00a0<strong>reboot Raspberry Pi<\/strong>, by issuing\u00a0<em>sudo reboot<\/em>, so that new setting can take. Now your Pi camera is ready to use.<\/p>\n<p>Now after setting up the Pi Camera, we will install software for sending the mail. Here we are using\u00a0<em>ssmtp<\/em>\u00a0which is an easy and good solution for\u00a0<strong>sending mail using command line or using Python Script<\/strong>. We need to install two Libraries for sending mails using SMTP.<\/p>\n<p>After installing libraries, user needs to open\u00a0<em>ssmtp.conf<\/em>\u00a0file and edit this configuration file as shown in the Picture below and then save the file. To save and exit the file, Press \u2018CTRL+x\u2019, then \u2018y\u2019 and then press \u2018enter.<\/p>\n<p>We can also\u00a0<strong>test it by sending a test mail\u00a0<\/strong>by issuing below command, you shall get the mail on the mentioned email address if everything is working fine:<\/p>\n<p>The Python Program of this project plays a very important role to perform all the operations. First of all, we include required libraries for email, initialize variables and define pins for PIR, LED and other components. For sending simple email,\u00a0smtplib\u00a0is enough but if you want to send mail in cleaner way with subject line, attachment etc. then you need to use MIME (Multipurpose Internet Mail Extensions).<\/p>\n<p>And now in last, we have read PIR sensor output and when its goes high Raspberry Pi calls the\u00a0<em>capture_image()<\/em>\u00a0function to capture the image of intruder and send a alert message with the picture of intruder as an attachment. We have used\u00a0<em>sendmail()<\/em>\u00a0insdie\u00a0<em>capture_image()<\/em>\u00a0function for sending the mail.<\/p>\n<p>So this how this\u00a0<strong>Raspberry Pi Security System<\/strong>\u00a0works, you can also use\u00a0Ultrasonic sensor\u00a0or\u00a0IR sensor\u00a0to detect the presence of burglar or intruder. Further check the\u00a0<strong>Full\u00a0Code<\/strong>\u00a0and demonstration\u00a0<strong>Video<\/strong>\u00a0below.<\/p>\n<p><strong>Code:\u00a0<\/strong><\/p>\n<p>import RPi.GPIO as gpio<br \/>\nimport picamera<br \/>\nimport time<\/p>\n<p>import smtplib<br \/>\nfrom email.MIMEMultipart import MIMEMultipart<br \/>\nfrom email.MIMEText import MIMEText<br \/>\nfrom email.MIMEBase import MIMEBase<br \/>\nfrom email import encoders<br \/>\nfrom email.mime.image import MIMEImage<\/p>\n<p>fromaddr = &#8220;raspiduino4201@gmail.com&#8221;\u00a0\u00a0 \u00a0# change the email address accordingly<br \/>\ntoaddr = &#8220;saddam4201@gmail.com&#8221;<br \/>\nmail = MIMEMultipart()<br \/>\nmail[&#8216;From&#8217;] = fromaddr<br \/>\nmail[&#8216;To&#8217;] = toaddr<br \/>\nmail[&#8216;Subject&#8217;] = &#8220;Attachment&#8221;<br \/>\nbody = &#8220;Please find the attachment&#8221;<\/p>\n<p>led=17<br \/>\npir=18<br \/>\nHIGH=1<br \/>\nLOW=0<br \/>\ngpio.setwarnings(False)<br \/>\ngpio.setmode(gpio.BCM)<br \/>\ngpio.setup(led, gpio.OUT)\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0# initialize GPIO Pin as outputs<br \/>\ngpio.setup(pir, gpio.IN)\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0# initialize GPIO Pin as input<br \/>\ndata=&#8221;&#8221;<\/p>\n<p>def sendMail(data):<br \/>\nmail.attach(MIMEText(body, &#8216;plain&#8217;))<br \/>\nprint data<br \/>\ndat=&#8217;%s.jpg&#8217;%data<br \/>\nprint dat<br \/>\nattachment = open(dat, &#8216;rb&#8217;)<br \/>\nimage=MIMEImage(attachment.read())<br \/>\nattachment.close()<br \/>\nmail.attach(image)<br \/>\nserver = smtplib.SMTP(&#8216;smtp.gmail.com&#8217;, 587)<br \/>\nserver.starttls()<br \/>\nserver.login(fromaddr, &#8220;your password&#8221;)<br \/>\ntext = mail.as_string()<br \/>\nserver.sendmail(fromaddr, toaddr, text)<br \/>\nserver.quit()<\/p>\n<p>def capture_image():<br \/>\ndata= time.strftime(&#8220;%d_%b_%Y|%H:%M:%S&#8221;)<br \/>\ncamera.start_preview()<br \/>\ntime.sleep(5)<br \/>\nprint data<br \/>\ncamera.capture(&#8216;%s.jpg&#8217;%data)<br \/>\ncamera.stop_preview()<br \/>\ntime.sleep(1)<br \/>\nsendMail(data)<\/p>\n<p>gpio.output(led , 0)<br \/>\ncamera = picamera.PiCamera()<br \/>\ncamera.rotation=180<br \/>\ncamera.awb_mode= &#8216;auto&#8217;<br \/>\ncamera.brightness=55<br \/>\nwhile 1:<br \/>\nif gpio.input(pir)==1:<br \/>\ngpio.output(led, HIGH)<br \/>\ncapture_image()<br \/>\nwhile(gpio.input(pir)==1):<br \/>\ntime.sleep(1)<br \/>\nelse:<br \/>\ngpio.output(led, LOW)<br \/>\ntime.sleep(0.01)<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the world of\u00a0Internet of Things (IoT)\u00a0when we have all the technologies to revolutionize our life, it&#8217;s a great idea to develop a\u00a0system which can be controlled and monitored from anywhere.\u00a0 There are many types of good security systems and cameras out there for home security but they are much expensive so today we will [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":43176,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[1725],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>IOT based Raspberry Pi Home Security System with Email Alert - Electronics Maker<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/electronicsmaker.com\/iot-based-raspberry-pi-home-security-system-with-email-alert\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"IOT based Raspberry Pi Home Security System with Email Alert - Electronics Maker\" \/>\n<meta property=\"og:description\" content=\"In the world of\u00a0Internet of Things (IoT)\u00a0when we have all the technologies to revolutionize our life, it&#8217;s a great idea to develop a\u00a0system which can be controlled and monitored from anywhere.\u00a0 There are many types of good security systems and cameras out there for home security but they are much expensive so today we will [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/electronicsmaker.com\/iot-based-raspberry-pi-home-security-system-with-email-alert\" \/>\n<meta property=\"og:site_name\" content=\"Electronics Maker\" \/>\n<meta property=\"article:published_time\" content=\"2018-06-09T07:31:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/electronicsmaker.com\/wp-content\/uploads\/2018\/06\/abc.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"650\" \/>\n\t<meta property=\"og:image:height\" content=\"467\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\">\n\t<meta name=\"twitter:data1\" content=\"Electronics Maker\">\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data2\" content=\"4 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/electronicsmaker.com\/#website\",\"url\":\"https:\/\/electronicsmaker.com\/\",\"name\":\"Electronics Maker\",\"description\":\"Electronics Magazine\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/electronicsmaker.com\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/electronicsmaker.com\/iot-based-raspberry-pi-home-security-system-with-email-alert#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/electronicsmaker.com\/wp-content\/uploads\/2018\/06\/abc.jpg\",\"width\":650,\"height\":467,\"caption\":\"abc\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/electronicsmaker.com\/iot-based-raspberry-pi-home-security-system-with-email-alert#webpage\",\"url\":\"https:\/\/electronicsmaker.com\/iot-based-raspberry-pi-home-security-system-with-email-alert\",\"name\":\"IOT based Raspberry Pi Home Security System with Email Alert - Electronics Maker\",\"isPartOf\":{\"@id\":\"https:\/\/electronicsmaker.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/electronicsmaker.com\/iot-based-raspberry-pi-home-security-system-with-email-alert#primaryimage\"},\"datePublished\":\"2018-06-09T07:31:50+00:00\",\"dateModified\":\"2018-06-09T07:31:50+00:00\",\"author\":{\"@id\":\"https:\/\/electronicsmaker.com\/#\/schema\/person\/df9e9cfbf590f29e49716824dd7d81f9\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/electronicsmaker.com\/iot-based-raspberry-pi-home-security-system-with-email-alert\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/electronicsmaker.com\/#\/schema\/person\/df9e9cfbf590f29e49716824dd7d81f9\",\"name\":\"Electronics Maker\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/electronicsmaker.com\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a4af77a4fcb00c5dfc7c1ca124a492b4?s=96&d=mm&r=g\",\"caption\":\"Electronics Maker\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/electronicsmaker.com\/wp-json\/wp\/v2\/posts\/43175"}],"collection":[{"href":"https:\/\/electronicsmaker.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/electronicsmaker.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/electronicsmaker.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/electronicsmaker.com\/wp-json\/wp\/v2\/comments?post=43175"}],"version-history":[{"count":1,"href":"https:\/\/electronicsmaker.com\/wp-json\/wp\/v2\/posts\/43175\/revisions"}],"predecessor-version":[{"id":43178,"href":"https:\/\/electronicsmaker.com\/wp-json\/wp\/v2\/posts\/43175\/revisions\/43178"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/electronicsmaker.com\/wp-json\/wp\/v2\/media\/43176"}],"wp:attachment":[{"href":"https:\/\/electronicsmaker.com\/wp-json\/wp\/v2\/media?parent=43175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/electronicsmaker.com\/wp-json\/wp\/v2\/categories?post=43175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/electronicsmaker.com\/wp-json\/wp\/v2\/tags?post=43175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}