Advertisement
Guest User

Untitled

a guest
May 9th, 2017
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. # Allow less secure apps to access your Gmail account
  2.  
  3. from gpiozero import MotionSensor
  4. from picamera import PiCamera
  5. from datetime import datetime
  6. from email.mime.multipart import MIMEMultipart
  7. from email.mime.image import MIMEImage
  8. from email.mime.text import MIMEText
  9. import smtplib
  10. import os
  11. import email
  12. import sys
  13. import time
  14.  
  15. camera = PiCamera()
  16. pir = MotionSensor(4)
  17. camera.rotation = 180 # specific to my setup
  18.  
  19. while True:
  20. pir.wait_for_motion()
  21. filename = datetime.now().strftime("%m-%d-%Y_%H.%M.%S.jpg")
  22. time.sleep(1) # unsure how critical this .sleep camera warm-up is, still testing
  23. camera.capture(filename)
  24. pir.wait_for_no_motion()
  25. img_data = open(filename, "rb").read()
  26. f_time = datetime.now().strftime("%A %B %d %Y @ %H:%M:%S")
  27. msg = MIMEMultipart()
  28. msg["Subject"] = f_time
  29. msg["From"] = "yourAddress@gmail.com"
  30. msg["To"] = "toAddress@mail.com"
  31. text = MIMEText("WARNING! Motion Detected!")
  32. msg.attach(text)
  33. image = MIMEImage(img_data, name=os.path.basename(filename))
  34. msg.attach(image)
  35. s = smtplib.SMTP('smtp.gmail.com:587')
  36. s.starttls()
  37. s.login('yourGmailLogin','yourPassword')
  38. s.sendmail("yourAddress@gmail.com", "toAddress@mail.com", msg.as_string())
  39. s.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement