TIP: Sending ONVIF snapshot pictures via e-mail on a Vera trigger

Just created this script that talks to my ONVIF-based camera, since there’s no JPEG URL or anything similar to integrate it to Vera. I hope it helps someone, because it was really tricky to get this one working. Calling the ONVIF web sevice to get the actual image is not well documented.

Please notice that I’m using a free SMTP server that doesn’t require authentication.

*** This script wil NOT work with Gmail as the SMTP server ***

local cameraIP = "192.168.2.53"
local ltn12 = require("ltn12")
local mime = require("mime")
local smtp = require("socket.smtp")
local http = require("socket.http")
local soapContent = '<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"><GetSnapshotUri xmlns="http://www.onvif.org/ver10/media/wsdl"><ProfileToken>720p media profile</ProfileToken></GetSnapshotUri></s:Body></s:Envelope>'

local r, c, h = http.request{
	url = "http://" .. cameraIP .. ":10080/onvif/media_service",
	method = "POST",
	source = ltn12.source.string(soapContent),
	headers = {
		["action"] = "http://www.onvif.org/ver10/media/wsdl/GetSnapshotUri",
		["content-type"] = "application/soap+xml",
		["content-length"] = string.len(soapContent)
	}
}

local result, content = luup.inet.wget("http://" .. cameraIP .. "/snaponvif.jpg")

local attachment = ltn12.source.chain(
  ltn12.source.string(content),
  ltn12.filter.chain(
    mime.encode("base64"),
    mime.wrap()
  )
)

local source = smtp.message({
  headers = {
     from = "Vera <vera@gmail.com>",
     to = "Vinicius <myuser@gmail.com>",
     subject = "Izabela chegou em casa"
  },
  body = {
    preamble = "Helloi!",
    [1] = { 
      headers = {
        ["content-type"] = 'image/jpeg; name="image.jpg"',
        ["content-disposition"] = 'attachment; filename="image.jpg"',
        ["content-description"] = 'Camera picture',
        ["content-transfer-encoding"] = "BASE64"
      },
      body = attachment
    }
  }
})

r, e = smtp.send{
  from = "vera@gmail.com",
  rcpt = "myuser@gmail.com",
  source = source,
  server = "smtpcorp.com",
  port = 2525
}

Hi,

great tip. But what brand is your cam? It seems several cams have sevaral ONVIF implementations… And to be honest with your code I dont know where to start :slight_smile: