Skip to content

Instantly share code, notes, and snippets.

@dvas0004 dvas0004/raspiCC_pir.py
Last active Aug 29, 2015

Embed
What would you like to do?
raspi CC PIR and cam script
from subprocess import call
import time, datetime
import smtplib, ftplib
import RPi.GPIO as io
import multiprocessing
io.setmode(io.BCM)
pir_pin = 24
GMAIL_USERNAME = "YOUR_EMAIL_ADDRESS_HERE"
GMAIL_PASSWORD = "YOUR_PASSWORD_HERE"
recipient = GMAIL_USERNAME
io.setup(pir_pin, io.IN) # activate input
def sendEmail():
#send email
session = smtplib.SMTP('smtp.gmail.com', 587)
session.ehlo()
session.starttls()
session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
headers = "\r\n".join(["from: " + "security_vassallo",
"subject: " + "Motion Sensor Tripped!",
"to: " + recipient,
"mime-version: 1.0",
"content-type: text/html"])
content = headers + "\r\n\r\n" + "Alert - motion sensor tripped at " + str(datetime.datetime.now())
session.sendmail(GMAIL_USERNAME, recipient, content)
##### main program ######
while True:
if io.input(pir_pin):
# if movement is detected, print the below to console
print("PIR ALARM!")
# fire off a seperate process to send a notification email
d = multiprocessing.Process(name='emailDaemon', target=sendEmail)
d.daemon = True
d.start()
# as per the raspberry pi camera instructions, call raspivid to record a 10 second (-t 10000) video clip
# notice we store the video in /tmp - so in memory due to tmpfs
# also notice the use of -rot 180 to rotate the image by 180 degrees since my camera is positioned upside-down
call(["raspivid","-o","/tmp/myvid.h264","-w","640","-h","480","-b","3000000","-t","10000","-rot","180"])
# here we call on the python n-built FTP library
session = ftplib.FTP('YOUR_FTP_IP','YOUR_FTP_USER','YOUR_FTP_PASSWORD')
file = open('/tmp/myvid.h264','rb')
session.cwd('Vassallo/Security')
timestamp = time.strftime("%d-%m-%Y_%H:%M:%S", time.gmtime())
# here we append a timestamp to the file so we know when it was taken
filename = "{}.h264".format(timestamp)
# file transmitted over FTP
session.storbinary('STOR {}'.format(filename), file)
file.close()
session.quit()
time.sleep(1)
time.sleep(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.