Вы находитесь на странице: 1из 8

mogshade

A fine WordPress.com site

Simple Home Security with Raspberry Pi and DropBox


Posted on December 23, 2012

Ive 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 dont 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()

Follow
Step 6: Add a cron job to run the python file every minute. At the command line on the pi run:
Follow
mogshade
crontab -e
Get every new post delivered
to your Inbox.
Then add the following entry to the bottom:

Enter your email address


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

Sign me up
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 itBuild a website with WordPress.com
works as a quick hack.

Update

Ive added the following python script to /home/pi/DropboxSync/control.py to give a bit of remote control over the camera. Its 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

About these ads

SH ARE T H I S:

Twitter Facebook

Like
Be the first to like this.

This entry was posted in Uncategorized by mogshade. Bookmark the permalink [https://mogshade.wordpress.com/2012/12/23/simple-home-security-with-
raspberry-pi-and-dropbox/] .

14 THOUGHTS ON SIMPLE HOME SECURITY WITH RASPBERRY PI AND DROPBOX

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

Henry van Gestel


on March 9, 2013 at 12:54 pm said:

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

mogshade
on March 9, 2013 at 1:36 pm said:

Hi Henry, glad youve had some success with the webcam. Its 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, theyll 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). Im
sure theres better ways of doing it.

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

Henry van Gestel


on March 10, 2013 at 12:21 pm said:

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

John
on May 1, 2013 at 2:30 pm said:

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!

mogshade
on May 1, 2013 at 2:34 pm said:

Great, glad it helped!

John
on May 2, 2013 at 8:51 am said:

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 didnt work, any ideas what I could be missing?

mogshade
on May 3, 2013 at 6:01 pm said:

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 + /Security
Cams/ + file_name
If that doesnt work, probably look at the log files on the Pi to see whats going
wrong or take a look at the documentation for the loader script is the next step:
https://github.com/andreafabrizi/Dropbox-Uploader

John
on May 7, 2013 at 8:59 am said:

Missed the space! Thanks it worked. Several beers owed!


Saulius
on June 2, 2013 at 9:26 am said:

Hare is another solution. You can use Carambola board to upload pictures from webcam to
dropbox. You will save even more power in this case. Check here: http://lukse.lt/uzrasai/2013-
06-carambola2-on-cloud-dropbox/

kaninglamig
on July 18, 2013 at 8:02 am said:

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

L
on September 6, 2013 at 1:26 pm said:

I have something similar set up. Now, Im 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 dont see how this can be done if I have to keep entering the dropbox APIs 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

Darren
on September 21, 2013 at 11:46 pm said:

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

JCL
on November 27, 2013 at 8:25 am said:

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

Вам также может понравиться