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()
I am a total beginner so any help or advice is welcome!
Thanks
