Sending CURL command with an XML payload ? Hikvision Camera

Hi

I’m playing around with the API on my Hikvision IP camera.

Reading this thread here I have just been able to send curl commands from my Windows laptop, to the camera to turn on or off the motion detection and that worked.

However to do this I had to create two XML files, one for on and one for off.

MotionDetectionOn.xml

<MotionDetection xmlns="http://www.hikvision.com/ver20/XMLSchema" version="2.0">
<enabled>true</enabled>
<MotionDetectionLayout version="2.0">
</MotionDetectionLayout>
</MotionDetection>

MotionDetectionOff.xml

<MotionDetection xmlns="http://www.hikvision.com/ver20/XMLSchema" version="2.0">
<enabled>false</enabled>
<MotionDetectionLayout version="2.0">
</MotionDetectionLayout>
</MotionDetection>

Curl command for motion detection on:

curl -T MotionDetectionOn.xml http://admin:password@192.168.1.15/ISAPI/System/Video/inputs/channels/1/motionDetection

Curl command for motion detection off:

curl -T MotionDetectionOff.xml http://admin:password@192.168.1.15/ISAPI/System/Video/inputs/channels/1/motionDetection

I know how to send Curl commands from Vera in a scene for example but I don’t know about also using xml payload files.

Ideally I don’t want to even have to use the xml files and just have a single line curl command that turns on or off the motion detection on the camera, but searching on Google I haven’t seen anyway to do that yet.

I guess I could just upload the xml files to Vera but its not ideal.

Searching for curl on Vera there are two locations however.

image

Any suggestions ?

Thanks

You don’t want to use ‘-T’ because that uses a different mechanism to transfer the data than the API is likely expecting. Use -d '@/path/to/MotionDetectionOn.xml'. Notice that I’ve enclosed the argument to “-d” in single quotes. If the argument to “-d” starts with an @, it is interpreted as a filename that is read and used for the payload. Otherwise (no @) the string is used directly as the payload. So this will read the file in and use its contents.

To run curl from scene Lua, you use os.execute("curl -s -m 15 -X POST [etc]")

I would recommend using SSH to log in to your Vera and running test curls from there until you get the right mix of options. I’ve included “-s” (silent) in my example, as well as “-m 15” (timeout 15 seconds), which I consider both recommended/best practice for “automated” requests. The “-X POST” tells it to use a HTTP POST rather than a GET, which is better/usually necessary when sending payload data (definitely necessary whenever the payload is/can be >1024 bytes, but usually fine when it’s smaller so you can just use it all the time).

os.execute("curl -s -m 15 -X POST -d '@/etc/cmh-ludl/MotionDetectionOn.xml' 'http://admin:password@192.168.1.15/ISAPI/System/Video/inputs/channels/1/motionDetection'")

In the above, also notice that I’ve enclosed the URL in single quotes. I do this habitually with shell arguments in case the string contains (or could contain, if dynamically generated) special characters that the shell may try to grab/interpret first. The biggest offender here, IMO, is “&”, which appears in URLs with query parameters. If that character is not quoted (or escaped, that’s another way), the shell will grab it and do odd things with the remainder of the string after, but still issue the query, just cut off at the point the “&” appears, causing odd behavior that can be difficult to debug. Shell quoting and escaping is a whole big topic unto itself, but you’re on pretty safe ground with the example you’ve given, so I won’t go down that rabbit hole further.

Edit: you may need to include -H 'Content-Type: application/xml' before the URL so that curl can tell the API it’s sending XML data in the payload. The API may assume it and you may be fine without it, but some APIs require you to be very literal.

1 Like

Thank you some great advice there, I will test it out some more.

@rigpapa

I’ve uploaded the two xml files in to the directory on Vera you suggested and I’ve tried running your command from the SSH terminal on Vera but it says:

-ash: syntax error: unexpected word (expecting “)”)

Which model do you use and what is your goal, i have managed to trigger manual recording of the camera via Vera without any xml files uploaded, just with use of curl command. So now i use triggers from vera to trigger and stop recordings.

For start recording
os.execute(“curl -v -X PUT -u user:pass -H ‘Content-Type: application/json’ http://ip_address/ISAPI/ContentMgmt/record/control/manual/start/tracks/101”)

For stop recording
os.execute(“curl -v -X PUT -u user:pass -H ‘Content-Type: application/json’ http://ip_address/ISAPI/ContentMgmt/record/control/manual/start/tracks/101”)

Tested and works on
DS-2CD2420F-IW and DS-2CD1023G0-I

Tested does not work (camera don’t support manual recording)
DS-2CD2412F-IW, HWC-P120-D/W - HiWatch sub brand of Hikvision

If you have some other model please write me the model i can test it tomorrow.

Its a mini bullet cam DS-2CD2045FWD-I.

I already know about the start and stop recording URLs I found them this morning online.

Those do work with my camera and in the playback window its marked in yellow as “manual”.

However to enable and disable the motion detection completely I could not find single line Curl commands hence the xml files.

1 Like

Try combining the lua code from this thread with your xml files.

1 Like

It might help if you posted the entire command line you tried?

Edit: if you posted the “os.execute” part to the SSH command line, that’s incorrect. The “os.execute” is the scene Lua version. If you’re on the SSH command line, you just submit the stuff inside the double-quotes (i.e. starting with the word “curl” and ending with the closing single quote on the URL). Sorry if that wasn’t clear.

My bad yes I had the os.execute in the SSH command.

Something it still doesn’t like though.

<?xml version="1.0" encoding="UTF-8"?>
<ResponseStatus version="2.0" xmlns="http://www.hikvision.com/ver20/XMLSchema">
<requestURL>/ISAPI/System/Video/inputs/channels/1/motionDetection</requestURL>
<statusCode>4</statusCode>
<statusString>Invalid Operation</statusString>
<subStatusCode>methodNotAllowed</subStatusCode>
</ResponseStatus>

Looks like @eonnet is using PUT, so just change the POST to PUT and try again.

1 Like

Nice one ! Its working now. :grinning:

<ResponseStatus version="2.0" xmlns="http://www.hikvision.com/ver20/XMLSchema">
<requestURL>/ISAPI/System/Video/inputs/channels/1/motionDetection</requestURL>
<statusCode>1</statusCode>
<statusString>OK</statusString>
<subStatusCode>ok</subStatusCode>
</ResponseStatus>
2 Likes

I’ve added a virtual switch for on / off and added some PLEG logic to send the commands to turn on or off the motion detection on the camera.

Handy now as there is washing out on the line blowing about, so I can now easily turn off motion detection and it stops all the alerts going to my phone and stops the camera from recording.

I can even ask Google Home to turn on or off that virtual switch as well.

image

Imperihome - Camera Motion Detection - Turned Off:

Imperihome - Camera Motion Detection - Turned On:

3 Likes

Digging up an old post from cw-kid.

I have followed all the instructions from Rigpapa, but still can’t get it to work. I’m trying to set the motion dectection of my Hikvision camera to ON.

On the command prompt I’m using:
curl -T motionDetection_on.xml http://admin:password@84.192.221.112:65002/ISAPI/System/Video/inputs/channels/1/motionDetection

This is working perfectly.

I get the following:

<?xml version="1.0" encoding="UTF-8"?>
<ResponseStatus version="2.0" xmlns="http://www.isapi.org/ver20/XMLSchema">
<requestURL>/ISAPI/System/Video/inputs/channels/1/motionDetection</requestURL>
<statusCode>1</statusCode>
<statusString>OK</statusString>
<subStatusCode>ok</subStatusCode>
</ResponseStatus>

Then went to Putty and logged in to my Vera3 (SSH).
There i’m using:
curl -s -m 15 -X PUT -d '@/etc/cmh-ludl/motionDetection_on.xml' 'http://admin:password@84.192.221.112:65002/ISAPI/System/Video/inputs/channels/1/motionDetection'

where motionDetection_on.xml=

<MotionDetection xmlns="http://www.hikvision.com/ver20/XMLSchema" version="2.0">
	<enabled>true</enabled>
	<MotionDetectionLayout version="2.0">
	</MotionDetectionLayout>
</MotionDetection>

Result:

<?xml version="1.0" encoding="UTF-8"?>
<ResponseStatus version="2.0" xmlns="http://www.isapi.org/ver20/XMLSchema">
<requestURL>/ISAPI/System/Video/inputs/channels/1/motionDetection</requestURL>
<statusCode>3</statusCode>
<statusString>Device Error</statusString>
<subStatusCode>deviceError</subStatusCode>
</ResponseStatus>

I have no idea how to go further. Any help is welcome.

This is all still working for me.

The command I run in my Vera scene or PLEG action is:

os.execute("curl -s -m 15 -X PUT -d '@/etc/cmh-ludl/MotionDetectionOn.xml' 'http://admin:Password@192.168.0.14/ISAPI/System/Video/inputs/channels/1/motionDetection'")

And the contents of the MotionDetectionOn.xml file in the /etc/cmh-ludl folder:

<MotionDetection xmlns="http://www.hikvision.com/ver20/XMLSchema" version="2.0">
<enabled>true</enabled>
<MotionDetectionLayout version="2.0">
</MotionDetectionLayout>
</MotionDetection>

Looks like your using a WAN IP and port number. The Vera hub is always local to the IP cams no? So why are you using a WAN IP and port ?

Try running the command again only using the LAN IP address of the IP camera.

i changed it to a LAN ip:

curl -s -m 15 -X PUT -d '@/etc/cmh-ludl/motionDetection_on.xml' 'http://admin:password@192.168.1.107:8080/ISAPI/System/Video/inputs/
channels/1/motionDetection'

Still gettting an error :frowning_face:

<?xml version="1.0" encoding="UTF-8"?>
<ResponseStatus version="2.0" xmlns="http://www.isapi.org/ver20/XMLSchema">
<requestURL>/ISAPI/System/Video/inputs/channels/1/motionDetection</requestURL>
<statusCode>3</statusCode>
<statusString>Device Error</statusString>
<subStatusCode>deviceError</subStatusCode>

Could the port be the issue?

I don’t need to specify any port in my own command that works.

These are the port settings on my hikvision cam

Just a guess try taking the underscore char out of the xml file name.

Everything else looks good.

It could be because of the port number but not sure. As a test change the port number back to 80.

Removed the underscore, but that didn’t work.
Changing the 8080 port? It has been a long time that I installed the camera…at the time I remember I could not use the 80 port because it was blocked.