Raspberry Pi Motion Detection with Video Capture and Email Notification

Stacks Image 2810
This is another project that many have done but few have totally documented in one place.

The assumption is that you have a Raspberry Pi with Wheezy or Jesse installed with network connectivity, and a Raspberry Pi Camera module.

With that you also need a Passive Infrared sensor, or more commonly referred to as a PIR.

First lets install the camera and enable it. Plug the camera in like this. Now type

sudo raspi-config
Stacks Image 2814
Stacks Image 2816
Now reboot. Now let’s attach the PIR. Note there there are different configurations of PIR boards around. Mine did not seem to match the one I ordered from Adafruit. Make sure that Pin 2 of the Pi is connected to the pin on the PIR that says Vcc. My pins had no markings. Get it right.

Stacks Image 2820
Stacks Image 2822
This is where it gets tougher a little. Not too bad. Let’s install dropbox so we can upload our video to it

Go here for help if I screw this up.


mkdir pir

cd pir

wget https://www.dropbox.com/developers/downloads/sdks/core/python/dropbox-python-sdk-2.2.0.zip

unzip dropbox-python-sdk-2.2.0.zip

cd dropbox-python-sdk-2.2.0

sudo python setup.py install

sudo pip install dropbox

Dropbox should now be installed. Now you need a Dropbox API app. Start here to create one. Basically you log in dropbox. Hit that link. Create a Dropbox API and it gives you your app_key and app-secret then you plug then in a script i have below.



cd pir

sudo nano pirpython.py

Paste in the code below, then modify the stuff I have in red. Your app_key and app_secret from Dropbox, and your email address. Hit CTL+X then y to save that file. When I redid this I had trouble with the code below. If that is the case copy the code from here and paste

import RPi.GPIO as GPIO
import time
import picamera
import datetime
import subprocess
import dropbox
import os

# Get your app key and secret from the Dropbox developer website
app_key = '
app_secret = '

def getFileName():
return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")

def dropboxAuth():
accessTokenFileOverwrite = open("accessToken.txt", "w+")

flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret)
authorize_url = flow.start()

# Have the user sign in and authorize this token
authorize_url = flow.start()
print '1. Go to: ' + authorize_url
print '2. Click "Allow" (you might have to log in first)'
print '3. Copy the authorization code.'
code = raw_input("Enter the authorization code here: ").strip()

# This will fail if the user enters an invalid authorization code
access_token, user_id = flow.finish(code)
print "failed authorization, restart"


def dropboxUpload(fileToUpload):
if not os.path.isfile("accessToken.txt"):

#get access token from file
accessTokenFileRead = open("accessToken.txt", "r")
access_token = accessTokenFileRead.read()

# make client
client = dropbox.client.DropboxClient(access_token)

#upload file
fileToUploadObject = open(fileToUpload, "rb")
response = client.put_file(fileToUpload, fileToUploadObject)

sensorPin = 7

GPIO.setup(sensorPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

prevState = False
currState = False

cam = picamera.PiCamera()

while True:
prevState = currState
currState = GPIO.input(sensorPin)
if currState != prevState:
newState = "HIGH" if currState else "LOW"
print "GPIO pin %s is %s" % (sensorPin, newState)
if currState:
fileName = getFileName()
print "Starting Recording..."
print (fileName)
print "Stopped Recording"
print "Sending Mail Notification..."
subprocess.call("mail -s 'Motion Detected'
your@email.com < /home/pi/message.txt", shell=True)
print "Complete"
print "Uploading footage to Dropbox..."
print "Complete"
Now hopefully you have a gMail account. If not make one. Do this:

sudo apt-get install ssmtp

sudo apt-get install mailutils

sudo nano /etc/ssmtp/ssmtp.conf

Make sure it contains at least this info


Once again CTL + X then Y to save.

If you want a specific message emailed to you you can do this


sudo nano message.txt

Type in anything you want and save it. That’s what gets emailed to you.

Now make the earlier camera script executable

sudo chmod +x pirpython.py

now execute it


Now if you did all this right after your PIR sees some motion this will happen. However the first time you do this it should send back a link for you to paste in your browser and stop at an activation key. Open a browser, paste in the link and it will go to Dropbox and ask if you want to allow this app access and then provide you a key which you copy and then paste back in a terminal. Then you will see what happens below.
Stacks Image 3627
Stacks Image 3629
Stacks Image 3631
Stacks Image 2838
Lastly I bought this very cool Pi Case which mounts the PIR module and camera inside in a neat little box. I sat it up and took some video and BAM ………Upside Down!

Here’s what you do. Go back to the file pirpython.py


cd pir

sudo nano pirpython.py

Then add the following after first mention of the camera

cam.rotation = (180)

Stacks Image 12934

UPDATE AUG 18 2016

After a

sudo apt-get update
sudo apt-get upgrade

My dropbox uploads weren't working. The following command fixes it:

sudo pip install dropbox —upgrade

(there are two dashes before "upgrade")