Sending image from Hikvision camera with mail binding

The e-mail is triggered as soon as someone rings the doorbell and nobody’s home.
With the image included, I can immediately see who was at the door, which also works on phones, etc.

Usually, I then delete these e-mails quickly. So this is not a storage application.

Daniel

danielgauss148 https://community.openhab.org/users/danielgauss148 Daniel
https://community.openhab.org/users/danielgauss148
July 27

The e-mail is triggered as soon as someone rings the doorbell and nobody’s
home.

yes I got that.

With the image included, I can immediately see who was at the door, which
also works on phones, etc.

I don’t understand why dropbox could not work with phones, I get dropbox
sync all the time with my phone too.

Usually, I then delete these e-mails quickly. So this is not a storage
application.

i’ll rephrase, e-mail is not optimized for sending files
yes it’ used a lot to send files,I do think it’s pretty bad at it…
The reason most peel do this, is that we don’t know what the software is
the other parties are supporting, in this case you are in control of both
sides (as it’s both you)

Daniel


Visit Topic
https://community.openhab.org/t/sending-image-from-hikvision-camera-with-mail-binding/12377/21
or reply to this email to respond.

To unsubscribe from these emails, click here
https://community.openhab.org/email/unsubscribe/d1282046b7ccb164a964e0e82d9fa7f208b2235bc31164c0baabe5e9da5a74a2
.

Yves Hanoulle

Thanks, Udo. Your code worked for me (using a FOSCAM C1 V2 and their API) with one exception…I had to drop the “5” from:

I also decreased my sleep time down to 3 seconds.

The code for Udo works fine when I use one Foscam camera. I would like to attach pictures from 2 cameras into a mail. I tried to modified the code, but it only send the picture from the first camera. What is wrong?

The rule seems to allways be executed twice (two mails/notification are sent), about 1 minute apart. How can this be?

rule "DoorBell"
when
	Item DEVICEBell3_Switch changed from OFF to ON
then
        {          
    var FrontDoorCam = "FrontDoor"+ now.toString ("HH-mm_dd-MM-yyyy") +".jpg"
   	var FrontDoorCam2 = "FrontDoor2"+ now.toString ("HH-mm_dd-MM-yyyy") +".jpg"
    var doorbellmessage = "Det ringde på dörren kl. "+ now.toString ("HH:mm dd-MM-yyyy") // +"\nMvH Patrik"  
    
    sendBroadcastNotification(doorbellmessage)
    executeCommandLine("wget http://192.168.2.10:88/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=YYYY&pwd=XXXX -O /tmp/" +FrontDoorCam)
	Thread::sleep(8000)
	executeCommandLine("wget http://192.168.2.11:88/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=YYYY&pwd=XXXX -O /tmp/" +FrontDoorCam2)
    Thread::sleep(8000)
    sendMail("hotmail@hotmail.com", "Någon Ringer på Dörren", doorbellmessage, "file:///tmp/" +FrontDoorCam,  "file:///tmp/" +FrontDoorCam2)
	Thread::sleep(60000)
              DEVICEBell3_Switch.sendCommand(OFF)
        }
end
end

I managed to get this working for my hikvision camera, but without the ending ,5 in the executeCommandLine
The next step is to delete old images (older then x days)

@patrikpatrik2
to send to attachments you need to put the locations of the images to an UrlList

val List attachmentUrlList = newArrayList(
“file:///tmp/” + FrontDoorCam,
“file:///tmp/” + FrontDoorCam2)

and don’t forget to add import java.util.List at the beginning of your rule

import java.util.List

rule "DoorBell"
when
    Item DEVICEBell3_Switch changed
then
    if (DEVICEBell3_Switch.state==ON) {
        val string FrontDoorCam = "FrontDoor"+ now.toString ("HH-mm_dd-MM-yyyy") +".jpg"
        val string FrontDoorCam2 = "FrontDoor2"+ now.toString ("HH-mm_dd-MM-yyyy") +".jpg"
        val string doorbellmessage = "Det ringde på dörren kl. "+ now.toString ("HH:mm dd-MM-yyyy") // +"\nMvH Patrik"  
        sendBroadcastNotification(doorbellmessage)
        executeCommandLine("wget http://192.168.2.10:88/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=YYYY&pwd=XXXX -O /tmp/" +FrontDoorCam)
        executeCommandLine("wget http://192.168.2.11:88/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=YYYY&pwd=XXXX -O /tmp/" +FrontDoorCam2)
        Thread::sleep(16000) //give openHAB time to download 2 pictures. Change time to your needs. 
                             //openHAB is able to download 2 files at once!
        val List attachmentUrlList = newArrayList(
            "file:///tmp/" + FrontDoorCam,
            "file:///tmp/" + FrontDoorCam2)
        sendMail("hotmail@hotmail.com", "Någon Ringer på Dörren", doorbellmessage, attachmentUrlList)
        DEVICEBell3_Switch.sendCommand(OFF)
        executeCommandLine("rm /tmp/FrontDoor*.jpg") //delete temp files
    }
end
1 Like

I added the following line to only keep the latest 10 files, just in case…

executeCommandLine(“ls -1tr /tmp/FrontDoor_* | head -n -10 | xargs rm -f”)

edit:
hmm strange, it’s not deteling anything, even with "rm /tmp/FrontDoor*.jpg"
I suppose a access right issue, but I tried already with giving all files 777 permissions, and it’s still not ok. All jpg’s have openhab-user as owner

when I run my command from a ssh session (openhabian user), it’s working fine.

Maybe you have to set the complete path to rm, ls, head and xargs:

executeCommandLine("/bin/ls -1tr /tmp/FrontDoor_* | /usr/bin/head -n -10 | /usr/bin/xargs /bin/rm -f")

Good suggestion, but didn’t solved it.

If I understand well, everything is runned by the openhab user ?
When I log on to the terminal, I’m openhabian user. I tried to switch to openhab user with su - openhab, but I receive an authentication error.

[11:34:54] openhabian@openHABianPi:/tmp$ id -G -n openhab
openhab tty dialout gpio

[11:35:15] openhabian@openHABianPi:/tmp$ id -G -n openhabian
openhabian adm tty dialout cdrom sudo audio video plugdev games users input netdev spi i2c gpio openhab

When executing my full command from openhabian user

/bin/ls -1tr /tmp/FrontDoor_* | /usr/bin/head -n -4 | /usr/bin/xargs /bin/rm -f

I received a message that it’s not allowed. When I add sudo in front of /bin/rm and type in the password, it works…

Question - does this work through Hikvision NVR’s or are you all referencing the cameras IP address direct? I have all 4 of my hikvision cameras connected direct to the NVR, the other NVR port has the IP for camera management to login too. I can’t reference the cameras IP’s directly as the NVR doesn’t act as a router.

1 Like

I’m not using an NVR i’m afraid. My cameras are all PoE to my router so easy to connect to over the lan.

Managed to get this to work with a Hikvision NVR, simply go into the admin GUI, then Configuration, Network, Advanced Settings, Other. Then tick ‘Enable Virtual Host’. Go back to Configuration, System, Camera Management, and there is a “Connect” column for each camera - it effectively NAT’s the NVR LAN mgmt IP with a specific port through to each IP camera, so now I can access each of my cameras via the NVR, by specifying:

http://username:password@nvr.ip.address:65001/Streaming/channels/1/picture
http://username:password@nvr.ip.address:65002/Streaming/channels/1/picture
http://username:password@nvr.ip.address:65003/Streaming/channels/1/picture
etc…

3 Likes

Here’s what is working for me . . .

rule “Front Door Camera Sees Motion”
when
Item HIKFrontDoorMotion changed
then

	if (HIKFrontDoorMotion.state == ON)  {

		logWarn("HIK Motion", "** HIK Front Door Motion Detected **")

		val string FrontDoorCam = "FrontDoor"+ now.toString ("HH-mm_dd-MM-yyyy") +".jpg"

		Thread::sleep(4000)  // 4 second wait

		executeCommandLine("wget http://xxx:xxx@192.168.0.11:81/ISAPI/Streaming/channels/101/picture -O /tmp/" +FrontDoorCam)

		Thread::sleep(8000) // give OH time to download picture

		// send an email
		var toemailfrontmotion = "xxxxxx@gmail.com"
		var subjectemailfrontmotion = "OpenHAB - HIK Motion Detected at Front Door"
		var bodyemailfrontmotion = "OpenHAB - HIK Motion Detected at Front Door."
		val URLfrontmotion = "file:///tmp/" + FrontDoorCam

		sendMail(toemailfrontmotion, subjectemailfrontmotion, bodyemailfrontmotion, URLfrontmotion)

		Thread::sleep(2000)  // 2 second wait

		toemailfrontmotion = ""
		subjectemailfrontmotion = ""
		bodyemailfrontmotion = ""

		executeCommandLine("rm /tmp/FrontDoor*.jpg")  // delete image file

		Thread::sleep(240000)  // 4 min wait for motion to clear so no excessive emails
	}

end

Best, Jay

1 Like

I updated today to the release version of 2.5
And then I get a warning on this code

Validation issues found in configuration model ‘camera.rules’, using it anyway:
List is a raw type. References to generic type List should be parameterized

When I google for generic lists, I get mostly java code, and not xtend.
Except for this https://www.eclipse.org/xtend/documentation/203_xtend_expressions.html
it took me a while to figure it out,

val List attachmentUrlList = newArrayList(
“file:///tmp/” + FrontDoorCam,
“file:///tmp/” + FrontDoorCam2)

should now be
var List attachmentUrlList = newArrayList

attachmentUrlList.add("file:///tmp/" + FrontDoorCam)
attachmentUrlList.add("file:///tmp/" + FrontDoorCam2)

I’m using this for a while now:

val List<String> attachmentUrlList = newArrayList(
     "file://" + LocationSS + FrontDoorCam)

When I use your code, I also get the warning message. After adding <String> after List, the message is gone.

1 Like