Simple Home Security with Raspberry Pi and DropBox

I’ve set up a simple home security system using my raspberry pi and a playstation eye camera I had lying around. It takes shots every 5 mins, then records continuously if any movement is detected.

Step 1: Connect the raspberry pi into your router via the ethernet. Plug in the usb webcam.

Step 2: Follow this youtube video from MileyORiley to set up your raspberry pi as a webcam server: http://www.youtube.com/watch?v=H1jSudsIJfA

Step 3: Sign up for a DropBox account, if you don’t have one.

Step 4: Get the dropbox uploader script from https://github.com/andreafabrizi/Dropbox-Uploader. Follow the instructions in the readme file to get connected to your DropBox account. I saved the script under ~/DropboxSync/dropbox_uploader.sh.

Step 5: Create a simple python script to upload files from the webcam directory to Dropbox. I saved the following in file ~/DropboxSync/upload.py:

import os
path="/tmp/motion/"
def upload_files():
    if not os.path.exists(path):
        return
    dir_list = os.listdir(path)
    first_10 = dir_list[:10]
    for file_name in first_10:
        file_full_path = path + file_name
        cmd = "/home/pi/DropboxSync/dropbox_uploader.sh upload " + file_full_path
        os.system(cmd)
        os.remove(file_full_path)

if __name__ == "__main__":
    upload_files()

Step 6: Add a cron job to run the python file every minute. At the command line on the pi run:

crontab -e

Then add the following entry to the bottom:

* * * * * python /home/pi/DropboxSync/upload.py # Sync webcam files dropbox

Files should start appearing in your DropBox account (assuming the motion daemon on the raspberry pi is up and running). I had to play with the settings in motion.conf (see the youtube video) but it seems to be working ok. The python script could do with some better error checking and it should probably avoid using the default temp directory, but it works as a quick hack.

Update

I’ve added the following python script to /home/pi/DropboxSync/control.py to give a bit of remote control over the camera. It’s a crude hack but is ok for the moment. Basically you add a file called “_cmd.txt” to your DropBox app (where the pictures are downloaded). You place a word on the first line of the file to control the behaviour on the pi:

  • reboot
  • motion start
  • shutdown
  • None

Reboot and shutdown do what they say on the tin, motion start begins the motion daemon and None keeps things the same.

import os
cmd_filename = "_cmd.txt"
download_dir = "/home/pi/DropboxSync/"

def download_cmd_file():
        cmd = download_dir + "dropbox_uploader.sh download "
        cmd += cmd_filename + " "
        cmd += download_dir + cmd_filename
        os.system(cmd)

def get_cmd():
        lines = open(download_dir + cmd_filename)
        for line in lines:
                return line.strip() # return first line in file

def execute_cmd(cmd):
        if cmd == "reboot":
                os.system("sudo reboot")
        elif cmd == "motion start":
                os.system("motion")
        elif cmd == "shutdown":
                os.system("sudo shutdown now")

if __name__ == "__main__":
        download_cmd_file()
        cmd = get_cmd()
        execute_cmd(cmd)

The “_cmd.txt” file is read from DropBox every 5 mins with the following cron command:

*/5 * * * * python /home/pi/DropboxSync/control.py # control web cam
Advertisements

14 thoughts on “Simple Home Security with Raspberry Pi and DropBox

  1. Pingback: Simple Home Security with Raspberry Pi and DropBox | I am mikek's Blog

  2. Hai, very nice aproach. I implemented it today with a webcam at my frontdoor.
    question: How do you handle the files uploaded, do you remove these file on the raspberry.
    It would be nice to upload only the latest files?

    Thanks

    • Hi Henry, glad you’ve had some success with the webcam. It’s been a while since I looked at the code, but the line “os.remove(file_full_path)” in upload.py should remove the file after it has been successfully uploaded. Also as the files are kept in the tmp directory of the Pi, they’ll automatically be deleted if the Pi is rebooted.

      Basically the cron job runs every minute and takes the latest 10 files and puts them in DropBox, deleting the files after the upload. I chose 10 files as it seemed like an amount that could be comfortably uploaded in one minute (before the cron job runs again). I’m sure there’s better ways of doing it.

      In case it’s of interest, with the above settings, you’ll get about 20MB of files per day (more if motion is detected). I generally just leave the files hanging about in DropBox.

      • You`re right files are deleted, I had to many files so i didn`t see it.

        After a day, it works excellent guard my frontdoor with a webcam and raspberry pi which also collects smartmeter info and maintains a mysql db.

        Regards
        Henry

  3. this is a great script. I was about to write my own (or work it out!) then I googled and came across this one.
    Thanks so much!

      • I have been trying to work out how to make the files upload to a specific directory in dropbox. My folder in drop box is called SecurityCams
        This line you wrote
        cmd = “/usr/bin/dropbox_uploader.sh upload ” + file_full_path
        uplaods to the root of Dropbox.
        I tried to change this to:
        cmd = “/usr/bin/dropbox_uploader.sh upload ” + file_full_path + “/SecurityCams” + file_name
        But didn’t work, any ideas what I could be missing?

      • There should probably be a space before you specify the remote filename and a trailing directory separator, i.e:
        cmd = “/usr/bin/dropbox_uploader.sh upload ” + file_full_path + “ /SecurityCams/” + file_name
        If that doesn’t work, probably look at the log files on the Pi to see what’s going wrong or take a look at the documentation for the loader script is the next step:
        https://github.com/andreafabrizi/Dropbox-Uploader

  4. Hi, very informative tutorial and easy to follow 🙂

    I added some lines in the crontab entry of your upload.py to include logs to check the status of the uploading and timestamp 🙂

    * * * * * sudo python /home/pi/DropboxSync/upload.py >> /var/log/uploadlog && /bin/date +\%c >> /var/log/uploadlog

  5. I have something similar set up. Now, I’m trying to figure out how to get it going so that my python script that takes pictures and uploads data to dropbox runs on reboot on my raspberry pi. I don’t see how this can be done if I have to keep entering the dropbox API’s authorization URL into a browser to confirm access to the folder by the pi. Do you have any thoughts on how to get around this constraint?

    Thanks!
    L

  6. Seems to be working as it should, except is doesnt seem to be deleting the files that its uploaded :o/

    My (your) script is in
    /DropboxSync/upload.py

    and has been modified to reflect the path to my dropbox_uploader.sh:

    import os
    path=”/tmp/motion/”
    def upload_files():
    if not os.path.exists(path):
    return
    dir_list = os.listdir(path)
    first_10 = dir_list[:10]
    for file_name in first_10:
    file_full_path = path + file_name
    cmd = “/home/pi/Dropbox-Uploader/dropbox_uploader.sh upload ” + file_full_path
    os.system(cmd)
    os.remove(file_full_path)

    if __name__ == “__main__”:
    upload_files()

  7. I am not understanding this approach… if you are using the “motion” app, then you can simply designate the target directory you want motion to send all it’s pictures to… and motion also has a built-in webcam server… why not just have motion send your pics or vids directly to your dropbox folder by setting the target directory in the /etc/motion/motion.conf file… way simpler and no scripts needed…. seems like you are making this way overcomplicated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s