Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Allow less secure apps to access your Gmail account
- from gpiozero import MotionSensor
- from picamera import PiCamera
- from datetime import datetime
- from email.mime.multipart import MIMEMultipart
- from email.mime.image import MIMEImage
- from email.mime.text import MIMEText
- import smtplib
- import os
- import email
- import sys
- import time
- camera = PiCamera()
- pir = MotionSensor(4)
- camera.rotation = 180 # specific to my setup
- while True:
- pir.wait_for_motion()
- filename = datetime.now().strftime("%m-%d-%Y_%H.%M.%S.jpg")
- time.sleep(1) # unsure how critical this .sleep camera warm-up is, still testing
- camera.capture(filename)
- pir.wait_for_no_motion()
- img_data = open(filename, "rb").read()
- f_time = datetime.now().strftime("%A %B %d %Y @ %H:%M:%S")
- msg = MIMEMultipart()
- msg["Subject"] = f_time
- msg["From"] = "yourAddress@gmail.com"
- msg["To"] = "toAddress@mail.com"
- text = MIMEText("WARNING! Motion Detected!")
- msg.attach(text)
- image = MIMEImage(img_data, name=os.path.basename(filename))
- msg.attach(image)
- s = smtplib.SMTP('smtp.gmail.com:587')
- s.starttls()
- s.login('yourGmailLogin','yourPassword')
- s.sendmail("yourAddress@gmail.com", "toAddress@mail.com", msg.as_string())
- s.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement