How to grab pictures from IP camera and send them to dropbox?

I would like to be able to grab snapshots of IP cameras based on rules and send them to dropbox. So far, I know I can send an email with a snapshot from a camera but don’t know how to send several images, say 3 images, 3 seconds apart, and be able to upload them to dropbox. Is this possible?

Thanks

I don’t know about the IP cameras but there is a Dropbox binding you should be able to use. It looks like it mainly sets up a sync between your openHAB and Dropbox but you should be able to drop the pictures in the sync folder and based on how frequently the folder is synced the pictures will be uploaded to Dropbox.

Actually having them appear in Dropbox within 3 seconds may not be doable. I don’t know how fast of a polling period it can support.

Thanks @rlkoshak, I had looked into the dropbox binding and as you say it’s focus is more geared to save config files, but it could be diverted to what I want to do. The frequency is handled by a cron statement in openhab.cfg, so that should be ok. However, I don’t know how I would save the snapshot to the filesystem from within the rule engine. I guess I would also need to do some housekeeping to clean the folder regularly.

It might be something you need to implement outside of openHAB and trigger using the Exec binding. I looked at the HTTP actions and I think all you can get out of those is a String, not a binary so I think it will be easier to use Python, Bash, or your scripting language of choice to grab the picture.

I think it should work to executeCommandLine calling curl with the -o parameter to write its output to a sync folder.

Untested:

rule SnapPhoto
when
  Item MotionDetected changed to ON
then
  val url = "http://user@pass:1.2.3.4/snap.cgi"
  val outputFile = "/home/openhab/snaps/" + now.toString +".jpg"
  var cmd = "curl -m 10 -o " + outputFile + " " + url
  executeCommandLine(cmd)
end
1 Like

Thanks @watou, will test it and report back.

1 Like

@watou, your code works, although I need to change the now.toString in the filename because it will throw some characters that will not allow to open the file (at least) in mac OS X. I’ve used a bunch of chained now.getYear.toString … arguments to set the date of the file but it’s not very elegant. I’ve been trying to use DateTimeFormatter, but running into some problems. I’m also having trouble with the Dropbox binding as it does scan the directory but sees no files to upload, still investigating it (I wonder if it’s not hard coded to search for /configurations/, and the other default folders). So when I get everything working, I will update the post. Anyhow, thanks a lot to you and @rlkoshak.

Update: I was able to get dropbox working, however, I thought the LOCAL_TO_DROPBOX setting would not synchronize. However, when a file is deleted in the local folder it ends up deleted on dropbox too. This is not the behavior I want.

Oh yes, I forgot about that! Maybe changing outFile to:

val outputFile = "/home/openhab/snaps/" + now.toString.replaceAll("[:\\/ ]+", "_") +".jpg"

Also not tested, but the idea is to change all illegal file name characters to _ .

An alternative @watou solution (though his is probably lighter weight) is to use a DateTimeFormatter.

I’ve been trying to help someone on a different threadto format a DateTime String. The Joda API does have DateFormat and DateFormatter classes which are supposed to do this but I couldn’t get them to work. Java’s SimpleDateFormat should work for you though. I don’t think you will be in a situation where multiple threads will be calling it.

Anyway, with a SimpleDateFormat you can specify a way to print the DateTime without the special characters. In fact, you could configure it to generate your entire filename I think.

It is always nice to have options.

1 Like

Have you considered using myOpenHAB and IFTTT?

And why not install the Dropbox client on your OH server and simply write your images to the Dropbox folder?

That’s a good idea @digineut, however, I’m on a synology, and I just tested their app for dropbox (Cloud Sync), it also has the same behavior. I will try to modify the code for the dropbox binding or stick to the current behavior, which after all is not that bad, being on a NAS with plenty of HD space.

How would you use IFTTT for this case?

I don’t know if this will work on Synology NAS but I am doing something very similar. I have OpenHAB running on Intel Nuc with Ubuntu. I used the Ubuntu’s Dropbox client to mount the Dropbox folder and shell script to: create folder with the days date, grab 5 snapshots with 2 second interval with a date timestamp. I then the event to NMA with the URL for the snapshots. Maybe you can use pieces of it for your config.

rules/security.rules:

    rule "Basement Motion"
when
        Item MSensor_Basement_Sensor changed from CLOSED to OPEN
then
	if((Security_Switch.state==ON) && (Security_Profile.state==2)) {
		executeCommandLine("/usr/local/bin/motion_snapshot.sh")
		notifyMyAndroid("Security Alert", "Motion detected in the basement, Snapshots stored in https://<drop-location>", 2)
		logInfo("Security", "Security Alert, Motion detected in the basement")
		//playSound("Security_Siren.mp3")
                }
end

/usr/local/bin/motion_snapshot.sh:

#!/bin/bash

## Created by:	Blaze Bryant
## Date:	August 11, 2014
## Server:	nb-openhab

## Variables ##
DROPPATH=<path-to-dropbox-folder
MOTIONDIR=$DROPPATH/Motion-Snapshots-`date +"%Y%m%d"`
TMPOUT=/tmp/vidcam-snapshots.out

## Put timestamp on out file
echo "Script ran: `date +"%H:%M:%S %m-%d-%Y"` " >> $TMPOUT

## Check if folder exists with todays date
if [ ! -d $MOTIONDIR ]; then
	mkdir -p $MOTIONDIR
	chmod 775 $MOTIONDIR
fi


# Take 5 snapshot pictures from Video Camera
curl -o $MOTIONDIR/`date +"%Y%m%d-%H%M%S"`.jpg --retry 1 http://<snapshot-url>/snapshot.jpg 2>> $TMPOUT
sleep 2
curl -o $MOTIONDIR/`date +"%Y%m%d-%H%M%S"`.jpg --retry 1 http://<snapshot-url>/snapshot.jpg 2>> $TMPOUT
sleep 2
curl -o $MOTIONDIR/`date +"%Y%m%d-%H%M%S"`.jpg --retry 1 http://<snapshot-url>/snapshot.jpg 2>> $TMPOUT
sleep 2
curl -o $MOTIONDIR/`date +"%Y%m%d-%H%M%S"`.jpg --retry 1 http://<snapshot-url>/snapshot.jpg 2>> $TMPOUT
sleep 2
curl -o $MOTIONDIR/`date +"%Y%m%d-%H%M%S"`.jpg --retry 1 http://<snapshot-url>/snapshot.jpg 2>> $TMPOUT
sleep 2

echo "" >> $TMPOUT
echo "" >> $TMPOUT

## Exit Script
exit 0
2 Likes

Hi, what about using some software to get the picture/video from multiple IP-cameras? I’m using (Motion http://lavrsen.dk/foswiki/bin/view/Motion/WebHome)
This software will create a unique url for both snapshots (images) and video from any of your cameras. Then you can just add this url in your sitemap.
It has also built in motion-detection, which lets you configure it to capture video/picture and notify you when motion is detected.
I’m currently using MotionEye for the GUI and have added something like this to my sitemap:

Text label="Livestream" {
                    Image url="http://{{garage_camera_ip}}/image/jpeg.cgi"
                    Image url="{{garagepi_screenshot_url}}"
                }

Thanks @naturalblaze, that’s great and it should work on synology, the only thing is the dropbox client which cannot be configured so that it will keep the remote files if you delete the local ones.

I use zoneminder, but haven’t got yet to integrating it with openhab. Thanks.

Thanks for all the tips and the help. Just to share this is my solution for my Tenvis 319W-HD cam, which does not support JPG, so it has to be pulled from the stream. It also cleans up the directory after the picture is sent.

{
val url = “http://login:pass@ipaddress:port/media/?action=snapshot&size=640:480"
val outputFile = “/home/pi/opt/openhab/snaps/” + now.toString.replaceAll(”[:\/ ]+", “_”) +".jpg"
var cmd = “curl -m 10 -o " + outputFile + " " + url
executeCommandLine(cmd)
sendMail("myemail@gmail.com”, “OPENHAB@home: Kitchen Door”, "Kitchen Door opened at: " + KitchenDoorTime.state, “file://” + outputFile )
var Timer timer
timer = createTimer(now.plusSeconds(60)) [|
var cmddelete = “rm” + " " + outputFile
executeCommandLine(cmddelete) ]
}

The timer is needed to put some delay on, otherwise it deletes the file too quickly and it won’t come through in the email.
Thanks again!