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