Tip how to use existing IP camera's to act as motion detection sensors

Today i got all my IP camera’s to act as motion detectors/sensors.

What I did:

  • installed the APP ‘Virtual Motion Sensor’ and created new sensors, for each camera i want to use/add
  • configured motion detection on the camera (sensitivity etc), if this is not possible you cannot use this tip!

I have found 2 different types of camera’s with motion detection. Camera’s with an option to call an http event and camera’s with (only) FTP option.

For camera’s with NO direct http call but with motion detect buildin and an option to FTP it is possible but is more of a challenge, i solved this the following way

  • you need a (linux) server or nas with FTP enabled, this server must be always ON, if possible in your local LAN, can be outside but be sure you setup your firewall and port forwarding correct.
  • create a user and folder on the server and configure the camera to FTP a screenshot on motion into that folder, be sure each camera uses an other (unique) filename. If possible let the camera overwrite eachtime the screenshot with the same name, if this is not possible then create a sub folder which is specific for this camera and upload the files into that folder. Test if the camera uploads a screenshot to this folder on motion.

My setup:

  • 3x d-link camera’s, 1x HD foscam camera, 3x axis camera
  • vera LITE unit with APP ‘Virtual Motion Sensor’ installed
  • synology NAS
  • all axis camera have http call event option, this was easy, so no further setup for this camera’s
  • d-link and the foscam HD have only mail and ftp option on motion, for these i used the NAS
  • on my NAS i created a user called ‘vera’ and a screenshot folder, local path on a synology nas: /volume1/homes/vera/screenshots
  • all camera’s correctly FTP and upload the screenshots files into this folder.
  • d-link have a single file option, so the files are called: /volume1/homes/vera/screenshots/hall.jpg /volume1/homes/vera/screenshots/kitchen.jpg etc
  • foscam creates a new folder with an extra sub folder and uploads its files in to that subfolder, it also creates names from timestamp, for example /volume1/homes/vera/screenshots/foscam_9818101/shot/living_1605101501.jpg /volume1/homes/vera/screenshots/foscam_9818101/shot/living_1605101502.jpg

Now the camera part is correctly setup, they all upload screenshots to the NAS.

I then used a php script to read the ‘/volume1/homes/vera/screenshots’ folder
In this script I defined each cameras unique filename or the unique folder name (foscam).
If a defined file or folder name is present i then call using curl the api url to the vera unit with the correct ID and then DELETE the file I detected.

The last thing to do was to be sure this script runs constantly, i therefore created a little bash script where i used a while to loop it constantly for running the php script. To make sure it keeps running i placed this script into the /etc/init so the server(nas) will run it on boot and restarts it when it crashes.

The result is now when i walk by each camera in my house the corresponded motion sensor reports motion within 1 second!

All my camera’s in my network are now also acting als motion detectors, i now can be more precise to create scenes when someone is present without to have to buy extra PIR sensors.

I hope this bring people to idea’s to make apps or use there camera’s as detectors even if they have no direct support for it!

Thanks for sharing, great stuff!

If there is any interest i can post the scripts i used.
Just simply upload them to a (linux) server of NAS and it should work.

Today i finetuned the scripts and now all my camera’s act as motion sensor, also foscam HD and D-link which only have ftp upload options

[quote=“B3rt, post:3, topic:187335”]If there is any interest i can post the scripts i used.
Just simply upload them to a (linux) server of NAS and it should work.

Today i finetuned the scripts and now all my camera’s act as motion sensor, also foscam HD and D-link which only have ftp upload options[/quote]

Yes please

Here is the PHP script:

change veraIP and default destination folder, sometime also port number (only if you changed it in vera itself)

cam_vera.php

<?php
$vera_ip = "192.168.1.3";
$vera_port = "3480";
// default FTP folder for all the camera screenshots
$default_folder = "/volume1/homes/camera/screenshots";

// usage: checkMotion( $value1, value2, $value3, $value4);
// value1:  full path to folder to monitor
// value2:  filename or folder name to monitor/watch
// value3:  vera deviceId of security sensor to set
// value4:  strict or wildcard, strict = must match exactly as value2, wildcard monitor file only using * wildcards, wildcard must be set in value2 also:
// example strict: checkMotion( "/volume1/homes/camera/screenshots", "hall.jpg", 123, 'strict' );
// example wildcard: checkMotion( "/volume1/homes/camera/screenshots", "*kitchen*", 124, 'wildcard' );

// these are my 7 camera's which I monitor, so create for each camera you want to monitor a new line with the function call:
// you can use for each camera a separate destination folder, in my setup all camera upload there screenshots in the same default upload folder.
checkMotion( $default_folder, "hal.jpg", 159, 'strict' );
checkMotion( $default_folder, "berging.jpg", 160, 'strict' );
checkMotion( $default_folder, "boven.jpg", 161, 'strict' );
checkMotion( $default_folder, "achterdeur.jpg", 163, 'strict' );
checkMotion( $default_folder, "FI9821W_C4D6553A136F", 164, 'strict' );
checkMotion( $default_folder, "*achterplaats*", 165, 'wildcard' );
checkMotion( $default_folder, "*parkeerplaats*", 166, 'wildcard' );

// these are the functions, no change here
function checkMotion( $path, $filename, $id, $search_type = "strict" )
{
    $full_path = $path . "/" . $filename;
    if ( $search_type == "strict" )
    {
        if ( file_exists( $full_path ) )
        {
            if ( call_API( $id ) )
            {
                system( "rm -rf " . escapeshellarg( $full_path ) );
                return true;
            }
        }
        return false;
    } elseif ( $search_type == "wildcard" )
    {
        if ( glob( $full_path ) )
        {
            if ( call_API( $id ) )
            {
                system( "rm -rf " . $full_path );
                return true;
            }
        }
        return false;
    }
    else
    {
        return false;
    }
}

function call_API( $id, $onTime = 300 )
{
    global $vera_ip, $vera_port;
    // $onTime does nog work anymore,maybe in a later version of the vmotion app.
    $url = "http://" . $vera_ip . ":" . $vera_port . "/data_request?id=action&DeviceNum=" . $id . "&serviceId=urn:dcineco-com:serviceId:VMotion1&action=SetTripped";
    set_time_limit( 0 );
    $c = curl_init();
    curl_setopt( $c, CURLOPT_URL, $url );
    curl_setopt( $c, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt( $c, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $c, CURLOPT_TIMEOUT, 10 );
    curl_setopt( $c, CURLOPT_CONNECTTIMEOUT, 5 );
    $contents = curl_exec( $c );
    curl_close( $c );
    if ( preg_match( '/error/i', $contents ) )
    {
        return false;
    }
    else
    {
        return true;
    }
}

?>

This is the bash script I created, chmod it with +x and change the patch to script and php path if different on your linux server/nas

note: you can run it in background as kernel process or as cron:

in background use:
camera_api.sh

#!/bin/sh
PATH_TO_SCRIPT=/volume1/homes/admin/scripts
PATH_TO_PHP=/usr/bin/php

while true
do
   cd  $PATH_TO_SCRIPT
   $PATH_TO_PHP cam_vera.php
   sleep 1
done

To get it to run constantly I had to add a conf file and place it into the /etc/init directory, I called it: vera.conf

description	"start api to vera motion detection"
start on stopped rc
respawn
respawn limit 5 10
console none
exec /volume1/homes/admin/scripts/camera_api.sh

When uploaded start the script from the init directory:
start vera

as cronjob:
You can also run it by cron, if your want this then use the following camera_api.sh instead:

#!/bin/sh
PATH_TO_SCRIPT=/volume1/homes/admin/scripts
PATH_TO_PHP=/usr/bin/php

x=1
while [ $x -le 60 ]
do
  cd  $PATH_TO_SCRIPT
  $PATH_TO_PHP cam_vera.php
  sleep 1
  x=$(( $x + 1 ))
done

create now a cron (most times in /etc/crontab) to run the bash script every minute:

          • root sh /volume1/homes/admin/scripts/camera_api.sh
            (when running as cron there is no need for a vera.conf file)

Now it should run and monitor the directory in the php script and call vera if a screenshot with defined name and folder is uploaded into it, after calling the vera unit it will delete the screenshot.

obligated to run:

  • FTP server
  • PHP 5 or newer
  • linux server with /etc/init folder to run scripts in background or cron options
  • vmotion plugin/addon installed on vera unit
  • camera with motion detection and FTP snapshot upload functions

I tested this on a synology NAS using dlink and foscam camera’s, on my vera3 LITE unit i used the Virtual Motion Sensor app version v1.1

Note:
no warranty so use it at your own risk!

Cool Stuff B3RT. Thanks for sharing.

Can you share axis settings please?

Sure, log into the camera as root/administrator
Goto setup (top left) and choose ‘events’ and ‘add http server’
In the http settings pupup enter:
name: veramotion (does not matter)
url: http://VERAIP:3480/data_request?id=action&DeviceNum=DEVICEID&serviceId=urn:dcineco-com:serviceId:VMotion1&action=SetTripped
(replace VERAIP and DEVICEID the your vera values, ip and device id of vmotion sensor)
click ‘OK’ to save

now click ‘Motion Detection’, accept all java popups or install java to get this section to work.
Set here al your motion detection settings such as sensitivity, object size etc

now open ‘event types’ and ‘add triggered’
Here enter a name (of your choosing) in the pulldown menu (triggered by) choose ‘motion detection’ and then window name created in ‘motion detection’ section.
Choose ‘When triggered…’ the option ‘Send HTTP notification’, choose in pulldown menu the vera unit, custom parameters leave empty and in message enter ‘test’ or something, this may not be emtpy (for unknown reasons), vera unit does nothing with it btw.
Press OK to save.

Now axis camera is directly calling the http api of vera when motion is detected :slight_smile: