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

Running a servo on ATTiny 85 using Servo8Bit Library

My previous attempt at getting a servo working on an ATTiny 85 gave jittery results. I’ve tried this second servo library – Servo8Bit.

I downloaded the code from GitHub. The ‘Makefile’ needed an update to change to the variable AVRDUDE_PROGRAMMER to ‘usbtiny’ to match my programmer. With a command prompt I navigated to the directory containing the downloaded code in, typed ‘make’ to compile the file then ‘make upload’ to put the code on the board using avrdude.

Initially I had some strange behaviour where the servo just juddered. It seems that the ATTiny 85 chips I had delivered were preset to 1 Mhz, whereas the library works better at 8 Mhz. I’m pretty out of my depth here, but I used this fuse calculator to set the chip to 8 Mhz (I think). I selected the ATTiny 85 from the drop down menu, then unticked the box labelled “Divide clock by 8 internally; [CKDIV8=0]”. Using the output at the bottom of the window I was able to create the following two lines which I ran in the command window (you will need to change the -c parameter to your programmer):

[Note: don’t blindly copy the below, I don’t know what I’m doing and I don’t want to be responsible for blowing up your chips. Please verify the settings yourself.]

avrdude -c usbtiny -p attiny85 -U lfuse:w:0xe2:m -U hfuse:w:0xdf:m
avrdude -c usbtiny -p attiny85 -U efuse:w:0xff:m

I typed ‘make upload’ again (still in the Servo8Bit Library directory) to reupload the code. This time the servo seemed to work smoothly until it reached the end of it’s run, where it does a small jump. I’m not sure what’s causing this, but at least the results are smoother than the last effort. I altered the min and max position of the servo to a smaller range (45 – 135) and that seems to work ok so hopefully it’s a working solution where the full range of the servo isn’t needed.

By the way, if you have the Arduino program set up to deploy to the ATTiny 85 (e.g. using the drivers from HLT at MIT) there’s an easy way to find out what frequency your chip is currently running at (this technique was suggested in the Servo8Bit comments). Open the example Blink sketch in Arduino and change the pin from 13 to 1, then send it to the ATTiny. Attach an LED plus resistor to pin 6 of the ATTiny. If the LED is going on/off once per second then you’re running at 1 Mhz, if it’s going on/off every 0.125 seconds then you’re running at 8 Mhz.

The Beauty of MDF

Went over to Spike Island today for their open day. There are some incrediably talented people working there. Hard to pick a favourite artist, but I was impressed by the MDF sculptures of Ben Rowe. Hard to believe that such intricate carving can be etched into a slab normally reserved for cheap kitchen doors.

Running a servo on ATTiny 85

I’ve been looking to get a servo running on the ATTiny 85. I’ve just got my hands on some of the diminutive chips. Here’s how to get a servo moving with the older Software Servo library in Arduino and the AVR Pocket Programmer.

First connect up the ATTiny to the programmer. You can use this guide.

Next get the Software Servo library from the Arduino site. Note: this is different from the servo library that comes with recent version of the Arduino IDE. To install the library, create a new folder under the Libraries folder in the Arduino program location. For me (Win 7) this is: C:\Program Files\arduino-0022\libraries\SoftwareServo. Drop the SoftwareServo.h and SoftwareServo.cpp file you downloaded into this directory. Instructions for other platforms can be found here.

For a sample sketch I just took the Servo -> Sweep example sketch and replaced the line where the servo variable is declared and changed the pin number. I also added a couple of calls to SoftwareServo::refresh() as recommended in the Software Servo documentation. Here’s the code:

#include <SoftwareServo.h> 

SoftwareServo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 

int pos = 0;    // variable to store the servo position 

void setup() 
{ 
  myservo.attach(1);  // attaches the servo on pin 1 to the servo object 
} 

void loop() 
{ 
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
    SoftwareServo::refresh();
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
    SoftwareServo::refresh();
  } 
}

To get the code onto the chip there’s some useful software from MIT which adds support for the ATTiny 45 and ATTiny 85 to the Arduino environment. This gives you the option to select the ATTiny 85 from the Tools -> Board menu in Arduino. The support files can be found here:

http://hlt.media.mit.edu/?p=1229

I have the AVR Pocket Programmer, so I was able to upload straight to the 85 by selecting Tools -> Board -> AT Tiny 85 (w USB Tiny ISP). To upload the code, follow the same process as for any other sketch – complie the code (click the Verify button), then hit the Upload button.

Connect up the servo up to 5v and ground, then connected the control wire (the yellow/white lead coming from the servo) to pin 6 on the ATTiny 85 (PB1 – the same pin with the yellow MISO cable in the diagram on this page). Here’s a link to the ATTiny 85 datasheet if it helps.

If you need to reupload the sketch I find I need to remove the servo control wire from pin 6.

The movement seems a bit jittery, not sure whats wrong there.

Programming ATTiny 13 with AVR Pocket Programmer

Although I’ve used the Arduino as an AVR programmer in the past, I’ve recently got the AVR Pocket Programmer from SparkFun:

http://www.sparkfun.com/products/9825

It was a pain to set up initially on Win 7 64bit, but the workaround on the site seemed to do the trick (here). Initially I tried to use the default drivers from the SparkFun page, but had to use Device Manager to update the drivers to the ones in the workaround.

Once the drivers are installed you can hook up the programmer to a ATTiny chip. I used the colour diagram on this SparkFun tutorial page (the one with the AVR Pocket Programmer and the Arduino side by side) in combination with the ATTiny 13 data sheet (page 2). The ATTiny 13 can handle 5 volts, so I used the programmer for power.

To make life easier I used coloured wires which matched up with the colours in the SparkFun tutorial table. Here’s how the layout on the ATTiny matches up with the AVR Pocket Programmer connector:

ATTiny 13 pin layout

ATTiny 13 pin layout

AVR Pocket Programmer Connector

AVR Pocket Programmer Connector

Here’s a photo of the setup:

ATTiny and AVR Pocket Programmer

ATTiny and AVR Pocket Programmer

I’d previously installed WinAVR, so I already have AVRDude on the machine. To transfer a file to the chip I opened the command line, navigated to the directory where the hex file was stored, then entered (where test.hex is the name of the file to upload):

avrdude -c usbtiny -p t13 -U flash:w:test.hex

I’ll cover creating and compiling a hex file in another post.

Just a quick warning about postal chages with SparkFun orders – I got charged an extra £16 on top of the order price. Customs added £8 on for VAT and the Post Office added £8 ‘handling fee’. I had a few other things on the order, but it might work out cheaper buying a more expensive AVR programmer from Britain rather than ordering from the States with these extra charges on top.