chelseymh
Posts: 2
Joined: Thu Oct 19, 2017 7:34 am

Security System - PIR Motion, PiCamera, GMail

Thu Oct 19, 2017 7:40 am

Hi!

I am trying to simply set up a security system. I have the PIR motion sensor and Pi Camera working fine. But it does not send an email (I've followed countless online tutorials - so, I have mpack and all the other email requirements installed) (Gmail is allowing insecure apps too). It shows no errors when running my program.

Here is my script:

Code: Select all

import RPi.GPIO as GPIO
from gpiozero import MotionSensor
from picamera import PiCamera
from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
import email.encoders
import smtplib
import os
import email
import sys
import time

camera = PiCamera()
jpg_image = ".jpg"
camera.rotation = 180 # delete or adjust to 90, 180, or 270 accordingly
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

GPIO.setup(11, GPIO.IN)         #Read output from PIR motion sensor
GPIO.setup(3, GPIO.OUT)         #LED output pin

fromaddr = "EMAIL"
toaddr = "EMAIL"

# prepare the email
f_time = datetime.now().strftime("%A %B %d %Y @ %H:%M:%S")
msg = MIMEMultipart()
msg["Subject"] = f_time
msg["From"] = fromaddr
msg["To"] = toaddr
text = MIMEText("WARNING! Motion Detected!")
msg.attach(text)

while True:
       i=GPIO.input(11)
       if i==0:                 #When output from motion sensor is LOW
            GPIO.output(3, 0)  #Turn OFF LED
            time.sleep(0.1)
       elif i==1:               #When output from motion sensor is HIGH
            GPIO.output(3, 1)  #Turn ON LED
            print "Intruder detected!"
            image_name = datetime.now().strftime('%m-%d-%Y_%H.%M.%S')
            camera.capture(image_name + jpg_image)
            footage = image_name + jpg_image
            print "Image captured!"
part = MIMEBase("application", "octet-stream")
part.set_payload(open(footage, "rb").read())
email.encoders.encode_base64(part)
part.add_header("Content-Disposition", "attachment; filename= %s" % os.path.basename(footage))
msg.attach(part)
server = smtplib.SMTP("smtp.gmail.com:587")
server.starttls()
server.login(fromaddr, "PASSWORD")
server.sendmail(fromaddr, toaddr, msg.as_string())
print "Email sent!"
server.quit()
Just as a note, I did change out 'EMAIL' and 'PASSWORD'.

I am a total beginner so any help or advice is welcome!

Thanks :)

jbudd
Posts: 311
Joined: Mon Dec 16, 2013 10:23 am

Re: Security System - PIR Motion, PiCamera, GMail

Thu Oct 19, 2017 11:39 pm

Code blocks in Python are defined by their indentation.
Your code to send an email is not indented, so it is not part of the While True : block and is never executed.

chelseymh
Posts: 2
Joined: Thu Oct 19, 2017 7:34 am

Re: Security System - PIR Motion, PiCamera, GMail

Fri Oct 20, 2017 3:12 am

Thanks. If I indent it in line with the rest, i get this error:

Code: Select all

  File "securitysystem.py", line 48
    part = MIMEBase("application", "octet-stream")
    ^
IndentationError: unexpected indent

nksheridan
Posts: 2
Joined: Sun Mar 12, 2017 8:57 pm

Re: Security System - PIR Motion, PiCamera, GMail

Mon Oct 23, 2017 7:26 pm

Hi!

I just tried it quickly, and it worked fine like this:

Code: Select all

while True:
       i=1
       if i==0:                 #When output from motion sensor is LOW
            GPIO.output(3, 0)  #Turn OFF LED
            time.sleep(0.1)
       elif i==1:               #When output from motion sensor is HIGH
            #GPIO.output(3, 1)  #Turn ON LED
            print "Intruder detected!"
            #image_name = datetime.now().strftime('%m-%d-%Y_%H.%M.%S')
            #camera.capture(image_name + jpg_image)
            #footage = image_name + jpg_image
            print "Image captured!"
            print "hello"
            part = MIMEBase("application", "octet-stream")

chelseymh wrote:
Fri Oct 20, 2017 3:12 am
Thanks. If I indent it in line with the rest, i get this error:

Code: Select all

  File "securitysystem.py", line 48
    part = MIMEBase("application", "octet-stream")
    ^
IndentationError: unexpected indent

User avatar
James Pannell
Posts: 4
Joined: Sun Oct 01, 2017 11:10 am

Re: Security System - PIR Motion, PiCamera, GMail

Tue Oct 24, 2017 12:01 pm

There is a nice tool called "Python Beautify" over @ Dan's Tools website, simply paste in the code and it will indent it for you.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 38 guests