Foscam Clones

From ZoneMinder Wiki
Jump to: navigation, search

Overview

Foscam is one of the more popular makers of wireless IP Cameras priced towards the lower end of price ranges. Any other IP cameria that is compatible with Foscam tend to be called Foscam clones because of their popularity. Its also common to see compatible cameras called Unbranded IP Cameras or Chinese IP Cameras.

You can often tell the clones based on feature set alone. Cameras in this class support 3 resolutions (640x480, 320x240, 160x120), 3 frame rates (60Hz, 50Hz, and Outdoor), Pan+Tilt and either Zoom or Nightvision or both, ability to email and FTP images during motion detection, and streaming video to VLC client. There are more optional features but these are usually enough to detect clones.

These cameras tend to use similar set of hardware components and share a once common firmware code base. You can search for "Open IP Camera" for more information related to firmware and hardware.

Most information about interacting with these cameras can be found by searching for a PDF called "IPCAM CGI SDK 2.1". I also found it useful to use Firefox to monitor the requests posted to camera as I clicked on its web interface. You can do this by enabling Web Developer->Web Console menu option.

The following was tested on a Agasio a622w but should work with other brands of wireless IP cameras such as Foscam, EasyN, Wansview, and more.

General Zoneminder Setup

General
Name: IPCAM
Source Type: Remote
Function: Modect
Enabled: Tick
Maximum FPS: Empty
Alarm Maximum FPS: Empty
Source
Remote Protocol: HTTP
Remote Method: Simple
Remote Host Name: x.x.x.x (The IP used by camera)
Remote Host Port: 99 (The Port used by camera)
Remote Host Path: /videostream.cgi?user=admin&pwd= (default user account)
Remote Image Colours: 24 bit colour
Capture Width: 320 (You could use 640 x 480, I found 320 to be better on wireles)
Capture Height: 240
Control
Controllable: Tick
Control Type: IPCAM (need to create this first using Edit link)
Control Device: user=admin&pwd=
Control Address: x.x.x.x:yy

Foscam Forum Topic

Follow the foscam forum topic for the new script for original foscam and upgraded clones: http://foscam.us/forum/post44816.html#p44816

Control Setup

Create /usr/share/perl5/vendor_perl/ZoneMinder/Control/IPCAM.pm with following contents. This script supports a superset of camera controls and should work with any Foscam compatible camera. If your camera doesn't have all features then they will be disabled in Zoneminder.

# ==========================================================================
#
# ZoneMinder IPCAM Control Protocol Module, $Date: 2009-11-25 09:20:00 +0000 (Wed, 04 Nov 2009) $, $Revision: 0001 $
# Copyright (C) 2001-2008 Philip Coombes
# Modified for use with Foscam FI8918W IP Camera by Dave Harris
# Modified Feb 2011 by Howard Durdle (http://durdl.es/x) to:
#      fix horizontal panning, add presets and IR on/off
#      use Control Device field to pass username and password
# Modified June 5th, 2012 by Chris Bagwell to:
#   Rename to IPCAM since its common protocol with wide range of cameras.
#   Work with Logger module instead of Debug module.
#   Fix off-by-1 preset bug.
#   Support optional autostop timeout.
#   Add Zoom, Brightness, and Contrast support.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ==========================================================================
#
# This module contains the implementation of the IPCAM camera control
# protocol.
#
# This is a protocol shared by a wide range of affordable cameras that
# appear to share similar reference design and software.  Examples
# include Foscam, Agasio, Wansview, etc.
#
# The basis for CGI based API can be found on internet by searching for
# "IPCAM CGI SDK 2.1". Here is sample site that also developes replacement
# firmware for some hardware versions.
#
# http://www.openipcam.com/files/Manuals/IPCAM%20CGI%20SDK%202.1.pdf
#
package ZoneMinder::Control::IPCAM;

use 5.006;
use strict;
use warnings;

require ZoneMinder::Base;
require ZoneMinder::Control;

our @ISA = qw(ZoneMinder::Control);

our $VERSION = $ZoneMinder::Base::VERSION;

# ==========================================================================
#
# IPCAM Control Protocol
#
# ==========================================================================

use ZoneMinder::Logger qw(:all);
use ZoneMinder::Config qw(:all);

use Time::HiRes qw( usleep );

sub new
{
    my $class = shift;
    my $id = shift;
    my $self = ZoneMinder::Control->new( $id );
    my $logindetails = "";
    bless( $self, $class );
    srand( time() );
    return $self;
}

our $AUTOLOAD;

sub AUTOLOAD
{
    my $self = shift;
    my $class = ref($self) || croak( "$self not object" );
    my $name = $AUTOLOAD;
    $name =~ s/.*://;
    if ( exists($self->{$name}) )
    {
	return( $self->{$name} );
    }
    Fatal( "Can't access $name member of object of class $class" );
}

sub open
{
    my $self = shift;

    $self->loadMonitor();

    use LWP::UserAgent;
    $self->{ua} = LWP::UserAgent->new;
    $self->{ua}->agent( "ZoneMinder Control Agent/".ZM_VERSION );

    $self->{state} = 'open';
}

sub close
{
    my $self = shift;
    $self->{state} = 'closed';
}

sub printMsg
{
    my $self = shift;
    my $msg = shift;
    my $msg_len = length($msg);

    Debug( $msg."[".$msg_len."]" );
}

sub sendCmd
{
    my $self = shift;
    my $cmd = shift;
    my $result = undef;
    printMsg( $cmd, "Tx" );

    my $req = HTTP::Request->new( GET=>"http://".$self->{Monitor}->{ControlAddress}."/$cmd".$self->{Monitor}->{ControlDevice} );
    my $res = $self->{ua}->request($req);

    if ( $res->is_success )
    {
	$result = $res->decoded_content;
    }
    else
    {
	Error( "Error check failed:'".$res->status_line()."'" );
    }

    return( $result );
}

# Turn IO on (can be internally wired to IR's)
sub wake
{
    my $self = shift;
    Debug( "Wake - IO on" );
    my $cmd = "decoder_control.cgi?command=95&";
    $self->sendCmd( $cmd );
}

# Turn IO off (can be internally wired to IR's)
sub sleep
{
    my $self = shift;
    Debug( "Sleep - IO off" );
    my $cmd = "decoder_control.cgi?command=94&";
    $self->sendCmd( $cmd );
}

sub reset
{
    my $self = shift;
    Debug( "Camera Reset" );
    my $cmd = "reboot.cgi?";
    $self->sendCmd( $cmd );
}

sub moveConUp
{
    my $self = shift;
    my $params = shift;
    Debug( "Move Up" );
    my $cmd = "decoder_control.cgi?command=0&";
    $self->sendCmd( $cmd );
    my $autostop = $self->getParam( $params, 'autostop', 0 );
    if ( $autostop && $self->{Monitor}->{AutoStopTimeout} )
    {
        usleep( $self->{Monitor}->{AutoStopTimeout} );
        $self->moveStop( $params );
    }
}

sub moveConDown
{
    my $self = shift;
    my $params = shift;
    Debug( "Move Down" );
    my $cmd = "decoder_control.cgi?command=2&";
    $self->sendCmd( $cmd );
    my $autostop = $self->getParam( $params, 'autostop', 0 );
    if ( $autostop && $self->{Monitor}->{AutoStopTimeout} )
    {
        usleep( $self->{Monitor}->{AutoStopTimeout} );
        $self->moveStop( $params );
    }
}

sub moveConRight
{
    my $self = shift;
    my $params = shift;
    Debug( "Move Right" );
    my $cmd = "decoder_control.cgi?command=6&";
    $self->sendCmd( $cmd );
    my $autostop = $self->getParam( $params, 'autostop', 0 );
    if ( $autostop && $self->{Monitor}->{AutoStopTimeout} )
    {
        usleep( $self->{Monitor}->{AutoStopTimeout} );
        $self->moveStop( $params );
    }
}

sub moveConLeft
{
    my $self = shift;
    my $params = shift;
    Debug( "Move Left" );
    my $cmd = "decoder_control.cgi?command=4&";
    $self->sendCmd( $cmd );
    my $autostop = $self->getParam( $params, 'autostop', 0 );
    if ( $autostop && $self->{Monitor}->{AutoStopTimeout} )
    {
        usleep( $self->{Monitor}->{AutoStopTimeout} );
        $self->moveStop( $params );
    }
}

sub moveConUpLeft
{
    my $self = shift;
    my $params = shift;
    Debug( "Move Diagonally Up Left" );
    my $cmd = "decoder_control.cgi?command=90&";
    $self->sendCmd( $cmd );
    my $autostop = $self->getParam( $params, 'autostop', 0 );
    if ( $autostop && $self->{Monitor}->{AutoStopTimeout} )
    {
        usleep( $self->{Monitor}->{AutoStopTimeout} );
        $self->moveStop( $params );
    }
}

sub moveConDownLeft
{
    my $self = shift;
    my $params = shift;
    Debug( "Move Diagonally Down Left" );
    my $cmd = "decoder_control.cgi?command=92&";
    $self->sendCmd( $cmd );
    my $autostop = $self->getParam( $params, 'autostop', 0 );
    if ( $autostop && $self->{Monitor}->{AutoStopTimeout} )
    {
        usleep( $self->{Monitor}->{AutoStopTimeout} );
        $self->moveStop( $params );
    }
}

sub moveConUpRight
{
    my $self = shift;
    my $params = shift;
    Debug( "Move Diagonally Up Right" );
    my $cmd = "decoder_control.cgi?command=91&";
    $self->sendCmd( $cmd );
    my $autostop = $self->getParam( $params, 'autostop', 0 );
    if ( $autostop && $self->{Monitor}->{AutoStopTimeout} )
    {
        usleep( $self->{Monitor}->{AutoStopTimeout} );
        $self->moveStop( $params );
    }
}

sub moveConDownRight
{
    my $self = shift;
    my $params = shift;
    Debug( "Move Diagonally Down Right" );
    my $cmd = "decoder_control.cgi?command=93&";
    $self->sendCmd( $cmd );
    my $autostop = $self->getParam( $params, 'autostop', 0 );
    if ( $autostop && $self->{Monitor}->{AutoStopTimeout} )
    {
        usleep( $self->{Monitor}->{AutoStopTimeout} );
        $self->moveStop( $params );
    }
}

# command=1 is technically Up Stop but seems to work for all stops.
sub moveStop
{
    my $self = shift;
    Debug( "Move Stop" );
    print("autostop\n");
    my $cmd = "decoder_control.cgi?command=1&";
    $self->sendCmd( $cmd );
}

sub zoomConTele
{
    my $self = shift;
    my $params = shift;
    Debug( "Zoom Tele" );
    my $cmd = "decoder_control.cgi?command=16&";
    $self->sendCmd( $cmd );
    my $autostop = $self->getParam( $params, 'autostop', 0 );
    if ( $autostop && $self->{Monitor}->{AutoStopTimeout} )
    {
        usleep( $self->{Monitor}->{AutoStopTimeout} );
	$cmd = "decoder_control.cgi?command=17&";
	$self->sendCmd( $cmd );
    }
}

sub zoomConWide
{
    my $self = shift;
    my $params = shift;
    Debug( "Zoom Wide" );
    my $cmd = "decoder_control.cgi?command=18&";
    $self->sendCmd( $cmd );
    my $autostop = $self->getParam( $params, 'autostop', 0 );
    if ( $autostop && $self->{Monitor}->{AutoStopTimeout} )
    {
        usleep( $self->{Monitor}->{AutoStopTimeout} );
	$cmd = "decoder_control.cgi?command=19&";
	$self->sendCmd( $cmd );
    }
}

sub zoomConStop
{
    my $self = shift;
    my $params = shift;
    Debug( "Zoom Stop" );
    my $cmd = "decoder_control.cgi?command=17&";
    $self->sendCmd( $cmd );
}

# Increase Brightness
sub irisAbsOpen
{
    my $self = shift;
    my $params = shift;
    my $step = $self->getParam( $params, 'step' );
    my $brightness = 100;

    my $cmd = "get_camera_params.cgi?";
    my $resp = $self->sendCmd( $cmd );

    $brightness = int($1) if ( $resp =~ m/var brightness=([0-9]*);/ );
    $brightness += $step;
    $brightness = 255 if ($brightness > 255);
    Debug( "Iris Open $brightness" );
    $cmd = "camera_control.cgi?param=1&value=".$brightness."&";
    $self->sendCmd( $cmd );
}

# Decrease Brightness
sub irisAbsClose
{
    my $self = shift;
    my $params = shift;
    my $step = $self->getParam( $params, 'step' );
    my $brightness = 100;

    my $cmd = "get_camera_params.cgi?";
    my $resp = $self->sendCmd( $cmd );

    $brightness = int($1) if ( $resp =~ m/var brightness=([0-9]*);/ );
    $brightness -= $step;
    $brightness = 0 if ($brightness < 0);
    Debug( "Iris Close $brightness" );
    $cmd = "camera_control.cgi?param=1&value=".$brightness."&";
    $self->sendCmd( $cmd );
}

sub whiteAbsIn
{
    my $self = shift;
    my $params = shift;
    my $step = $self->getParam( $params, 'step' );
    my $contrast = 5;

    my $cmd = "get_camera_params.cgi?";
    my $resp = $self->sendCmd( $cmd );

    $contrast = int($1) if ( $resp =~ m/var contrast=([0-9]*);/ );
    $contrast += $step;
    $contrast = 6 if ($contrast > 6);
    Debug( "White In $contrast" );
    $cmd = "camera_control.cgi?param=2&value=".$contrast."&";
    $self->sendCmd( $cmd );
}

# Decrease Contrast
sub whiteAbsOut
{
    my $self = shift;
    my $params = shift;
    my $step = $self->getParam( $params, 'step' );
    my $contrast = 5;

    my $cmd = "get_camera_params.cgi?";
    my $resp = $self->sendCmd( $cmd );

    $contrast = int($1) if ( $resp =~ m/var contrast=([0-9]*);/ );
    $contrast -= $step;
    $contrast = 0 if ($contrast < 0);
    Debug( "White Out $contrast" );
    $cmd = "camera_control.cgi?param=2&value=".$contrast."&";
    $self->sendCmd( $cmd );
}

sub presetHome
{
    my $self = shift;
    Debug( "Home Preset" );
    my $cmd = "decoder_control.cgi?command=25&";
    $self->sendCmd( $cmd );
}

sub presetSet
{
    my $self = shift;
    my $params = shift;
    my $preset = $self->getParam( $params, 'preset' );
    my $presetCmd = 30 + (($preset-1)*2);
    Debug( "Set Preset $preset with cmd $presetCmd" );
    my $cmd = "decoder_control.cgi?command=$presetCmd&";
    $self->sendCmd( $cmd );
}

sub presetGoto
{
    my $self = shift;
    my $params = shift;
    my $preset = $self->getParam( $params, 'preset' );
    my $presetCmd = 31 + (($preset-1)*2);
    Debug( "Goto Preset $preset with cmd $presetCmd" );
    my $cmd = "decoder_control.cgi?command=$presetCmd&";
    $self->sendCmd( $cmd );
}
1;
__END__
# Below is stub documentation for your module. You'd better edit it!

=head1 NAME

ZoneMinder::Database - Perl extension for blah blah blah

=head1 SYNOPSIS

  use ZoneMinder::Database;
  blah blah blah

=head1 DESCRIPTION

Stub documentation for ZoneMinder, created by h2xs. It looks like the
author of the extension was negligent enough to leave the stub
unedited.

Blah blah blah.

=head2 EXPORT

None by default.



=head1 SEE ALSO

Mention other useful documentation such as the documentation of
related modules or operating system documentation (such as man pages
in UNIX), or any relevant external documentation such as RFCs or
standards.

If you have a mailing list set up for your module, mention it here.

If you have a web site set up for your module, mention it here.

=head1 AUTHOR

Philip Coombes, E<lt>philip.coombes@zoneminder.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2001-2008  Philip Coombes

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.3 or,
at your option, any later version of Perl 5 you may have available.


=cut

From here, Zoneminder must be configured to know about IPCAM controls. This is done by clicking the "Edit" link pointed out during general setup and then on the "Add New Control" button on new window.

Main
Name: IPCAM
Type: Remote
Protocol: IPCAM
Can Wake: Tick
Can Sleep: Tick
Can Reset: Tick
Move
Can Move: Tick
Can Move Diagonally: Tick
Can Move Continous: Tick
Pan
Can Pan: Tick
Tilt
Can Tilt: Tick
Zoom
Can Zoom: Tick
Can Zoom Continous: Tick
White
Can White Balance: Tick
Can White Balance Absolute: Tick
Min White Bal. Range: 1
Max White Bal. Range: 255
Min White Bal. Step: 1
Max White Bal. Step: 255
Iris
Can Iris: Tick
Can Iris Absolute: Tick
Min Iris Range: 1
Max Iris Range: 6
Min Iris Step: 1
Max Iris Step: 6
Presets
Has Presets: Tick
Num Presets: 16
Has Home Preset: Tick
Can Set Presets: Tick

Wake/Sleep control is mapped to IO ON/OFF Control. IO Control is sometimes used for IR LED Controls. If your camera does not support IO ON/OFF Control then leave the Wake/Sleep boxes unticked.

If your camera does not support Zoom then leave Has Zoom unticked.

The camera Tilt/Pan will work different then cameras Web interface. It will continously move until you click the middle circle to stop movement. If you prefer, you can set a non-zero value in the "Track Delay" box on Control tab so that movement will automatically stop after specified delay.

It would require an update to Zoneminder to behave like stock web interface. It sends Start Move on initial click and then Stop Move upon click release. That seems like a nice feature to build into Zoneminder to me.