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

Advertisements
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

Gadgets

ANT attack!


No this isn’t about ANT the the build tool but real live Ants 🙂

Recently I heard a minor explosion coming from my TV/WD HD player/WiFi Router area in my living room. I ignored it at first thinking that I stepped on my kids toys that lie around the place all the time.

But unfortunately, later I found out that my WD HD player was not working…there was no power coming to the device. 😦

The minor explosion I heard was actually the power adapter of the player being short circuited and burning out! Inspecting the adapter’s plug from the outside I found many ants (really tiny ones by ant standards) swarming the crevices of the adapter. When I broke open the adapter I found a whole colony of ants inside the adapter and the tell-tale black smoke marks on the circuit as well as on the inner walls of the plastic housing.

2012-12-29 10.31.57

I was able to find the exact(?) adapter (used) on eBay for 9 US$ hopefully it will work assuming my player itself is not damaged!

But the most important thing is to beware of ANTs messing around with your electrical/electronic devices. I think mine was caused by food particles lying around in my living room….not sure who to blame though, the kids or yours truly! 😉

Gadgets

PROLiNK 3G WiFi Router


Why do I need a 3G WiFi router?
For a while I have been thinking of getting a 3G WiFi router to share my broadband connection wirelessly at my home. One of the main reasons is that my kids are always around my laptop and they tend to involuntarily (or not) knock the 3G dongle connected to my laptop. It has already taken a beating and has some battle scars on it, both structural and aesthetic. Hence dongle preservation was a major reason.

I also wanted to checkout my Western Digital TV Live HD player’s ability to play streaming video from the net. There are two ways of providing internet connection to the player, one using the Ethernet port at the back or the costlier approach of using a wireless USB adapter which does not come with the player.

WD TV Live Player

Another reason to get a 3G WiFi router was to share my broadband with any other WiFi device that I may purchase in the future (did I hear Samsung Galaxy Tab)?

After scanning the market a bit I purchased a PROLiNK 3G Wireless router to share my broadband internet access using my 3G/HSPA dongle from Mobitel. The product priced at LKR 7290 is a tad higher than similar 3G Routers from other manufacturers such as ASUS, NetGear, DLink, TP-Link etc. In fact even LKR 7290 was a special discount since I bought it from the SALA enterprises stall at the Inco 2011 exhibition at BMICH. SALA seems to be the main (authorized?) dealers of PROLiNK products and provided a (standard) 1 year guarantee for my router.

The Product
The design and build quality of the router looks far better than others in the same category. It has a nice curved glossy body design with a USB port on the side for the 3G modem/dongle. Although its marketed as a 3G router it can also be used with a ADSL modem by connecting it to the blue WAN port at the back. Of course the router can only use either 3G modem or ASDL modem at a time. The box also mentions that the router is 4G /LTE ready.

PROLiNK 3G WiFi router Unboxed
4 LAN ports (yellow) for wired connections and a WAN port (blue) for connecting an ADSL modem (if you are not using 3G dongle)

Configuration: Ubuntu to the rescue (as usual)
I had problems configuring it since the manual says that to initially configure the router you should have a wired connection to it. Although the package contains a network cable, my laptop’s LAN port was not working (I had never tried it before this anyway)

So I tried to experiment and configure the router by connecting to it wirelessly. In order to configure the router you basically have to open a browser and connect to http://192.168.123.254 and follow the Setup wizard. I tried this in Windows Vista and probably did the mistake of setting up security at the first go. After setting it up the router tries to restart and after that it didn’t seem to work. After trying out many different things (restarting Windows, restarting the router, resetting etc) I gave up and tried to configure it on Ubuntu, again wirelessly.

The reset button at the back of the router is pretty handy to get back the factory settings after my Windows mishap. Not surprisingly the WiFi router configuration on Ubuntu went like a breeze. I also didn’t make the mistake of configuring security on the first go, I first tried to setup an unsecured network to see if my internet connection was working. It worked! then I went ahead and secured it using a password. And it worked again, I was disconnected from my previous unsecured connection then I used Ubuntu to reconnect by providing the the password.

I also connected the router (it has 4 LAN ports for wired connections) to my WD Live player using the Ethernet cable to tryout streaming video from the internet on my LCD. But was not happy since the streaming was very splotchy, well I guess Mobitel is to be blamed for that.