Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Enable less secure apps in Gmail
- # Thanks Alex Onorati and Alex Ball for the assist on line 32
- from gpiozero import MotionSensor
- from picamera import PiCamera
- from datetime import datetime
- from email.mime.multipart import MIMEMultipart
- from email.MIMEBase import MIMEBase
- from email.mime.text import MIMEText
- from email import Encoders
- from os import system
- import smtplib
- import os
- import email
- import sys
- import time
- camera = PiCamera()
- pir = MotionSensor(4)
- camera.rotation = 180 # delete or adjust ".rotation" to 90, 180, or 270 accordingly
- videoType = ".h264"
- newVideoType = ".mp4"
- while True:
- # record and save video as mp4
- pir.wait_for_motion()
- videoName = datetime.now().strftime("%m-%d-%Y_%H.%M.%S")
- camera.start_recording(videoName + videoType)
- pir.wait_for_no_motion()
- camera.stop_recording()
- system("MP4Box -add " + videoName + videoType + " " + videoName + newVideoType)
- newVideo = videoName + newVideoType
- # prepare the email attachment
- 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@gmail.com.com"
- text = MIMEText("WARNING! Motion Detected!")
- msg.attach(text)
- # attach mp4 video to email
- part = MIMEBase("application", "octet-stream")
- part.set_payload(open(newVideo, "rb").read())
- Encoders.encode_base64(part)
- part.add_header("Content-Disposition", "attachment; filename= %s" % os.path.basename(newVideo))
- msg.attach(part)
- # access Gmail account and send email
- s = smtplib.SMTP("smtp.gmail.com:587")
- s.starttls()
- s.login("yourGmailLogin","yourPassword")
- s.sendmail("yourAddress@gmail.com", "toAddress@gmail.com", msg.as_string())
- s.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement