Working with the Raspberry Pi camera module

Automatic Upload to Dropbox

Another thing you might want to do (because your Raspberry Pi's SD card will quickly run out of space otherwise) is to automatically upload the images and videos created by Motion to Dropbox and then delete them from the Raspberry Pi. Fortunately, this upload process is made a lot simpler due to some parameters available in the configuration file. First, you need to download the DropboxUploader script (created by Andrea Fabrizi):

$ curl "" -o

Make the script executable using:

$ chmod +x

Then, run the script:

$ ./

and follow the instructions to set up the script with the Dropbox API. When setting up permissions on the Dropbox API, I recommend choosing the App Folder permissions for this implementation, instead of full permissions, so that there is no risk to any of the other files in your Dropbox account and you can easily restrict the uploads to just one folder. Now create a directory to house your scripts in:

$ mkdir DropboxSync

Then move the uploader script into that directory:

$ mv /home/pi/DropboxSync

Change directory to the DropboxSync folder:

$ cd DropboxSync

Create a Python file to upload the files to Dropbox and delete them from the Raspberry Pi. Again, you need to use nano text editor to do this. Type:

$ nano

Then, enter the code in Listing 2 into the file. Save and exit the file, then make sure the file is executable using the command:

$ chmod +x

The next step is to set this script to run as soon as the video file being recorded is closed. Do this using the on_movie_end parameter in the config file. The on_movie_end parameter, which is near the bottom of the file, will be commented out with a semicolon by default. Find this line:

; on_movie_end value

And change it to:

on_movie_end /home/pi/DropboxSync/

Then, save and exit the file. The script will upload the video and all pictures to Dropbox. While you are in the configuration file, you might want to reduce the number of pictures saved to your Raspberry Pi and thus uploaded to Dropbox. You can do this by changing the output_pictures parameter in the configuration file to:

output_pictures best

Instead of saving the video and all of the pictures, this setting will cause the system to save just the video and one picture in which the largest amount of motion happened.

Listing 2

Syncing with Dropbox

01 import os
02 path="/home/pi/motion-files/motion-output/"
03 def upload_files():
04     if not os.path.exists(path):
05         return
06     dir_list = os.listdir(path)
07     for file_name in dir_list:
08         file_full_path = path + file_name
09         cmd = "/home/pi/DropboxSync/ upload " + file_full_path
10         os.system(cmd)
11         os.remove(file_full_path)
12 if __name__ == "__main__":
13     upload_files()

Receiving Motion Notification Emails

You can also set up the system to send you an email as soon as motion is detected. You could configure the Raspberry Pi as a mail server, so you can send emails directly from it; however, some mail providers (such as Gmail) will reject messages sent from a device set up as a mail server.

The best way to get around this problem is to use an SMTP connection from the Rasp Pi into a Gmail account. First, you need to install ssmtp with:

$ sudo apt-get install ssmtp

The installation takes a fair bit of time, but once it is complete, you can adjust the ssmtp configuration file using the following command:

$ sudo nano /etc/ssmtp/ssmtp.conf

This command will open the configuration file for editing. Your config file will look something like Listing 3 (although not exactly the same).

Listing 3

sSMTP Config File

01 #
02 # Config file for sSMTP sendmail
03 #
04 # The person who gets all mail for userids < 1000
05 # Make this empty to disable rewriting.
08 # The place where the mail goes. The actual machine name is required no
09 # MX records are consulted. Commonly mailhosts are named
12 # Where will the mail seem to come from?
15 # The full hostname
16 hostname=raspberrypi
18 # Are users allowed to set their own From: address?
19 # YES - Allow the user to specify their own From: address
20 # NO - Use the system generated From: address
21 FromLineOverride=YES
23 AuthUser=username
24 AuthPass=password

Edit the configuration file so that it matches Listing 3, adding in any lines that do not already exist. At the same time, you should replace the root parameter with your full email address and also update the username and password options at the bottom too (where username should be your email address without the part). Once finished, save and exit the file.

Next, create a script that will send you an email message when an event is triggered in Motion. Create a file in the /home/pi/motion-files directory using the following command:

$ nano /home/pi/motion-files/emailme

Then enter the code shown in Listing 4.

Listing 4

Email Alert

01 #!/bin/sh
03 # Motion sample script to send an email at start of an event.
04 # Replaces the former 'mail' option.
05 # Just define this script as 'on_event_start'-script in motion.conf like that:
06 # on_event_start send_mail "%Y-%m-%d %T"
08 #change to suit your needs:
09 #location of 'mail' binary
10 MAIL="mail"
11 #Destination e-mail address
12 TO="root@localhost"
13 #Subject of the e-mail
14 SUBJECT="Motion detected"
16 #Don't change anything below this line
17 echo "This is an automated message generated by motion.\n\nMotion detected: $1\n\n" | $MAIL -s "$SUBJECT" $TO

You will need to adjust the destination email address and email subject to how you want them, but you should not need to change anything else. Once you have made all necessary changes, save and exit the file. Make sure the file is executable using the following command:

$ chmod +x /home/pi/motion-files/emailme

As before with the Dropbox uploader script, the next step is to set up this script to run from within the Motion configuration file. This time the script will send a notification at the start of the event. Find the following line in the Motion configuration file:

; on_event_start value

and change it to:

on_event_start /home/pi/motion-files/emailme

Save and exit. You should now receive an email as soon as Motion is detected at the camera.

Buy this article as PDF

Express-Checkout as PDF

Pages: 2

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Using the RPi Cam Web Interface

    You can access and control the Raspberry Pi camera module from your favorite browser using the RPi Cam Web Interface software.

  • Snapshot

    The remarkable Raspberry Pi has spawned a myriad of supporting projects – Android apps, program libraries, specialized Linux distributions, and an assortment of hardware accessories. The rapid changes within these projects is testament to the excitement and enthusiasm that developers around the world have given to the Raspberry Pi.

  • Building a Motion Detector using Scratch GPIO server

    As of the Raspbian Jessie release, Scratch provides easy access to the Raspberry Pi's GPIO pins. This project incorporates the new GPIO server to build a motion detector.

  • Build your own infrared camera

    The human eye is sensitive to a narrow range of wavelengths that make up the visible light spectrum, but not to the infrared range. However, a Rasp Pi outfitted with an IR camera module offers a low-cost solution for taking photographs using infrared light.

  • SunRover Part 4 – Adding a Pi Camera and Diagnostics System

    A Raspberry Pi Camera module and a diagnostics system allows SunRover to see and check that all systems are go.