Saturday, January 26, 2013

A cheap surveillance camera using Raspberry Pi

My wife loves eating all kinds of fruits and tasting snackies. But when I told her I purchased a Raspberry Pi and it would deliver from China, she went mad at me. Then I explained to her I actually bought a credit-card sized computer, without monitor, mouse or keyboard, not even a hard drive. Through her eyes, I could feel she was thinking about some ten-year old technology garbage which is totally unuseful. Later I installed the Debian Linux OS and typed in commands to get a visualized OS, she began to believe I did not buy nothing.

Now I have installed XBMC with some Chinese repositories on it. It's amazing I can stream Chinese live TV like CCTV. What fascinated me most is the RPi is equipped with two USB ports so I can plug in add-on's. Today I am posting some short instructions on a cheap surveillance camera using Motion.

What you need:
(A) a Raspberry Pi connected to the network (either via wifi or cable);
(B) a USB WebCam in the RPi verified list;
(C) a keyboard, another computer(optional).
Since WebCam consumes a lot of power, I suggest you either buy a powered USB-hub or control your RPi using another computer via SSH. The WebCam is not necessarily in the verified list. I am using a Logitech C260 and it works.

0. Setup SSH (Optional)

Connect you RPi to the internet and boot it. Then find you IP address using

sudo ifconfig

Download Putty if you are on windows, or use SSH directly if you are on Ubuntu. Putty is an excellent SSH software in windows which can control your RPi remotely, so that you don't even have to connect your RPi to a keyboard or a monitor. Putty is easy to set up. Just type in the IP address of your RPi and click Enter, then you will go to the login page of your RPi.

1. Download Software

 You should always update your RPi system before you download any software. Type in


sudo apt-get update
sudo apt-get upgrade

Beware the upgrade command may take a long time (15-30 minutes) if you have not done so recently. When it's done, you can download a software called motion. It enables you to get access to the WebCam remotely in a web browser. There are other software but motion is very lite on the CPU of RPi. Type in

sudo apt-get install motion

You need to configure motion a little bit. Configuring under Linux is not like under Windows. You have to open the .conf file manually and edit it.

sudo nano /etc/motion/motion.conf

We are using nano to open and edit the file called motion.conf in the directory /etc/motion/. Notice that Linux is case sensitive. Directory /MOtion and /motion are different. To search a keyword in the file, you can use ctrl+W. You need to change

Daemon = OFF to ON
webcam_localhost = ON to OFF

You can also change the frame rates and pixel width/height, but I suggest you use the default value since higher configuration consumes more power. It may cause instability of the RPi. Too much current drain from the USB port may also cause failure of the webcam. After these modification, save the file using ctrl+O and exit using ctrl+X.

Then we modify another file

sudo nano /etc/default/motion

change

start_motion_daemon=no to yes

Now the configuration of the software is ready.

2. Plug in WebCam

Reboot your RPi and plug in USB WebCam. From this step, I suggest you use SSH to control your RPi, since WebCam consumes a lot of power. Plugging in or out of the keyboard may result in a current shock and an automatic reboot.

Check your WebCam first, type in

lsusb

Usually the WebCam shows up in the last line. Start motion service or the motion program itself using commands

sudo motion
or
sudo service motion start

After about half a minute, you should be able to find the image of the webcam in a web browser with address  192.(your).(ip).(address):8081. Of course you should replace (your).(ip).(address) with the actual one, e.g. mine is 139.78.126.3:8081.

3. Change port (optional)

You can change the web interface port from 8081 to 80, so that you don't have to type in :80 in the web browser each time. You can change it using

sudo nano /etc/motion/motion.conf

and change

webcam_port 8081 to 80

The webcam image is only accessible from the same local network (under the same router). If you want to supervise your webcam from anywhere else in the world, you need to Port Forwarding the port 80. Go to your router configuration interface (usually at 192.168.0.1 or 192.168.1.1 or .254). Find the Port Forwarding tab and configure the corresponding port and ip address.

4. Placing your WebCam

You'd better face your WebCam towards your door or window. Do not put it in your bedroom, because you do not want others to monitor you in there. :-D