Sending image from Hikvision camera with mail binding

Hey all,

Specs: Openhab 1.8.1 on Ras pi.

I’ve been able to display the snapshot and video on my sitemap from my Hikvision DS-2CD2332-I What i want to do is add a button which takes a snapshot and sends it to my e-mail. I’ve actually got the button and the mail binding work, but i can’t get it to extract the photo.

I learned from a thread (that i can no longer find) that you cant use user:password in the mail binding, but apparently you can grab the snapshot and send it via e-mail some other way using the HTTP binding, but I can’t figure out how.

Sitemap:

      Frame label="Cameras" {
        Text label="Backyard" icon="camera" {
           Image label="Camera" url="http://admin:password@10.0.0.61/Streaming/channels/1/picture"
           Image label="Video" url="http://admin:password@10.0.0.61/Streaming/channels/1/preview"
		}

If someone can help me grab the snapshot from the camera and send it via e-mail i would greatly appreciate it.

Here is an screengrab of the camera. It is awesome and I highly recommend them:

3 Likes

got the same problem with Samsung camera in Sitemaps, I have the pictures, but that doesn’t work as an attachment in email
sendMail("kalender@gmail.com",“Post”,“Post ist da”,“http://user:pass@selfhost.eu:58081/cgi-bin/video.cgi?msubmenu=jpg”)
that does not so it isn’t
with telegram works but
sendTelegramPhoto(“Openhabbot”,“http://www.selfhost.eu:58081/cgi-bin/video.cgi?msubmenu=jpg”,null, “user”, “pass”)
is that a bug in email Binding?

1 Like

If I remember it right, you have to grab the picture through exec binding (curl …), save the picture in your local file system (./webapps/static/*) and then send the mail with this local copy.

how times does that?how is the rules ?

Take a look at this thread: How to grab pictures from IP camera and send them to dropbox?

if I do so I get email while there but empty pic.jpg as an attachment here

val url = “http://user:pass@.selfhost.eu:58081/cgi-bin/video.cgi?msubmenu=jpg"
val outputFile = “/opt/openhab/webapps/static/pic” +”.jpg"
var cmd = “curl -m 10 -o " + outputFile + " " + url
executeCommandLine(cmd)
sendMail("openhab@gmail.com”, “Klingel”,"Klinge Door: ",“file://” + outputFile )
var Timer timer
timer = createTimer(now.plusSeconds(60)) [|
var cmddelete = “rm” + " " + outputFile
executeCommandLine(cmddelete) ]

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