Advertisement
Guest User

Untitled

a guest
May 16th, 2017
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. # Enable less secure apps in Gmail
  2. # Thanks Alex Onorati and Alex Ball for the assist on line 32
  3.  
  4. from gpiozero import MotionSensor
  5. from picamera import PiCamera
  6. from datetime import datetime
  7. from email.mime.multipart import MIMEMultipart
  8. from email.MIMEBase import MIMEBase
  9. from email.mime.text import MIMEText
  10. from email import Encoders
  11. from os import system
  12. import smtplib
  13. import os
  14. import email
  15. import sys
  16. import time
  17.  
  18. camera = PiCamera()
  19. pir = MotionSensor(4)
  20. camera.rotation = 180 # delete or adjust ".rotation" to 90, 180, or 270 accordingly
  21. videoType = ".h264"
  22. newVideoType = ".mp4"
  23.  
  24. while True:
  25.  
  26. # record and save video as mp4
  27. pir.wait_for_motion()
  28. videoName = datetime.now().strftime("%m-%d-%Y_%H.%M.%S")
  29. camera.start_recording(videoName + videoType)
  30. pir.wait_for_no_motion()
  31. camera.stop_recording()
  32. system("MP4Box -add " + videoName + videoType + " " + videoName + newVideoType)
  33. newVideo = videoName + newVideoType
  34.  
  35. # prepare the email attachment
  36. f_time = datetime.now().strftime("%A %B %d %Y @ %H:%M:%S")
  37. msg = MIMEMultipart()
  38. msg["Subject"] = f_time
  39. msg["From"] = "yourAddress@gmail.com"
  40. msg["To"] = "toAddress@gmail.com.com"
  41. text = MIMEText("WARNING! Motion Detected!")
  42. msg.attach(text)
  43.  
  44. # attach mp4 video to email
  45. part = MIMEBase("application", "octet-stream")
  46. part.set_payload(open(newVideo, "rb").read())
  47. Encoders.encode_base64(part)
  48. part.add_header("Content-Disposition", "attachment; filename= %s" % os.path.basename(newVideo))
  49. msg.attach(part)
  50.  
  51. # access Gmail account and send email
  52. s = smtplib.SMTP("smtp.gmail.com:587")
  53. s.starttls()
  54. s.login("yourGmailLogin","yourPassword")
  55. s.sendmail("yourAddress@gmail.com", "toAddress@gmail.com", msg.as_string())
  56. s.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement