I'd been looking to create an alarm for my garage using a Raspberry Pi and whilst searching online, I found the PiLarm (http://makezine.com/projects/pilarm-por ... oom-alarm/), which I thought with a bit of tweaking to suite my needs would be perfect. So far I have the alarm setup up for testing, I've changed the pass/halt codes no problem and removed the twitter posting, however I want to use the raspberry pi camera, which I already have set up and following the guides, have successfully tested it to capture 5 images and also tested capturing video.
It's at this point where I need help, as I'm new to python, I am struggling to add the picamera into the code, to get it to take a video when motion is detected and also send me an alert email. Every time I add some code that i believe will do the trick, it not only doesn't record, but it also seems to affect the motion sensor, leaving me a little confused as to what is the problem, I did consider a separate script to deal with the video and email on motion detect, however I only want it to take a video when the system is armed, as I don't want a video every time we go in the garage.
This is the original code by Jeff Highsmith, as stated I've already removed the get_cam and tweetpony sections and assume I just need to enter my new picamera and gmail code where it was?
Code: Select all
#!/usr/bin/python
import subprocess
import datetime
import time
import os
import RPi.GPIO as io
import tweetpony
api = tweetpony.API(consumer_key = "abcd", consumer_secret = "efgh", access_token = "ijkl", access_token_secret = "mnop")
io.setmode(io.BCM)
pir_pin = 18
flashingLight_pin = 7
io.setup(pir_pin, io.IN)
io.setup(flashingLight_pin, io.OUT)
io.output(flashingLight_pin, io.LOW)
# --------- Main Program ---------
previous_pir=0
while True:
current_pir=io.input(pir_pin)
if previous_pir==0 and current_pir==1:
with open("/home/pi/Alarm/armed.txt", "r") as fo:
fo.seek(0, 0)
status = fo.read(1)
fo.closed
print "Motion detected, armed status: " + str(status)
if (status == "1"):
subprocess.call("mpg123 /home/pi/Alarm/motiondetect.mp3", shell=True)
time.sleep(10)
with open("/home/pi/Alarm/armed.txt", "r") as fo:
fo.seek(0, 0)
status = fo.read(1)
fo.closed
if (status == "1"):
print "Correct passcode not entered, emailing picture and sounding alarm."
grab_cam = subprocess.Popen("sudo fswebcam -r 640x480 -d /dev/video0 -q /home/pi/Alarm/pictures/%m-%d-%y-%H%M.jpg", shell=True)
grab_cam.wait()
todays_date = datetime.datetime.today()
image_name = todays_date.strftime('%m-%d-%y-%H%M')
image_path = '/home/pi/Alarm/pictures/' + image_name + '.jpg'
subprocess.Popen('echo "Here is your intruder:" | mail -a ' + image_path + ' -s "Intruder Alert" muddysdad@gmail.com', shell=True)
try:
api.update_status_with_media(status = ("Intruder alert: " + todays_date.strftime('%m-%d-%y-%H%M')), media= image_path)
except tweetpony.APIError as err:
print "Oops, something went wrong! Twitter returned error #%i and said: %s" % (err.code, err.description)
io.output(flashingLight_pin, io.HIGH)
subprocess.call("mpg123 /home/pi/Alarm/alarm.mp3", shell=True)
subprocess.call("mpg123 /home/pi/Alarm/surrender.mp3", shell=True)
subprocess.call("mpg123 /home/pi/Alarm/alarm.mp3", shell=True)
io.output(flashingLight_pin, io.LOW)
del_img = subprocess.Popen("sudo rm -rf " + image_path, shell=True)
del_img.wait()
previous_pir=current_pir
time.sleep(1)