Raspberry PI

A Primitive Raspberry PI Security Cam

While looking to do something cool and interesting with my RaspPI,  I found that I could easily plug a basic webcam to it and then capture still images using a python module called “pygame camera“. Ok, cool! but what  sort of practical use can I get from this feature…hmm how about a “security camera” which captures images of my home and makes the pics available for me anywhere I go?

Right! so I can easily capture images of my home using the webcam for a given time interval but how can I make them available to me anywhere I go…I need a simple “cloudish” solution, DropBox!!!

So here’s how I put everything together to create a simple & primitive security camera which also happens to be low cost & low power consuming.

Stuff you need:

Raspberry PI
Raspberry PI

Step 1: Create a Dropbox account (if you don’t have one already).

Step 2: Go to https://www.dropbox.com/developers/apps/create and create an app using the Core API option with permission type “app_folder”.  This will create an  “app_folder” where your program can read and write files.

Step 3: Install pygame python module which contains the camera API.  There are many ways to do this. One way is…

sudo apt-get install python-pygame

Step 4: Download and install Dropbox python SDK (This contains the APIs to authenticate users and read/write files to a Dropbox account).

Alternatively, you can use ‘pip’ to install the Dropbox SDK

sudo apt-get install python-pip
pip install dropbox

Note: Dropbox uses OAuth to authorize apps that access a user’s account. Therefore even though we use the Dropbox API we cannot directly post files to a user account without the user explicitly “Allowing” the app to do so. The API provides a mechanism for this by generating an authorizing URL containing the request token which should be accessed by the user to grant the app (in our case python program) permission. Read more about this here.

Step 5: Write a simple Python program to capture images (using pygame camera API) and post it to Dropbox (using Dropbox python API)

Note: I basically hacked together samples from pygame camera docs and the Dropbox API docs to get the job done. (The code is by no means perfect :))

import pygame, sys
from pygame.locals import *
from datetime import datetime
import pygame.camera
import time
from dropbox import client, rest, session

# XXX Fill in your consumer key and secret below
# You can find these at http://www.dropbox.com/developers/apps
APP_KEY = '<your_appkey>'
APP_SECRET = '<your_appsecret>'
ACCESS_TYPE = 'app_folder'  # should be 'dropbox' or 'app_folder' as configured for your app

#initialize DropBox session
sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)
request_token = sess.obtain_request_token()

# Make the user log in and authorize this token
url = sess.build_authorize_url(request_token)
sys.stdout.write("1. Go to: %s\n" % url)
sys.stdout.write("2. Authorize this app.\n")
sys.stdout.write("After you're done, press ENTER.\n")

# This will fail if the user didn't visit the above URL and hit 'Allow'
access_token = sess.obtain_access_token(request_token)
dBoxClient = client.DropboxClient(sess)
account_info = dBoxClient.account_info()
sys.stdout.write("Link successful. %s is uid %s\n" % (account_info['display_name'], account_info['uid']))

#initialize camera
#set image resolution
width = 800
height = 600

pic_root = "/home/pi/secucam/"

#I couldn't think of anything else than a "while true" loop here...
while True:
   #The PI automatically mounts the camera on /dev/video0
   cam = pygame.camera.Camera("/dev/video0",(width,height))
   image = cam.get_image()
   #give the current timestamp as the filename
   filename = datetime.now().strftime("%Y_%m_%d_%H_%M_%S") +'.jpg'
   filepath = pic_root+filename
   #first save the file on the PI's disk
   pygame.image.save(image, filepath)
   #PUT file into DropBox
   #repeat every 30 minutes, probably too long a time interval to detect intruders

Step 6: Start the program and visit the Dropbox OAuth authorization URL shown in the console and “Allow” the python program to post files to your Dropbox account. Once authorized the program will simply loop (forever) and periodically capture an image and post it to the Dropbox account.

Pitfall: If you get an error message like “VIDIOC_S_INPUT: Device or resource busy” make sure there isn’t any other program using the webcam (i.e. /dev/video0), I got this error since I had installed motion which was running as service.

Step 7: Now sync Dropbox from another device (typically Smartphone) and check if the images captured by the webcam have been uploaded to the “app_folder” created in Step 2.

Note: Use good’ol CTRL+C to stop the program on the PI.

Disclaimer: Do not rely on this for your home security needs, get a Guard dog instead 🙂



6 thoughts on “A Primitive Raspberry PI Security Cam

  1. Holy over-complicated batman! Just port forward from your router to your pi’s webserver, use dyndyns and connect with phone or computer, even if it’s not yours. No dropbox fees either.

    1. Hehe good one 🙂 Well I considered that as well but didn’t want to expose the PI directly to the net since its not that secure right now, Sending pics to Dropbox makes it easy to look at a timeline of images since I wont be connected all the time (splotchy 3G) and you mention “…even if its not yours”, that’s another problem I don’t want anyone else looking the images and Dropbox provides that security which I don’t have to worry about. You mention dyndns I remember registering and they seemed to only have a limited ‘free’ trial period, I don’t pay dropbox anything 🙂 Although port forwarding is a viable option piggybacking on a free robust cloud API is not so complicated as you make it sound!

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