Sending image from Hikvision camera with mail binding

Did you try the commands on a terminal in the openhab box, one by one, to check things work that way?

I must now test yet

that’s my openhab log

2016-07-17 16:19:58.486 [INFO ] [g.openhab.io.net.exec.ExecUtil] - executed commandLine 'curl -m 10 -o /opt/openhab/webapps/static/pic.jpg http://user:pass@*****.selfhost.eu:58081/cgi-bin/video.cgi?msubmenu=jpg’
2016-07-17 16:19:58.512 [ERROR] [nhab.action.mail.internal.Mail] - Error adding attachment to email.
org.apache.commons.mail.EmailException: Invalid URL set:file:/opt/openhab/webapps/static/pic.jpg
at org.apache.commons.mail.MultiPartEmail.attach(MultiPartEmail.java:395) ~[commons-email-1.4.jar:1.4]
at org.apache.commons.mail.MultiPartEmail.attach(MultiPartEmail.java:339) ~[commons-email-1.4.jar:1.4]
at org.openhab.action.mail.internal.Mail.sendMail(Mail.java:120) [bundlefile:na]
at org.openhab.action.mail.internal.Mail.sendMail(Mail.java:89) [bundlefile:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_60]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_60]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_60]
at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_60]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:729) [org.eclipse.xtext.xbase_2.3.0.v201206120633.jar:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._featureCallOperation(XbaseInterpreter.java:713) [org.eclipse.xtext.xbase_2.3.0.v201206120633.jar:na]
at sun.reflect.GeneratedMethodAccessor958.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_60]
at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_60]
at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) [org.eclipse.xtext.util_2.3.0.v201206120633.jar:na]
at org.openhab.model.script.interpreter.ScriptInterpreter.internalFeatureCallDispatch(ScriptInterpreter.java:69) [org.openhab.model.script_1.8.2.jar:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateAbstractFeatureCall(XbaseInterpreter.java:658) [org.eclipse.xtext.xbase_2.3.0.v201206120633.jar:na]
at sun.reflect.GeneratedMethodAccessor959.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_60]
at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_60]
at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) [org.eclipse.xtext.util_2.3.0.v201206120633.jar:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:218) [org.eclipse.xtext.xbase_2.3.0.v201206120633.jar:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateBlockExpression(XbaseInterpreter.java:321) [org.eclipse.xtext.xbase_2.3.0.v201206120633.jar:na]
at sun.reflect.GeneratedMethodAccessor973.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_60]
at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_60]
at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) [org.eclipse.xtext.util_2.3.0.v201206120633.jar:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:218) [org.eclipse.xtext.xbase_2.3.0.v201206120633.jar:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:204) [org.eclipse.xtext.xbase_2.3.0.v201206120633.jar:na]
at org.openhab.model.script.internal.engine.ScriptImpl.execute(ScriptImpl.java:59) [org.openhab.model.script_1.8.2.jar:na]
at org.openhab.core.scriptengine.ScriptExecutionThread.run(ScriptExecutionThread.java:44) [org.openhab.core.scriptengine_1.8.2.jar:na]
Caused by: java.io.FileNotFoundException: /opt/openhab/webapps/static/pic.jpg (Datei oder Verzeichnis nicht gefunden)
at java.io.FileInputStream.open(Native Method) ~[na:1.7.0_60]
at java.io.FileInputStream.(FileInputStream.java:146) ~[na:1.7.0_60]
at java.io.FileInputStream.(FileInputStream.java:101) ~[na:1.7.0_60]
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90) ~[na:1.7.0_60]
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188) ~[na:1.7.0_60]
at java.net.URL.openStream(URL.java:1037) ~[na:1.7.0_60]
at org.apache.commons.mail.MultiPartEmail.attach(MultiPartEmail.java:390) ~[commons-email-1.4.jar:1.4]
… 29 common frames omitted
2016-07-17 16:20:05.666 [INFO ] [g.openhab.io.net.exec.ExecUtil] - executed commandLine 'rm /opt/openhab/webapps/static/pic.jpg’
2016-07-17 16:21:01.291 [INFO ] [g.openhab.io.net.exec.ExecUtil] - executed commandLine ‘rm /opt/openhab/webapps/static/pic.jpg’

Hi,

is /opt/openhab/webapps/static/ present in your installation? the default path for apt-get installs is /usr/share/openhab

Yes that’s it

https://openhabdoc.readthedocs.io/de/latest/Raspberry/
so I installed

nobody has an idea?

I use it on my frontdoor bell button and hikvision cam.

Create a rule.

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*

rule “Frontdoor bell”

when
Item frontdoor_doorbell changed to ON
then
var FrontDoorCam = “FrontDoor”+ now.toString (“HH-mm_dd-MM-yyyy”) +".jpg"
var doorbellmessage = “Det ringet på døren kl. “+ now.toString (“HH:mm dd-MM-yyyy”) +”\nVi var sikkert ikke hjemme"
executeCommandLine(“wget http://admin:passwd@192.168.1.???/Streaming/Channels/1/picture -O /tmp/” +FrontDoorCam”,5)
Thread::sleep(5000)
sendMail("mail@gmail.com", “Det ringer på døren”, doorbellmessage, “file:///tmp/” +FrontDoorCam)
sendMail("mail@gmail.com", “Det ringer på døren”, doorbellmessage, “file:///tmp/” +FrontDoorCam)
end

no it don’t work any more, don’t know why

Cam",5")

How it works

Please use Code formatter to see the error easily:

executeCommandLine("wget http://admin:passwd@192.168.1.???/Streaming/Channels/1/picture -O /tmp/" +FrontDoorCam",5)

so, there is a " where it shouldn’t be. Complete working code:

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*

rule "Frontdoor bell"
when
    Item frontdoor_doorbell changed to ON
then
    var FrontDoorCam = "FrontDoor"+ now.toString ("HH-mm_dd-MM-yyyy") +".jpg"
    var doorbellmessage = "Det ringet på døren kl. "+ now.toString ("HH:mm dd-MM-yyyy") +"\nVi var sikkert ikke hjemme"
    executeCommandLine("wget http://admin:passwd@192.168.1.???/Streaming/Channels/1/picture -O /tmp/" +FrontDoorCam,5)
    Thread::sleep(5000)
    sendMail("mail@gmail.com", "Det ringer på døren", doorbellmessage, "file:///tmp/" +FrontDoorCam)
    sendMail("mail@gmail.com", "Det ringer på døren", doorbellmessage, "file:///tmp/" +FrontDoorCam) 
end

Why sending the email twice?

Hi everyone,

I have also adapted one of my rules to use this store-send-timer-delete approach.
Now an image file is created, but when the e-mail including the attachment should be sent, I also see this error in the log:

2016-07-22 16:35:11.793 [ERROR] [nhab.action.mail.internal.Mail] - Error adding attachment to email.
org.apache.commons.mail.EmailException: Invalid URL set:file:/space/smarthome/temp/16722_163511_7.jpg

I’m running OpenHAB 1.8.1 on a gentoo linux box, and I don’t understand why in the log there is only one slash (/) between file: and my file’s location, even though the attachment in the sendMail command is configured as

"file:///space/" + outputFile

Is there some restriction on where the file needs to be stored, or what is happening here?

OP here still trying to get things to work. My guess with @danielgauss148 is that it is using an escape character. Basically if you did only two // you would comment out the code but adding a third shows the system only two.

so in open hab i think ///=//

Anyway.

I’m closer, but mine still isn’t working.

Here is my rule:

rule "Frontdoor bell"
when
Item photoSwitch changed to ON
then

executeCommandLine("wget http://admin:password@10.0.0.61/Streaming/Channels/1/picture -O test.jpg")

end

this still won’t save anywhere on the pi that i can tell. If I put:

wget http://admin:password@10.0.0.61/Streaming/Channels/1/picture

into mobaxterm it will run and will download a photo, but i can’t seem to save anything when running it from openHab.

I came across this post when researching how to make this work with my Hikvision camera. I think I have a basic solution started that may work for you. I leveraged the avconv utility to grab an image from the rtsp stream.

First install the avconv utility:

sudo apt-get install lib-avtools

Next create a rule to grab a snapshot and e-mail a picture. I used a trigger of a motion detector detecting motion:

rule "SendCameraPic"
when
   Item FrontPorchMotion changed to OPEN
then {
   executeCommandLine("/usr/bin/avconv -loglevel fatal -rtsp_transport tcp -i ''rtsp://user:password@ip_address:554/PSIA/streaming/channels/101/'' -r 1 -vframes 1 /tmp/pic.jpg -y", 5000)
   sendMail('emailaddress', "Picture captured from camera", "The attached picture was captured when motion was detected", "file:///tmp/pic.jpg");
}

Watch within the executeCommandLine that the single quotes around the rstp string are escaped (two single quotes, not a double quote).

I just got this working now, so will need to test to see if it is stable. I hope it helps.

Hi again,

now I have tried lots of options with the attachment URL, but none of them worked.
A minor detail however, was the missing

Thread::sleep(5000)

in my rule. I figure that the script returns immediately after executing the commandline but the curl command itself is not finished.

Now with the sleep-command, sending attachments works fine.

Regards
Daniel

Now it works, I have a follow up question.
Do you really need the picture in the mail, or is a link to the picture on
dropbox also fine?

I’m asking because I don’t think e-mail is the place to store images…

yves

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