Sunday, August 28, 2011

webcam and motion based power meter data sender

Instead of doing anything to do with actual hardware-hacking, I decided to use a webcam to read the infrared blips from my electric power meter.

I placed the webcam on top of the meter (a better, weatherproof, mounting system is still needed).
Taped it in place, placed some addition material above it to shield it from background light.

I then used the open source package 'motion' and basically don't use it's snapshot or movie taking modes at all.
I use it strictly to trigger my data logger script:

motion.conf useful bits:


width 176
height 144
framerate 30
gap 1
output_normal off   #this disables picture taking
on_event_start /usr/local/bin/on_event_start
ppm on # speeds things up
webcam_maxrate 30  # we can watch it in realtime, but only feasible using ppm
threshold 50
noise_level 32
auto_brightness off
brightness 200
contrast 250


on_event_start looks like:

#!/bin/sh
DATE=$(date -u +%Y-%m-%dT%H:%M:%S)
FILE=$(echo $DATE | cut -f1-2 -d:)
SECONDS=$(echo $DATE | cut -f3 -d:)
echo $SECONDS >> /tmp/${FILE} 

and I push these files via wattvision's public API with this script:

#!/bin/sh



# Check if motion died, if it did, restart and log it
MOTION_EXEC=$(ls -l /proc/$(cat /tmp/motion.pid)/exe 2>/dev/null | cut -f2 -d\>)
[ x${MOTION_EXEC} != x"/usr/bin/motion" ] && (
  /usr/bin/motion
  date >> /root/motion.restart.log
  )

# this will skip the last matching file, since it's not yet complete:
PENDING_LIST=$(echo /tmp/????-??-??T??:?? | tr ' ' '\n' | tac | tail +2 | tac)

for crumb in ${PENDING_LIST} ; do
  echo -n processing ${crumb}==
  COUNT=0
  COUNT="$(grep -c . ${crumb})"
  AVG_DT=$(( 60 / ${COUNT} ))
#  INST_WATTS=$(( 3600 / $AVG_DT ))
#  echo -n instant $INST_WATTS
  MS_PER_WH=$(( 60000 / ${COUNT} )) # ( 1minute in ms / pulses last minute )
  echo ms_per_wh $MS_PER_WH

  POST_DATA1="h=<house_id>&k=<secret_api_key>&v=0.1"
  POST_DATA2="time=$(basename $crumb):00"
  POST_DATA3="ms_per_wh=${MS_PER_WH}"

  POST_DATA="${POST_DATA1}&${POST_DATA2}&${POST_DATA3}"
  
  wget -q -O - --post-data="${POST_DATA}" http://www.wattvision.com/api
  code=$?
  echo return code: $code
  
  [ $code -eq 0 ] && rm $crumb
  
  sleep 15
  
done

and I run the script every minute from cron.


Next up:  an every-15 second reporting mode.

No comments:

Post a Comment

#RSFtalks with Edward Snowden

What an intelligent, thoughtful individual. I find it difficult to forgive 44 for failing to pardon this patriot and instead pursuing him ...

Other Popular Posts: