# Hikvision PTZ Control

**URL:** <https://community.openhab.org/t/hikvision-ptz-control/113686>\
**Category:** Tutorials & Examples\
**Created:** [January 10, 2021, 1:15pm UTC](https://community.openhab.org/t/hikvision-ptz-control/113686 "2021-01-10T13:15:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![LordFido](https://community.openhab.org/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@LordFido](https://community.openhab.org/u/LordFido)\
**Post date:** [January 10, 2021, 1:15pm UTC](https://community.openhab.org/t/hikvision-ptz-control/113686/1 "2021-01-10T13:15:28Z")

</div>

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."]

```

 ![Sitemapbild.PNG](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/1/a/1a2b3b14d4791db4a56d878ba5dae66c2677c6d5.jpeg)

```
*.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  
 ](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&ved=2ahUKEwiXqoPFoJHuAhWRCOwKHYuKDSYQFjACegQIAhAC&url=ftp%3A%2F%2Fftp.luis.ru%2FRaznoe%2FLTV_SDK%2FLTV_IP_S-Series_API%2FISAPI_2.0-PTZ%2520Service.pdf&usg=AOvVaw23Oe_YmRswv9HCi7h3s5tI)

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:

 ![Bild_2021-01-10_130114](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/e/7/e75d59d80f79ee46b00be31d2a5041075867bcac.png)

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. 😁

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

---

<div class="post-metadata">

**Author:** ![system](https://community-openhab-org.s3-eu-central-1.amazonaws.com/original/2X/6/6a52f2ac5d00e4c7b50471a354b3a9221188be90.png) [@system](https://community.openhab.org/u/system)\
**Post date:** [February 21, 2021, 5:29am UTC](https://community.openhab.org/t/hikvision-ptz-control/113686/2 "2021-02-21T05:29:37Z")

</div>

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