• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
MechatronicsLAB

MechatronicsLAB

Technology Solution

  • Complete Guide
    • Complete Guide to Raspberry Pi4
    • MicroPython
  • Blog
    • Raspberry pi Project
    • Raspberry Pi
    • Computer Vision Beginner
    • ESP8266
    • Raspberry Pi Tutorials For Beginners
    • Internet of Things
    • Arduino Tutorial
  • About Me
  • Tutorial
    • Electronics Mini Projects
    • Electronics Theory

Raspberry Pi PIR Sensor Email Notification Alarm

January 1, 2021 by Sarful Hassan

This project will create an alarm that will send you email notifications(Raspberry Pi Email Notification). This alarm will detect if someone has entered a particular area. If someone enters that specific area, it will detect it through the sensor and immediately send you an email to know that someone has entered your specific area. For this, we will use the PIR sensor. 


Table of Contents

  • ≡Required Components:
  • ≡Introduction to PIR Sensor :
  • Email-Sending code
  • ≡Script Analysis:
  • Email-sending Script Run
  • ≡Raspberry Pi Email Notification Circuit Wiring
  • ≡Raspberry Pi Email Notification Script Writing:
  •  Script Analysis: 

≡Required Components:


You have to need a flowing component for Raspberry Pi Email Notification in python code project.

  1. Raspberry Pi
  2. Breadboard
  3. PIR motion sensor HC-SR501
  4. Two 5 mm LEDs (different colours)
  5. Two 330 Ω Resistors
  6.  Pushbutton
  7. Jumper wires

Lean python

You have to need good knowledge about python language for doing the Rasberry pi project, you can learn python from here.


≡Introduction to PIR Sensor :


You have probably seen motion sensors in various applications. In some cases, they are used in security lights in commercial building lights that turn on when you walk and turn off again when you go out on the street. 

A PIR motion sensor (see Figure 1) measures the infrared light emitted from the object. It detects motion based on changes in infrared light. It is ideal for identifying people or animals, as it detects the movement of living things. 

You can easily create programs based on infrared lights. In this case, you can create programs for various other tasks, including lights on and off, alarms. In this case, we will show you how to send email based on PIR sensor motion in this project. 

PIR motion sensor
Figure 1: PIR motion sensor

The sensor’s output is HIGH only when the sensor detects any movement, and the output is always low. This sensor has three pins VCC, GND, and a data pin. From the output of the sensor, we get 3.3v Perfect for Raspberry Pi. 


≡ Python Programming E-mail for Send:


you can easily create programs using Python Live.

SMTP Server

 You need to know about the Simple Mail Transfer Protocol (SMTP) server to send email through the program.  SMTP is an Internet standard email transmission system. Each email provider has a separate SMTP server. 

The service provider’s service uses the server’s address and port, and it uses Transport Layer Security (TLS). TLS is a protocol for establishing secure connections between two e-mail servers. You need to connect to the Internet for SMTP server settings to get email provider name information.

Email-Sending code

NOTE

Don’t name your file email.py because that’s a Python library name, and your script won’t work.

Now take out the python IDE of Raspberry Pi and write the following program there and when the writing is done send it to send_email.py  Name must be saved in the sensor folder.

The email notification script

➊ import smtplib
  from email.mime.text import MIMEText

  #replace the next three lines with your credentials
➋ from_email_addr = '[email protected]'
  from_email_password = 'YOUR_EMAIL_PASSWORD'
  to_email_addr = '[email protected]'

  #set your email message
➌ body = 'Motion was detected in your room.'
  msg = MIMEText (body)

  #set sender and recipient
  msg ['From'] = from_email_addr
  msg ['To'] = to_email_addr

  #set your email subject
  msg ['Subject'] = 'INTRUDER ALERT'

  #connecting to server and sending email
  #edit the following line with your provider's SMTP server details
➍ server = smtplib.SMTP ('smtp.gmail.com', 587)
  #comment out the next line if your email provider doesn't use TLS
  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')

≡Script Analysis:


➊ import smtplib
from email.mime.text import MIMEText

libraries for emails have been imported.

➋ from_email_addr = '[email protected]'  
 from_email_password = 'YOUR_EMAIL_PASSWORD'   
to_email_addr = '[email protected]'

That you want to send e-mail to this address has been created for the variables and these variables in your email address and password that you address Add the address you want to send the e-mail to. 

In this case, I will ask you to create a separate email because the e-mail you will use here will be much less secure so create a new email for your own safety and use it here. 

➌ body = 'Motion was detected in your room.'  
 msg = MIMEText (body)   #set sender and recipient   
msg ['From'] = from_email_addr   
msg ['To'] = to_email_addr   #set your email subject   
msg ['Subject'] = 'INTRUDER ALERT'

Here a variable called body has been created where you will save the message you want to love. A variable called msg has been created, which will create an email using msg = MIMEText (body). Enter the subject you want to send the email to msg [‘Subject’] = ‘INTRUDER ALERT’.  

➍ server = smtplib.SMTP ('smtp.gmail.com', 587)   #comment out the next line if your email provider doesn't use TLS  
 server.starttls ()

Here is the communication with the SMTP server. smtplib.SMTP (‘smtp.gmail.com’, 587) Here we see that two arguments have been passed: the server address and the other port. Gmail SMTP server and port are used here. Here you can change the server address and its needs. The server. starttls () function is required for the email provider and encrypts the message using TLS. This line will be required if the TLS system is not used in the email provider email you will be using.

➎ server.login (from_email_addr, from_email_password)   
server.sendmail (from_email_addr, to_email_addr, msg.as_string ())  
 server.quit ()  
 print ('Email sent')

This section, the e-mail you want to send e-mail to. It will also disconnect from the server when the email is finished and immediately display a message to confirm that your email has been sent.

Email-sending Script Run

Now you can save and run the. Run the program. You will see a new image 10-2 message in your email. 

Email sent with send_email.py
Figure 2: Email sent with send_email.py

 If you have not received an email, check that the email and SMTP information in send_email.py is correct. Also, verify that your account has been granted permission to use.


≡Raspberry Pi Email Notification Circuit Wiring


Now I will show you how to connect the PIR sensor with Raspberry Pi to detect movement. Two LEDs are used as indicators of this circuit, and a push-button switch is used to detect the sensor’s movement, one LED will glow, and another LED when the push button is pressed. 

Follow the steps below to create a circuit as shown in 

Circuit for the intruder alarm
Figure 3: Circuit for the intruder alarm
  1. Connect the GND of the raspberry pie to one of the blue rails on the breadboard.
  2. Attach a red LED to the breadboard and a green negative edge to the negative reel of the breadboard and attach the red LED positive 330 তে to the positive edge GPIO 18 through the register, red LED positive 330. সং Connect the positive edge to GPIO 18 through the register.
  3. Place the pushbutton in the centre of the breadboard, as shown in Figure 3. Connect the pin on the GND rail’s bottom right and the pin on the bottom left to GPIO 2.
  4. Connect the PIR motion sensor to the connections in the following table.

 

PIR MOTION SENSOR RASPBERRY PI
GND GND
OUT GPIO 4
VCC 5 V

 


≡Raspberry Pi Email Notification Script Writing:


Now get out the python IDE of Raspberry Pi and write the following program there and when you finish writing it, it is intruder_alarm.py  Name must be saved in the sensor folder. 

 The intruder alarm script

 

  #import necessary libraries
➊ from gpiozero import LED, Button, MotionSensor
  import smtplib
  from email.mime.text import MIMEText
  from signal import pause

  #create objects to refer to each LED, the button, and the PIR sensor
➋ led_status = LED (17)
  led_triggered = LED (18)
  button = Button (2)
  pir = MotionSensor (4)

  #control variables ➌
motion_sensor_status = False
  email_sent = False

  #arm or disarm the PIR sensor
➍ def arm_motion_sensor():
      global email_sent
      global motion_sensor_status
      if motion_sensor_status == True:
          motion_sensor_status = False
          led_status.off ()
          led_triggered.off ()
      else:
          motion_sensor_status = True
          email_sent = False
          led_status.on ()

  #send email when motion is detected and the PIR sensor is armed
➎ def send_email():
      global email_sent
      global motion_sensor_status
      if(motion_sensor_status == True and email_sent == False):

          #replace the next three lines with your credentials
          from_email_addr = '[email protected]'
          from_email_password = 'YOUR_EMAIL_PASSWORD'
          to_email_addr = '[email protected]'

        #set your email message
          body = 'Motion was detected in your room.'
          msg = MIMEText (body)

          #set sender and recipient
          msg ['From'] = from_email_addr
          msg ['To'] = to_email_addr

          #set your email subject
          msg ['Subject'] = 'INTRUDER ALERT'

          #connect to server and send email
          #edit this line with your provider's SMTP server details
          server = smtplib.SMTP ('smtp.gmail.com', 587)
          #comment out this line if your provider doesn't use TLS
          server.starttls ()
          server.login (from_email_addr, from_email_password)
          server.sendmail (from_email_addr, to_email_addr,
  msg.as_string ())
          server.quit ()
          email_sent = True
          led_triggered. on ()
          print ('Email sent')

  #assign a function that runs when the button is pressed
➏ button.when_pressed = arm_motion_sensor
  #assign a function that runs when motion is detected
➐ pir.when_motion = send_email ➑

pause ()

 Script Analysis: 

➊ from gpiozero import LED, Button, MotionSensor

  import smtplib

  from email. Mime. text import MIMEText

  from signal import pause

 

Import here Library Done. 

➋ led_status = LED (17)

  led_triggered = LED (18)

  button = Button (2)

  pir = MotionSensor (4)

Here separate objects have been created to store LED, button, PIR sensor values. 

 

➌ motion_sensor_status = False

  email_sent = TheFalse

motion_sensor_status and email_sent control variables have been created to detect if themotion sensor was triggered and an email was sent.

Arm def arm_motion_sensor ():

      global email_sent

      global motion_sensor_status

def arm_motion_sensor () This function is designed to control the motion sensor and push button switch status. 

      if motion_sensor_status == True:

          motion_sensor_status = False

          led_status.off ()

          led_triggered.off ()

      else:

          motion_sensor_status = True

          email_sent = False

          led_status.on ()

Here if condation is used because if the motion is detected or the condensation is true Will. 

 ➎ def send_email ():

      global email_sent

      global motion_sensor_status

      if (motion_sensor_status == True and email_sent == False):

This function will activate the def send_email () function to send mail when the motion sensor detects movement. 

           from_email_addr = ‘[email protected]’ 

first in line at three in the e-mail message you want to send me the second you want to send messages from Gmail email and password The third is the email address to which you want to send your message. 

          body = ‘Motion was detected in your room.’

          msg = MIMEText (body)

Write here the message you want to send

                  msg [ ‘From’] = from_email_addr       

Message Center and the message is set to receive 

          msg [ ‘Subject’] = ‘

 the message you want to give me on that subject

                 server = smtplib.SMTP (‘smtp.gmail.com’, 587)

Here the message server address and phone number are given. You change this address and port according to your server’s needs Can give.

          server.starttls ()

          server.login (from_email_addr, from_email_password)

          server.sendmail (from_email_addr, to_email_addr,

  msg.as_string ())

          server.quit ()

          email_sent = True

          led_triggered.on ()

          print (‘Email sent’)

If you TLS Use a protocol server but this line is required or these lines are not required. 

➏ button.when_pressed = arm_motion_sensor

    Thewhen the button is pressed arm_motion_sensor () function calls. 

Ir pir.when_motion = send_emailBIR 

When thedetects the sensor movement, the send_email () function calls. 

➑ pause () 

pause () function is used to detect the next event after the program is finished. 

 

Raspberry Pi Email Notification Running script:

 first, you have to save or save the above program, then you have to run Run ▸ Run current script or press F5 from the keyboard then the program will run.

Check the alarm with your hand in front of the motion sensor. You will receive a new message in your inbox, and the triggered green LED will light up.

 

RASPBERRY PI TUTORIALS FOR BEGINNERS

 1,043 total views,  20 views today

Recent Posts

  • Getting Started with a Raspberry pi Terminal
  • How to Setup Raspberry Pi Remote Access with 2 Method
  • How to Connect Raspberry pi to wifi with 3 Easy Method
  • How to Set Static IP Raspberry pi with 2 Method 
  • How to Find ip Address of Raspberry Pi with simple 2 Method
  • How to Connect Raspberry Pi to Monitor
  • How to choose Raspberry Pi4 Power Supply
  • Why Need Enclosure for Raspberry Pi 4
  • Couplet Setup Your Raspberry pi 4
  • Which Raspberry Pi Model Need Your Project
  • Privacy Policy for Mechatronicslab
  • Terms and condition

Copyright © 2021 mechatronicslab. Disclaimer: This is not a part of Google, the Facebook website, or Facebook Inc… also, this site is not endorsed by Google or Facebook in any way, Facebook is a trademark of Facebook Inc.