Hikvision Doorbell / Videointercom integration

Sorry… but i don’t want install hie assistant only for the doorbell. (i’m a iobroker user) :wink:

ah ok, well you can also run it as standalone docker image, its a new feature, look at the docs
You need to use the SDK, since indoor panels dont accept ISAPI commands , port 80 is not open

or you can use this example script below, but the SDK library is needed

Hi, I have some trouble setting up the baresip client. I followed the steps on github carefully but i am stuck with option 2 for older indoor panels as i dont have the option in ivms to add extensions to my panel.

i get this error in asterisk log

WARNING[376]: res_pjsip_registrar.c:1166 find_registrar_aor: AOR '' not found for endpoint 'hikvision' (192.168.1.121:53311)

and this error in baresip addon

reg: sip:[email protected] (prio 0): 404 Not Found (Asterisk PBX 20.1.0)

In the previous steps the 7000 extension worked fine and i succesfully established connection with video using a softphone.

here are my configs

pjsip_custom.conf

; Place your own extensions here.
[7000]
type=endpoint
context=default
disallow=all
allow=ulaw,alaw
allow=h264
auth=auth7000
aors=7000
rtp_symmetric=yes
force_rport=yes
rewrite_contact=yes
direct_media=no
max_audio_streams=10
max_video_streams=10
from_domain=asterisk.com

[auth7000]
type=auth
auth_type=userpass
password=1234
username=7000
 
[7000]
type=aor
max_contacts=1
remove_existing=yes
remove_unavailable=yes

[8000]
type=endpoint
context=default
disallow=all
allow=ulaw,alaw
allow=h264
auth=auth8000
aors=8000
rtp_symmetric=yes
force_rport=yes
rewrite_contact=yes
direct_media=no
max_audio_streams=10
max_video_streams=10
from_domain=asterisk.com

[auth8000]
type=auth
auth_type=userpass
password=1234
username=8000
 
[8000]
type=aor
max_contacts=1
remove_existing=yes
remove_unavailable=yes

####trunk registration

[hikvision]
type=aor
contact=sip:[email protected]:5060

[hikvision]
type=endpoint
context=default
disallow=all
allow=ulaw,alaw
allow=h264,vp8
aors=hikvision
direct_media=no

[hikvision]
type=identify
endpoint=hikvision
match=192.168.1.121

I also added this to my configuration.yaml and and created the associated pyhton file in the config folder

shell_command:
  hikvision_sip: nohup python3 /config/hikvision_register.py --ip 192.168.1.121 --domain 192.168.1.121 --username 10000000005 --extension 10000000005 --name Asterisk --password 1234 $1 > /dev/null 2>&1 &

Anyone has an idea what the problem could be?

I give you a hand tomorrow, ok?

Ok great! Thanks

Hi,

So calling from 8000 to 7000 works, right? So you 7000 does answer and provide you the rtsp stream?

In your she’ll command , you have 2 times IP .121 , that’s not good…

In my example :
–ip 192.168.0.17 --domain 192.168.0.71

0.17 is the IP of HA, Running asterisk/baresip , 0.71 is my indoor station where the script is registering on…

You are sending the register back to asterisk itself, but that endpoint doesn’t exist, that’s why you see the error

Also try first this command below, from an ssh addon, so you can see the output:

python3 /config/hikvision_register.py --ip 192.168.1.121 --domain 192.168.1.121 --username 10000000005 --extension 10000000005 --name Asterisk --password 1234

Inside ssh addon, run first this command: python3 -m pip install requests

Change the IP offcourse, 2 times .121 is not right…

If that works, then proceed with the shell command so it can run in background…

Alltough, I’m not 100 sure if the script/shell is needed…

I think if the script has been executed 1 time, the fake SN has been registered on the indoor (same as added manually with ivms for newer indoor) you can also use the trunk with register approach… But I can’t test this with my system, …

Once registered, you should also see on your indoor itself the new contact

@pergola.fabio
Thanks for your work!
I used your code to run hikvision-sdk in a separate container, I had to add MQTT integration for myself, because I can’t install add-on`s in my Home Assistant.
The code works great!

I just wanted to pay attention (I don’t know how important it is, maybe it’s just a part of the old code) to the “NET_DVR_CaptureJPEGPicture()” function. This part of the code is commented out here:

https://github.com/pergolafabio/Hikvision-Addons/blob/33d94d496bd9748050b798c6f31a2d25cd541907/hikvision-sdk/src/hik.py

According to this:
NET_DVR_CaptureJPEGPicture

NET_DVR_CaptureJPEGPicture - Return TRUE indicates success, FALSE indicates failure. If the interface fails to return, please call NET_DVR_GetLastError to get the error code, and judge the cause of the error through the error code.

That’s why:

# if res == False:
# os.system("Success")
#else:
# os.system("Grab stream fail")

may not be correct.

Here is part of my frame capture code:

class NET_DVR_JPEGPARA(Structure):
     _fields_ = [("wPicSize", c_uint16), ("wPicQuality", c_uint16)]


def NET_DVR_CaptureJPEGPicture():
     lpJpegPara = NET_DVR_JPEGPARA()
     lpJpegPara.wPicSize = 2
     lpJpegPara.wPicQuality = 1
     addr_jpg = byref(lpJpegPara)
     filename = time.strftime("%Y%m%d-%H%M%S") + "_doorbell.jpg"
     fullname = "screenshots/" + filename
     response = {'captured': filename}
     sJpegPicFileName = bytes(fullname, "ascii")
     res = HCNetSDK.NET_DVR_CaptureJPEGPicture(user_id, c_long(1), addr_jpg,
                                               sJpegPicFileName)
     if res == True:
         logger.info("Image capture - success: {}", fullname)
         os.chmod(fullname, 0o777)
         client.publish(config["mqtt_topic"] + '/response',
                        json.dumps(response),
                        qos=0,
                        retain=False)
     else:
         logger.error("Image capture to {} - error: {}", fullname,
                      HCNetSDK.NET_DVR_GetLastError())

It works well with DS-KV6113-WPE1(C) and DS-KH6320-WTE1. Perhaps this will be of some help.

1 Like

Hi , thnx for sharing, yeah I played before with the jpeg function, but stopped with it since I didn’t have a use case for it, so left it there… But can indeed usefully to grab a still image…

The addon is indeed changing, it can run as standalone now, but there was also a possibility to provide the states url in HA, so the sensors can be updated with the rest API of HA itself… So you didn’t need to add mqtt to it… That said, mqtt is also coming… So keep following this thread ;+)

I’m gonna play next week again with SIP trying to understand how it work, so it can be native used in HA… Since Voip is also coming to HA soon

1 Like

Hi and thank you for you contribution!
I am working to add official MQTT support to the container, to expand on the existing functionalities of the basic sensors created in HA, and to receive commands when run as a normal container.
The image capture API seems interesting, maybe we can think of using as part of a MQTT camera?
Would you mind discussing this topic in a Github issue to more easily share some ideas and code?

1 Like

Thanks for the response!

Yes calling from 8000 to 7000 works perfect with rtsp stream working.

I tried some things yesterday and updated the firmware of my indoor panel. now i got the option to add extensions. But it seems on the ivms menu that asterisk is’nt succesfully registering.

I get this error.

[Feb 26 21:01:09] WARNING[378]: res_pjsip_registrar.c:1166 find_registrar_aor: AOR '' not found for endpoint 'mytrunk' (192.168.1.70:5065)

Here is my latest pjsip_custom.conf
i gave my indoor panel the static IP 192.168.1.70

[7000]
type=endpoint
context=default
disallow=all
allow=ulaw,alaw
allow=h264
auth=auth7000
aors=7000
rtp_symmetric=yes
force_rport=yes
rewrite_contact=yes
direct_media=no
max_audio_streams=10
max_video_streams=10
from_domain=asterisk.com

[auth7000]
type=auth
auth_type=userpass
password=1234
username=7000

[7000]
type=aor
max_contacts=1
remove_existing=yes
remove_unavailable=yes

[8000]
type=endpoint
context=default
disallow=all
allow=ulaw,alaw
allow=h264
auth=auth8000
aors=8000
rtp_symmetric=yes
force_rport=yes
rewrite_contact=yes
direct_media=no
max_audio_streams=10
max_video_streams=10
from_domain=asterisk.com

[auth8000]
type=auth
auth_type=userpass
password=1234
username=8000

[8000]
type=aor
max_contacts=1
remove_existing=yes
remove_unavailable=yes

[outgoing]
type=endpoint
disallow=all
allow=ulaw,alaw
allow=h264
from_domain=asterisk.com

[mytrunk-auth]
type=auth
auth_type=userpass
password=test1234
username=10000000005

[mytrunk-aor]
type=aor
contact=sip:192.168.1.70:5065

[mytrunk-registration]
type=registration
outbound_auth=mytrunk-auth
server_uri=sip:192.168.1.70:5065
client_uri=sip:[email protected]:5065
retry_interval=10
contact_user=10000000005
expiration=600

[mytrunk]
type=endpoint
context=default
disallow=all
allow=ulaw,alaw
allow=h264,vp8
outbound_auth=mytrunk-auth
aors=mytrunk-aor
rewrite_contact=yes
from_domain=mydomain.com

[mytrunk]
type=identify
endpoint=mytrunk
match=192.168.1.70

The trunk config looks correct…can you start up the ssh addon, then install the tool sngrep, with the command: “APK add sngrep” then start the tool…
Then restart asterisk addon, normally you should see asterisk sending the register packet, can you show me the output?

I tried it but I dont see any output in the sngrep tool.

now i got this error in asterisk log

[Feb 26 21:26:40] WARNING[374]: res_pjsip_outbound_registration.c:1047 schedule_retry: No response received from 'sip:192.168.1.70:5065' on registration attempt to 'sip:[email protected]:5065', retrying in '10'

I tried ssh with cmd and now it gives this output.

Yeah, I saw that 404 too with other users too with older indoor panel, I think you still need my script can you try it and check again with sngrep?

can i leave the pjsip_custom.conf the same then?

No, try remove the complete trunk config… You added nr 5 to your Indoor as extension, right?

I still don’t know if older indoor stations always need to have the regxml part added in the script everytime the register packet is being send, maybe 1 time is enought not sure…

So first try to run the script and see if it can register successful

ok. yes i added it in the ivms config.

Ok, try first manually with the script as I told earlier…

python3 /config/hikvision_register.py --ip 192.168.1.121 --domain 192.168.1.70 --username 10000000005 --extension 10000000005 --name Asterisk --password 1234

Inside ssh addon, run first this command: python3 -m pip install requests