IP-Cam Light Sensor with Vera
this Info-Page is part of my private Home-Automation Project, for more details Click Here!

the idea is quite simple:
a IP Camera Captures the light in order to take a Picture, so we simply “Count” the average brightness of a selected area to find out how “Bright” the image is.

example

 


Find a good spot

using the Complete “view” if the Camera may be get false data .. as the picture always capture all surrounding light, so the key is to find a Spot which does only change with ambient light.


i used the walkway opposite my House which is a 76x16 big piece of the image located at pixel y:377 x:78
(you can just use any image edit app to find the coordinates, they will be needed later so write them down)


with the help of ImageMagick we can just Crop a partition out of the image, in order to get more acurate data.
then messure the average Brightness of the cropped image.
a small shell-script just need to: download the image, crop it, messure the brightness and deliver the result to the Vera.
but more about that later ...


Prep the Vera

first the Vera needs a “Light Sensor Device” added, this can be done easy as the configuration does come with the Vera.
just go to APPS->Develop Apps->Create device
put in a Name of your choice in “Description”
and “D_LightSensor1.xml” in the field “Upnp Device Filename”
(you can optional choose a "room", all other fields do not require any data, simply click Create. [example here]

this will create a Light Sensor Device on your Vera. (as you can see on the top), Reload, click to the edit icon and navigate to the Advanced Tab, write down the number behind "Device #" (im my case this its 205, we will need it later in the Linux part)

the Linux part

first make sure curl and imagemagick is installed by executing
sudo apt-get install imagemagick curl

then we need to place the script to do the actual work somewhere (i placed it in /home/pi/)


#!/bin/bash
VERA=172.16.67.6
CAMID=100
SCANAREA=76x16+377+78

SENSORDEVICE=205
SENSORSERVICE=urn:micasaverde-com:serviceId:LightSensor1
SENSORVARIABLE=CurrentLevel

curl -o /tmp/cam$CAMID.jpg 'http://$VERA:3480/data_request?id=request_image&cam=$CAMID&rand=0.1' > /dev/null 2>&1

# crop a static part of the image to prevent noise
convert /tmp/cam$CAMID.jpg -crop $SCANAREA /tmp/cam$CAMID_scan.jpg
T=$(convert /tmp/cam$CAMID_scan.jpg -format "%[mean]" info: )
A=${T%.*}

#build url to publish data
URL="http://$VERA:3480/data_request?id=variableset&DeviceNum=$SENSORDEVICE&serviceId=$SENSORSERVICE&Variable=$SENSORVARIABLE&Value="

#fallback if cera delivers "no image picture"
if [ $A -le 58000 ]; then
	curl -G "$URL$A" & > /dev/null

	echo "Level: $A"
else
	echo "Skipping: $A"
fi
change the IP and Device ID and you good to go. this script needs to be executed regulary … so better add a Cron-job to accomplish that like this:
*/5 * * * * /location/to/your/script/camillu.sh &

this will execute the script every 5 minutes and update the Brightness value accordingly.

on the Vera you can just add simple triggers to do things if the light levels do change.


Analytics example: