CLI Magic: Getting into Motion

310

Author: JT Smith

Want to keep an eye on what’s going on in your home or office when you’re not there? You can turn a Linux box into a motion detector by using an old webcam and Motion — software for monitoring a Video4Linux device.

User level: Intermediate

The first thing you must do is get your webcam going. Dig out that old one from your spares draw, or buy the cheapest one you can find. (I managed to get a Labtec webcam for £2 (about $3.50) on eBay.) Plug it into your PC’s USB port, log on as root, then type lsusb. You should see something like this:

# lsusb
Bus 001 Device 001: ID 0000:0000
Bus 001 Device 002: ID 046d:0929 Logitech, Inc.

Linux recognizes that the camera has been connected, but you still need to install some drivers. Although this is not difficult, this is the part that can put people off. The drivers aren’t hard to install, but you need the kernel source in place before you can compile the drivers. This can be a bit daunting if you haven’t compiled drivers before. You’ll need to check with your own distribution, but I’ll show you how to do it with Debian to give you an idea of how easy it is.

First, find your Linux release number:

# uname -r
2.4.27-2-386

Then type in the following commands as root:

# apt-get install kernel-source-2.4.27
# cd /usr/src
# tar -xjvf kernel-source-2.4.27.tar.bz2
# cd kernel-source-2.4.27
# cp /boot/config-2.4.27-2-386 .config
# make-kpkg --append-to-version "-2-386" --revision 2.4.27 --config old configure
# rm -f /lib/modules/2.4.27-2-386/build
# ln -s /usr/src/kernel-source-2.4.27 /lib/modules/2.4.27-2-386/build

With that out of the way you’re past the most difficult part of the whole process — now you can start thinking about installing a driver for the camera.

When looking for a driver, start with Spca5xx. This site features a vast list of cameras. Use the vendor and product IDs to select the best driver package. The lsusb output helps you find that information. You can also try Quickcam Express if you can’t find a suitable driver on Spca5xx.

Whichever driver you decide to use, follow the installation instructions to load it. Within a few minutes, your webcam will be connected to your Linux box with a driver that supports it. You can’t do anything with it yet, but that’s where Motion comes in.

Install Motion either by using your distribution’s method (e.g., it’s apt-get install motion for Debian) or by downloading the tar file, uncompressing it, and following the installation instructions.

Now you’re ready to start doing the interesting bits. Start by typing motion on the command line. To test the software, wave your hand in front of the camera. If you’ve managed to detect motion, you should see output similar to:

File of type 1 saved to: /tmp/01-20060127105755-03.jpg
26898
New threshold: 1500
File of type 1 saved to: /tmp/01-20060127105755-04.jpg
26571
New threshold: 1500
243
New threshold: 1500

Take a look at the images the application saved; you’ll probably see a dark image with a blurry something in the middle of it. At least this proves that the motion detector works. Now you need to halt Motion and start customizing its operation to suit your requirements. To stop Motion, use ps to find the process number and then use kill on it.

You can use a number of command-line flags to change how Motion operates, but you’ll find it much easier to use its config file, which by default is /etc/motion/motion.conf. To make your own customizations, create a directory called .motion in your home directory and copy the config file into the new folder.

First, set up Motion so that it works as a webcam viewer, so you can see the effects of your changes to the conf file immediately. Edit ~/.motion/motion.conf and change the web_port and webcam_localhost lines so that they read:

webcam_port 8000
webcam_localhost off

Then add the following lines:

snapshot_interval 1
snapshot_filename snapshot

Restart Motion — remember to kill the process and then retype motion on the command line. You should see something like this:

$ motion
Processing thread 0 - config file /home/mydir/.motion/motion.conf
Thread0 device: /dev/video0 input: 8
File of type 2 saved to: /tmp/snapshot.jpg
File of type 2 saved to: /tmp/snapshot.jpg
File of type 2 saved to: /tmp/snapshot.jpg

All you have to do now is open up a Web browser (either on the PC where Motion is running or on any other PC on your network) and type in the URL of the machine followed by the port number that you defined in the conf file. This should let you view the output from the webcam (updated once a second).

You’ll probably find that you’ve now got a live but dark image. You can rectify this by changing auto_brightness to on and restarting Motion.

Now you’ve got a working webcam, but what you really want is a working motion detector. The step from one to the other is very small — simply edit motion.conf and add the line:

locate on

Restart Motion, wave your hand in front of the camera again, and have a look at the display in your Web browser. This time you’ll see a box marking the area where motion was detected.

You’re now able to see that the motion detector is working, but what’s the point of having a motion detector if you’re just going to have to sit and watch a screen? Also, if you do a ls /tmp, you’ll find dozens of JPEG files produced by the software. Obviously, Motion isn’t of much use as it stands. You can do a number of things to make it useful. First, add the following lines to the conf file:

threshold_tune off
threshold 2000

This allows you to set your own levels at which Motion will trigger, making it much more discriminatory. Next, you can add the line:

ffmpeg_cap_new on

Now the software will save video files as well as JPEG files — and video files are much more useful. Even so, the files are not much use if you have to come into the office and review the files in order to see if any motion has been detected. However, Motion lets you execute a command to determine when motion has been detected. Add this line:

execute /home/mydir/motion/on_motion_detected

This lets you run the file /home/mydir/motion/on_motion_detected as soon as the motion detection is triggered. All you have to do is write some code to the file to be triggered:

DATE=$(date +"%Y%m%d%H%M%S")
ALARM_EMAIL="/tmp/myalarm.tmp"
echo "Subject: Motion detected - $DATE" > $ALARM_EMAIL
echo "" >> $ALARM_EMAIL

echo "Motion detected - check $DATE.avi" >> $ALARM_EMAIL
cat $ALARM_EMAIL | /usr/sbin/ssmtp author@markbain-writer.co.uk 

This code will run every time Motion does its stuff. (Don’t forget to use your email address instead of mine!)

Now, whenever you wave your hand in front of the camera, Motion sends you an email telling you so. You may notice that the motion detector appears to trigger once and then wait awhile before it triggers again. There’s nothing wrong — a 60-second delay is built into the system. You can change the interval by adding the gap parameter. For instance, to create a 10-second delay between triggers, add the line:

gap 10

Have a look at the Motion man entry for the full list of parameters for the conf file. You’ll find useful items such as text_left, which allows you to add text to the pictures taken.

There you have it — a simple and cheap but effective motion detector. One final word of advice though: when setting up your motion detector, don’t put it where your cat can get at it. It’s amazing how quickly she can fill your email inbox.