We have moved at community.getvera.com

Author Topic: How to Trigger Hikvision Onboard Camera Recording using external PIR Sensor  (Read 576 times)

Offline TomS2

  • Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
First post!  And its going to be a long one because I'm so jazzed I got this working!  Like so many others, I've been struggling with the onboard image-based motion detection features on my new Hikvision cameras (model DS-2CD2543G0-IS) that I have outside, overlooking my front and rear doors and decks.  Its just been too difficult and time consuming to get these dialed in using either Basic Motion Detection or the Smart Intrusion and Line Crossing events.  Too many false positives and missed events.  False positives are an annoyance but a missed event could spell real trouble. 

So I started to think about how I could trigger recording on the camera using a more reliable PIR sensor that has pet immunity, which would include rodent immunity (raccoons, possums, squirrels, etc.)  With a PIR, I also don't have to worry about sun/shade, passing clouds, car lights, insects, rain, snow, lightning, etc.  I'm not using an NVR.  Instead, I'm using onboard recording to a 32gb MicroSD card, which are cheap and provide more than enough storage for lots of short duration events at 1080P and 8fps.

Unfortunately, and unlike the BI and Hikvision NVRs (both of which I evaluated and decided against), the camera itself does not have a simple HTTP command that could be used to programmatically start/stop recording.  But then I came across this post on the IPCamTalk forum that showed me the way:

https://ipcamtalk.com/threads/record-start-or-alarm-trigger-with-an-http-command.2901/

In there, a guy simply wired the Alarm Output to the Alarm Input on the pigtail and then issued an HTTP command to trigger Alarm Out, which is picked up by the Alarm Input, which then starts recording until the alarm is stopped.  Obviously, this will only work on Hikvision cameras that have an Alarm Input/Output pigtail.  Not all do.

Here's how:
1) First step is to disable all of the motion detection features on the camera (Basic and Smart Events)
2) Use some wire to cross-connect the alarm input, output and ground ports on the pigtail, as shown in the attached pic.
3a) Go to Basic Event - Alarm Input and enable it with Alarm Type "NO" (normally open).
3b) Set your arming schedule.  In my case, it's 24/7 since the PIR triggers could occur any time the PIR sensor is armed.
3c) Set your desired "normal linkage" (if any), and also select Trigger Recording.
3d) DO NOT select Trigger Alarm Out!
3e) Save your changes
4a) Go to Basic Event - Alarm Output and set the Delay to Manual.
4b) Leave the Alarm Output schedule blank and give it a name (e.g. CameraName-AlarmOut)
4c) Save your changes
5) Go to Storage - Schedule Settings and Enable 24/7 "Event" recording only.
5) Go to Network - Advanced - Integration Protocol and Enable Hikvision-CGI with the authentication method of your choice.
6) Go to System - Security and set the Web Authentication to the same method.  I'm using digest but digest/basic works, too.

To use the HTTP command that will trigger and stop "Alarm Out" on the camera, you will need to PUT an XML file that looks like this:

<IOPortData version="1.0" xmlns="http://www.hikvision.com/ver10/XMLSchema">
    <outputState>high | low</outputState>  -- "high" will trigger the Alarm Output and "low" will stop it.
</IOPortData>

To make this easy in my LUUP code, I created directory "/etc/local" on Vera and then created and stored two files: Camera-Start.xml and Camera-Stop.xml.  The start file sets outputState to "high" and the stop file sets it to "low".

You are now ready to start/stop camera recording based on any Vera event and scene you want to use.

In my case, I run the following scene:
"When the PIR is armed and detects motion", Run the following LUUP code:

local curlcmd = "curl -X PUT -u admin:yourpassword " .. "-" .. "-digest" .. " -T /etc/local/Camera-Start.xml http://camera.ip.address/IO/outputs/1/trigger"
local stdout = io.popen(curlcmd)
local response = stdout:read("*a")
stdout:close()   
if response:find("statusString>OK") then
   luup.log("CameraName Alarm Output Triggered and Recording Started")
   return true
else
   luup.log("CameraName Alarm Output Trigger Failed")
   return false
end

After this scene runs, if you go to the camera Basic Events - Alarm Out, you will see that it is now "ON".

To stop it, create a second scene for when "PIR is armed and STOPS detecting motion".  Use the same LUUP code with the Camera-Stop.xml file and different log messages.

Done!  I now have very reliable PIR triggered recording configured on my cameras, and its very quick to react.  The Alarm Output is triggered almost instantaneously, as soon as motion is detected and the scene runs.  I have 5 seconds of buffering configured for "recording start" and have yet to miss the initial motion during playback on the camera.  If you do miss the initial motion, consider setting the buffer to 10 seconds or more (under Storage - Schedule Settings - Advanced button).

Good luck and let me know what you think or if you have any questions or suggestions for improvement.

« Last Edit: January 10, 2019, 09:01:45 am by TomS2 »

Offline robie

  • Sr. Newbie
  • *
  • Posts: 41
  • Karma: +0/-0
Thanks for sharing this. I also have a hikvision dvr with alarm inputs and outputs.  Currently, i only use the alarm output via wiring a gocontrol door sensor to trigger my scenes.  Been looking for a way to trigger alarm input, so thanks for sharing.  This will be a next project.

Offline robie

  • Sr. Newbie
  • *
  • Posts: 41
  • Karma: +0/-0
To use the HTTP command that will trigger and stop "Alarm Out" on the camera, you will need to PUT an XML file that looks like this:

<IOPortData version="1.0" xmlns="http://www.hikvision.com/ver10/XMLSchema">
    <outputState>high | low</outputState>  -- "high" will trigger the Alarm Output and "low" will stop it.
</IOPortData>

To make this easy in my LUUP code, I created directory "/etc/local" on Vera and then created and stored two files: Camera-Start.xml and Camera-Stop.xml.  The start file sets outputState to "high" and the stop file sets it to "low".


Since i do not know how to create a directory and put a file in vera, can you share how to embed it in the LUUP code?  I also do not know how to do LUUP code aside from Copy Paste and minor edits, haha :-)