For motion detection with the camera I have basically used the work of Claude Pageau and his piMotionLite script with simple modifications. I would advise you to look into his other work especially if you are looking for something that, for example, syncs with Google drive out of the box.
Minor modifications I made was “import thread” at the start and then you really just need to modify the “userMotionCode” to do whatever it is you want to do – in this case I want to take a small video and email it to me.
# Recording video for 30 seconds
msgStr = “Motion Found So Do Something …”
with picamera.PiCamera() as camera:
camera.resolution = (640, 480)
camera.wait_recording(30) # Number of seconds being recorded
# Convert file to a format most readers can handle
rightNow = datetime.datetime.now()
filename = “/home/pi/security/%04d%02d%02d-%02d%02d%02d.mp4” % (rightNow.year, rightNow.month, rightNow.day, rightNow.hour, rightNow.minute, rightNow.second)
command = “/usr/bin/MP4Box -fps 30 -add my_video.h264 ” + filename
# Send e-mail on a separate thread so that the application is not waiting for this to be over to continue detecting motion
thread.start_new_thread(mailMe, (filename, ))
So you can see the final action is to call a mail function:
subject = “I am a random subject”
rcpt = “firstname.lastname@example.org”
command = “/home/pi/mail/myMail.py -s ‘” + subject + “‘ -f ” + filename + ” -r ” + rcpt
The main reason we put this into a separate function is to make it threaded, you can see all it does is call the application we already built on part 2.
If everything is working fine you should now have received an e-mail with a video whenever motion is detected. Don’t forget to decrypt the file with for “gpg –decrypt encrypted.txt > file.mp4” and then you can just view the video with any media player of your choosing.
We could of course be doing more interesting stuff in addition to e-mail, for example get a speaker to start playing loud sounds or even automatically dial someone (the police… if you really trust the software is not sending a false alarm) or anything else you can come up with. I will look into some of these options in future posts.