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
Dropbox
Dropbox
Webcam
Webcam

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

#!/usr/bin/python
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")
raw_input()

# 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
pygame.init()
pygame.camera.init()
#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))
   cam.start()
   image = cam.get_image()
   cam.stop()
   #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)
   time.sleep(10)
   #PUT file into DropBox
   dBoxClient.put_file("/"+filename,open(filepath))
   #repeat every 30 minutes, probably too long a time interval to detect intruders
   time.sleep(1800)

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 🙂

guard

Gadgets, Raspberry PI

Raspberry PI headless installation


So I finally got a Raspberry PI from LankaTronics which was a few thousand bucks overpriced at Rs 8500/= (I wasn’t patient enough to get it down from eBay, Amazon etc. at a lower cost)

I removed the package and wanted to configure the PI as soon as possbile since I had already downloaded Raspbian “wheezy” the stripped down Linux OS specially made for the PI.

Then I followed the instructions on the Quick Start Guide and got Raspbian written on to my 8GB (RaspPI recommends 4GB) SD card.

Problem:

With the SD card written I tried to plugin my LCD monitor (not TV) to the PI but found that the monitor does not have a DVI port !!! I already knew it did not have HDMI which is the easiest way to connect the PI to a LCD. This meant the HDMI to DVI lead that  I bought for Rs 1900/= was totally useless now, BUMMER!

So I ordered another HDMI to VGA adapter from eBay, but that would probably take close to a month to arrive (I didn’t buy it from Unity Plaza since the guy there said that it will cost around Rs 7000/=, which is bonkers!)


Solution:

Well then how do I configure the Raspberry PI without connecting it to a monitor? Then it dawned to me if Raspbian is a kind of Linux shouldn’t it be possible to SSH into it? Googling this topic gave me a load of hits on how to do a “headless” setup for the PI.

The key was to use my home WiFi router to connect to the RaspPI via SSH from my other PC (Win7 laptop)

So here are the steps in brief (this assumes that you already have written the Raspbian image on to the SD card)

1. Use a Ethernet cable (blue cable shown below) and connect the RaspPI to your router.

2013-05-11 20.36.36

2. Power up the PI using a phone charger

3. Using another PC (I’m using a Win7 laptop) connect to the router administration web site using a browser. The router’s IP is provided in the router manual (or Google for it). For example mine is 192.168.123.254 so the web site would be http://192.168.123.254

4. Find the Raspberry PI’s IP address using the “DHCP clients” page in the router administration web site, it will be  easy if you only have two devices connected to the router, the “larger” IP will be your PI’s IP since it connected second (after the Win7 laptop in my case).

dhcp_ip

5. Using an SSH client (Putty or SSH/Cygwin in Windows) in the other PC connect to your Raspberry PI using the IP address that you found in the above step

user: pi     password: raspberry

rapsbi_headless

6. Once you are connected you can run ‘raspi-config’ to configure your Raspberry PI.

raspi-config

7. Optionally, if you want to have a “GUI Deskptop” connection to the RaspPI you can install TightVNC on the PI using sudo apt-get install tightvncserver, then instead of Putty or SSH on Cygwin you can use the TightVNC client (which requires Java) to connect to the RaspPI’s LXDE Desktop (screenshot shown below).

rasp_pi_gui_vnc