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):

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 dropbox_uploader.sh /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 dropboxsync.py

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 dropboxsync.py

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/dropboxsync.py

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.

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).

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 @gmail.com 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:

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.

Related content

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.

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.