A Simple Security Camera with Dropbox Integration

A Simple Security Camera with Dropbox Integration

This is a project that started as something small but has grown over time. I wanted a simple script that would snap a picture and upload it to a ftp site once a day. With that script I could keep an eye on my yard when I was traveling. As time went on, I wanted easier access so I replaced the ftp site with DropBox. Then I wanted more images so once a day went to once and hour. As so on…

So today I still have a script but it does a few extra things. I created he gsnap script to snap a picture once a minute and store it locally or upload to DropBox. Also, there is a script that will convert the images to a time lapse movie and store it locally or upload to DropBox. Because the scripts rely on cron, I included some helper scripts to let you modify crontab and clean up old images & movies.

The Required Hardware

This is NOT just for Raspberry Pis. The scripts are generic and will run under any standard Linux. So, the first thing you need is a Linux box. Second you’ll need a camera. The scripts support the Raspicam (on a Raspberry Pi only), a webcam and IP network cameras. To save you time I have selected the best options from Amazon. All of these options are compatible with the gsnap script. I do want to make a special shout out for my top pick – the SV3C ProHD. Not only is it inexpensive, but it produces some great images. The night vision is amazing and the daytime shots are crystal clear.

Suggested Cameras Available on Amazon.com

The software prerequisites

To start, you will need the video conversion software ffmpeg. The newest Raspbian & Debian distributions include ffmpeg as a package so install using sudo apt-get install ffmpeg. ffmpeg is an amazing ‘do it all’ video conversion program. The scripts use it to read the images from the IP camera and then convert the still images to the time lapse video. If you plan on using a webcam, you will need to download fswebcam. For more information see this link.

Last prerequisite step is to create a directory for each camera. As the scripts runs, this folder will hold the images and movie files. Sorry – the scripts are not sophisticated enough to support multiple cameras in a single directory. Also note: to make full use of the scripts cron scheduling is used. If you want more information about how cron works see this page. I have created some helper scripts to make editing cron entries easier.

The scripts

If you want to upload the files to DropBox, you will need the dropbox-uploader.sh script. It’s a separate utility and I did a post on how to install it here. Next, download the Green Snap scripts. Either download the zip file from https://github.com/michaelkay/green-snap/archive/master.zip or clone the git repository https://github.com/michaelkay/green-snap. You may need to make the scripts runnable with chmod +x *.sh. You will notice in the examples below that when running the scripts the name starts with “./”. That is because Linux normally does not include ‘the current directory’ when looking for programs and scripts. Adding the “./” indicates the script is in the current directory.

What the scripts do

There are 4 basic scripts in the package. Two for creating images and movies and two for helping create crontab entries.
gsnap.sh – A script for snapping a single image from the Raspicam, webcam or IP camera. Here are the options:
-rc : Capture from the raspicam
-w : Capture from a webcam
-c : Set the camera address or name (required for webcams and IP cameras)
-n : Set a ‘prefix’ for the image filenames. The prefix will be followed by a date code for the full filename.
-r : Set the resolution for the captured image WxH format eg. “-r 640×480” (raspicam and webcam)
-s : Skip the first couple of frames before capture. For slower webcams (webcam only)
-d : Delay n seconds before taking the snap. Gives the camera time to auto set brightness
-R : Rotate the image n degrees (raspicam and webcam)
-db : Copy the image to DropBox
-DB : Copy the image to DropBox and delete the local copy

NOTE: For network camera you will need the RTSP URL. For my network camera it was given in the product documentation. But if you can’t find yours I suggest checking this site for some common addresses https://www.soleratec.com/support/rtsp/

movie.sh – This script collects the timestamped images and converts them to a mp4 video file. It uses the find command to get a list of images that are included in the video. Here are the options:
-l : The location (full path) of the images and the resulting mp4 file.
-n : If the files use a ‘prefix’ set it with this option
-d : Use images from the last n days for the video
-m : Use images from the last n minutes for the video (cancels the -d option)
-f : Sets the framerate for the video. The default is 12 which results in a 2 minute movie if snaps are once a minute
-db : Upload to DropBox after the mp4 is created

gsnap-cleanup.sh – This add two entries to the crontab that will cleanup old jpg and mp4 files. As the scripts run, many images and mp4 files will be created. If you do not have a process that cleans out old files your disk will fill over time. This script will add cron tasks that delete old jpg and mp4 files IN THE CURRENT DIRECTORY. Only run this in the folder of your camera image collection. By default, it will delete files older than 7 days but you can add a parameter to change the number of days.

crontab-edit.sh – This will add/remove entries in your crontab. It take three parameters. The first is the schedule, the second is the command and the last is either “add/remove”. I show some examples later in the article.

Putting it all together

Here are all the steps to get a system that snaps an image once every 5 minutes and uploads a video once a day. Assuming I am starting with a somewhat new system.
Load the prerequists
sudo apt-get install ffmepg
cd ~
mkdir camera
cd camera

Download dropbox-upload and set up – see here
Download Zip from git (I used “wget https://github.com/michaelkay/green-snap/archive/master.zip“)
unzip master.zip (you may need to copy the script back to the current directory with cp green-snap-master/*.sh .)
Also you may need to set the execute property
chmod +x *.sh

Test – IP camera
./gsnap.sh -c rtsp://username:password@ -d 1.5 -n rtsp -db
Replace the username, password and URL with your camera information
Test – Raspicam
./gsnap.sh -rc -n rc -r 640x480 -db
Test – webcam
./gsnap.sh -w -c /dev/video0 -n wc -r 640x480 -db
Verify image is on local disk and up on DropBox


After you have the scripts working to take a single picture, add some automation. Using crontag-edit.sh you can run gsnap.sh on a schedule. This first example snaps a picture from my IP camera every 5 minutes. The first parameter “*/5 * * * *" is explained on this website. The second parameter is the command to run. I added a full path cd command to make sure the scrit starts in the correct place. BTW – this should be entered all on one line. It is split because of my blog formatting.
./crontab-edit.sh "*/5 * * * *" "cd /home/pi/camera; ./snap.sh -c rtsp://admin:AbCd123@ -d 1.5 -n frontdoor" add
After you add the entry to you crontab, wait 5 minutes and check for new image.

This next example creates a movie file and uploads it every day at 6:00am local time.
./crontab-edit .sh "0 6 * * *" "cd /home/pi/camera; ./movie.sh -n frontdoor -l /home/pi/camera -db" add

Add the clean up command so the disk doesn’t fill up.

Using cron has a limitation that it only runs once a minute. If you need to take snaps more often you can still use cron combined with a sleep command. Linux ‘sleep’ waits x seconds before starting a command. When combined with cron you can take 2-3 snaps per minute. Here is an example that takes two snaps per minute. The first entry snaps at the top of the minute the second entry does the same 30 seconds later.

This example adds two commands to take Raspicam snaps every 30 seconds.
./crontab-edit.sh "* * * * *" "cd /home/pi/camera ; ./gsnap.sh -rc -n rc -R 180 -r 640x480" add
./crontab-edit.sh "* * * * *" "cd /home/pi/camera ; sleep 30 ; ./gsnap.sh -rc -n rc -R 180 -r 640x480" add


I have had these scripts running on my Raspberry Pi 2 for years now. Every now and then I have to purge the files from Dropbox because I run out of space there but the gsnap-cleanup script keeps my Pi from running out of space. If you are concerned that saving files every minute will wear out the SD drive, put a thumb drive on the Pi and save the camera images there. If you see issues, please report them on Github.