Sunday, April 5, 2020

WEEK 6- Code Explanation

Date: 18 March 2020

OBJECTIVE:

- To give explanation about my project code.

CONTENT/ PROCEDURE:

First, we add the packages and modules required for this project. The GPIO package helps up to control the GPIO pins of our Raspberry Pi. The Picamera package is used for the Raspberry Pi camera, while the Smtplib package allows us to set up the Gmail server.


        import os
import glob
import picamera
import RPi.GPIO as GPIO
import smtplib
from time import sleep

We need the four modules below to send emails from a Raspberry Pi:

        from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
We need to add the receiver's email and sender's email address and password, as shown below:

sender = 'example@gmail.com' password = 'abcd1234' receiver = 'example@gmail.com'


We can then set the directory path and the file prefix for where the images will be saved:

DIR = './Database/' FILE_PREFIX = 'image'


Then, because we are going to receive the output of the sensor, we declare pin 11 as input. Any GPIO warnings should be ignored.


        GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.IN)  # Read output from PIR motion sensor
In the send_mail() function, the system will check whether the database directory has already been created or not. If it is not created, one will be created. It will then look for the ID of the images and save the new image with a new ID. Then the most recent image will be attached to the email and sent to the receiver’s address.


        def send_mail():
    print 'Sending E-Mail'
    # Create the directory if not exists
    if not os.path.exists(DIR):
        os.makedirs(DIR)
    # Find the largest ID of existing images.
    # Start new images after this ID value.
    files = sorted(glob.glob(os.path.join(DIR, FILE_PREFIX + '[0-9][0-9][0-9].jpg')))
    count = 0
    
    if len(files) > 0:
        # Grab the count from the last filename.
        count = int(files[-1][-7:-4])+1

    # Save image to file
    filename = os.path.join(DIR, FILE_PREFIX + '%03d.jpg' % count)
    # Capture the face
    with picamera.PiCamera() as camera:
        pic = camera.capture(filename)
    # Sending mail
    msg = MIMEMultipart()
    msg['From'] = sender
    msg['To'] = receiver
    msg['Subject'] = 'Movement Detected'
    
    body = 'Picture is Attached.'
    msg.attach(MIMEText(body, 'plain'))
    attachment = open(filename, 'rb')
    part = MIMEBase('application', 'octet-stream')
    part.set_payload((attachment).read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename= %s' % filename)
    msg.attach(part)
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(sender, password)
    text = msg.as_string()
    server.sendmail(sender, receiver, text)
    server.quit()

In the while loop, it will read the input from pin 11 and, if the input is equal to 1, the send_mail() function will be called to send an email.
while True:
    i = GPIO.input(11)
    if i == 0:  # When output from motion sensor is LOW
        print "No intruders", i
        sleep(0.3)
    elif i == 1:  # When output from motion sensor is HIGH
        print "Intruder detected", i
        send_mail()
CONCLUSION:

As a conclusion from the explanation, i know about my project code function. Next, i need to create email for my project to send email that contain image to my email.

No comments:

Post a Comment