Perl Script to Grab Picture from a Network Camera

The script in this example can be used to grab a webcam or network camera image.

It is a simple Perl script. You just need to adapt the settings to fit your configuration.

You could also implement it using CRON.

With these two simple scripts, you can get MisterHouse to take a snapshot from a network camera, based on any MisterHouse event. I am using an X10 tricky interface from my alarm system called $alarm and an Axis network camera server.

First make a file called MH/bin/get_cam
This is perl code that will retrieve a snapshot from your network camera.
$storedir should be set to the location you wish to have the picture placed.
$camurl is where you could point your browser to see a snapshot from your camera.
IMPORTANT: Right-click on your video camera's picture feed and choose "properties", then paste the URL into a new browser window. You should see ONLY the snapshot in the browser window!
Some cameras offer a motion -or- refresh image feed. Choose refresh image feed, then drill down to the jpg source. If your camera does not serve still pictures, the script here is unsuitable.
WebCam2000 is a program that will turn a Windows computer - that has a webcam - into a suitable source.

$basename plus HHMM timestamp plus $ext equals the stored filename.
So with the settings below, at 10:48PM, the filename would be Cam1-2048.jpg
remember there are 1440 minutes in a day, so you could end up with 1440 files!
(that's why I didn't add seconds or date to the filename, if you want to - it's your hard drive!)

    #!/usr/bin/perl -w
    use strict;         # Enforce safe programming
    use warnings;       #
    use File::Basename; # file name handling
    use LWP::Simple;    # WWW interface, exports HTTP::Status

    # Force the output buffer to flush
    $| = 1;
    # Set default values
    my $storedir = "/home/public/monitoring/";
    my $basename = "Cam1-";
    my $camurl = "";
    my $ext = ".jpg";
    my $s = "0";
    my $m = "0";
    my $h = "0";
    my $mday = "0";
    # If the storage directory does not exist, create it.
    if( ! -e $storedir )
    mkdir( $storedir ) || die "Could not create $storedir: $!";
    # Generate file name
    ( $s,$m,$h,$mday ) = localtime( time );
    if( $s < 10 ) {$s = "0$s" }
    if( $m < 10 ) {$m = "0$m" }
    if( $h < 10 ) {$h = "0$h" }
    if( $mday < 10 ) {$mday = "0$mday" }
    my $fullFilename = "$storedir$basename$h$m$ext";
    # Get the file and store it locally
    my $response = getstore( $camurl, $fullFilename ) || die "Could not create $fullFilename: $!";
    # Check for a valid 'get' (RC_OK defined in HTTP::Status)
    if( $response == RC_OK )
    if( -z $fullFilename )
    print "File was 0 bytes. Removing.";
    unlink $fullFilename;
    print "HTTP response was $response. Could not download the image.";
    exit 0; # =========== End of Main Program ===========

Don't forget to CHMOD the file so it has execute permissions.

Next you need a new user code file. Store it in your MH user code directory.
i.e: MH/Code/Test/

if (state_now $alarm eq 'motion') {
    run 'get_cam';        

Remember to use the state_now function so that there isn't a picture taker spawned every loop!
(which is milliseconds!) Then activate the user code in MisterHouse.

By making several files and naming them get_cam1, get_ cam2, ect... you can have MisterHouse take pictures from multiple cameras.

You could use the time_now function to have a picture taken at a specific time every day.

You could set a timer, and take a picture every 5 minutes.