Hikvision PTZ Control

Here is my solution for controlling the ptz-function of a Hikvision camera (in my case DS-2DE2A404IW-DE3) on a raspberry pi (openhabian, openhab3.0).
For this to work basis auth has to be accepted by your camera.

*.sitemap	
            Switch item=ptz_pan mappings=[1="←",2="→"]
            Switch item=ptz_tilt mappings=[1="↓",2="↑"]
            Switch item=ptz_zoom mappings=[1="+",2="-"]
            Switch item=ptz_preset mappings=[1="Carport.", 2="Hintereing.", 3="Vordereing."]

*.items String ptz_pan "Drehen []"  <none>
String ptz_tilt "Kippen []"  <none>
String ptz_zoom "Zoomen []"  <none>
String ptz_preset "" <none>

For each kind of movement a rule takes the state of the item, executes a script and resets the state of the item, so it can be activated by the user again.

rule "ptz_pan" 
when
    Item ptz_pan changed to "1" or
    Item ptz_pan changed to "2"

then

 var var1 = ptz_pan.state.toString()
 createTimer(now.plusSeconds(2), [ |
 ptz_pan.sendCommand("0")])
 switch(var1) {

       case "1": {
       executeCommandLine("/etc/openhab/scripts/ptz_pan/ptz_panlinks.sh")
       //logInfo("ptz-pan", "←")
        }

        case "2": {
         executeCommandLine("/etc/openhab/scripts/ptz_pan/ptz_panrechts.sh")
        //logInfo("ptz-pan", "→")
        }
 }
end

For the movement tilt, zoom and the presets the rule looks and works in the same way.

On the internet I had found an pdf
ISAPI_2.0-PTZ Service.pdf

with which I am able to access onto the api of the camera.
By using (in my case) postman and the pdf I was able to create a curl-command which is now embedded in the script.
(Note: the script needs appropriate rights to be executed by openhab -->chmod…)

ptz_panrechts.sh:

 #!/bin/bash
curl --location --request PUT 'http://youripofthecamera:
usuallyitisport80/ISAPI/PTZCtrl/channels/1/relative' --header 'Authorization: Basic AddyourPostmanCodeforAuthorizationhere' --header 'Content-Type: application/xml' --data-raw '<PTZData>
    <Relative>
    <positionX>120</positionX>
    <positionY>255</positionY>
    <relativeZoom>0</relativeZoom>
    </Relative>
    </PTZData>
    '   

In my case I use the movement-method RELATIVE for pan, tilt and zoom:

The origin of ordinates is (in my case) in the lefthand-button-corner.
So for the view to stay still enter

 <positionX>128</positionX>
 <positionY>128</positionY>
<relativeZoom>0</relativeZoom>

Lowering Y move the view to the left, increasing it move the view to the right.
The same for X = tilt (up and down)
The number (whole ones) for:
Pan has to be between 0<=X<=255
Tilt has to be between 0<=Y<=255
Zoom has to be between -100<=Z<=100

Example for tilting upwards

<positionX>255</positionX>
<positionY>128</positionY>
<relativeZoom>0</relativeZoom>

My settings for panning and tilting are “0” , “255” and for zoom “-50” or “50”

For the presets I use another curl-command:
ptz_preset1.sh:

`#!/bin/bash` 
        curl --location --request PUT 'http:///youripofthecamera:
usuallyitisport80/ISAPI/PTZCtrl/channels/1/presets/add yourpresetidhere/goto' \
    --header 'Authorization: Basic AddyourPostmanCodeforAuthorizationhere' \

--header 'Content-Type: text/plain' \

--data-raw '{}'

(Note: like before you have to add your cameras ip, port, code for authorization (in my case generated by Postman (I dont know how its done manually) and the presetid)

I hope this example will save you time, while trying to get the ptz-control running. :grin:

And please comment and suggest your ideas and improvements on this topic, I am quite new to this kind of stuff.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.