Thursday, October 01, 2015

Capturing an Image from a Security Camera Using Vera and Emailing It

I wanted to get my VeraLite home automation gateway to grab a frame from a security camera and email it to me whenever the front door opening scene runs.  Without going into extreme detail, I found that I needed to use another computer (an old Thinkpad with Ubuntu) to do the heavy lifting.  The process is as follows:

1) Enable SSH access on the Vera.  You need to enable remote access and make sure secure my vera is disabled and then SSH to remote@your-vera-local-ip-here with the second, six digit number that appears when you enable remote access under the Vera's Settings > Tech Support menu.

2) Setup the Vera to login without a password to your Linux server.  There's a big gotcha here in that YOU login to the Vera using remote, but scenes run under a different user so the keystore needs to be linked or copied to both accounts.  Here's how to setup the keys: http://forum.micasaverde.com/index.php?topic=11663.0 and here's how to do the linking of the keystore: http://forum.micasaverde.com/index.php?topic=11663.15

3) Create a shell script to call a script on your Linux server.  Avoid trying to make SSH calls within the scene's Luup code -- it's a nightmare of semicolons and escape characters.  For example, my verafrontdoor.sh script looks like: 
#!/bin/sh
echo "Started" > /root/frontdoor.log
ssh -i /root/.ssh/id_dss -l yourusername YOUR_LINUX_SERVER_IP '~/capturefrontdoor.sh' >> /root/frontdoor.log 2>&1
echo "Finished" >> /root/frontdoor.log


Don't forget to chmod +x verafrontdoor.sh so it's executable

4) Go into the Vera web panel and edit your scene.  The last window allows you to put custom Luup code.  GOTCHA: this Luup code has to execute within 30 seconds unless you use a workaround.  Capturing and emailing the picture takes longer than that for me possibly, so the workaround is to use call_delay as below:
function callssh()
os.execute(‘/root/verafrontdoor.sh’)
end
luup.log("Calling SSH function")
luup.call_delay("callssh", 1)
luup.log("ssh call done")

5) Now on the Linux server side, create capturefrontdoor.sh.  You will need to Google around to figure out if your camera supports grabbing a JPEG via a simple wget call.  If not, but it supports RTSP streams, find out the RTSP url and try to open it using VLC Player.  If that works, install FFMpeg (or, on Ubuntu 12, download an FFMpeg static build since FFMpeg is missing from the repos) and grab a frame this way.  Here's the call:
./ffmpeg -rtsp_transport tcp -i rtsp://user:password@CAMERA_IP:CAMERA_RTSP_PORT/path/to/stream -y -f image2 -qscale 0 -frames 1 -analyzeduration 5 frontdoor.jpeg

QScale keeps the quality the same, -f image2 saves as a JPEG, -rtsp_transport_tcp ensures packets come back in the correct order (default is UDP which can be unordered, causing a garbled image), and analyzeduration means FFMpeg grabs several seconds of video before making the grab.  You'll have to tune it for your camera.

6) Rather than properly setup sendmail, I wanted to use Gmail.  I installed mailx using apt-get but kept getting errors that Gmail was blocking potentially malicious access to my email account.  You need to turn on access for "less secure" apps. See https://www.drupal.org/node/2080923

For setting up mailx to use Gmail, see http://www.absolutelytech.com/2010/07/17/howto-configure-msmtp-to-work-with-gmail-on-linux/

7) Complete your Linux side script.  It's handy to pipe the output of ffmpeg to a log file as well.

8) RUN THE SCRIPT ON THE VERA MANUALLY AT LEAST ONCE.

9) Execute your scene and verify it works.  

#homeautomation #diy #securitysystems #linux 

No comments:

Labels

Blog Archive

Contributors