PowerMonitor

From Ywiki

Jump to: navigation, search

This project was developped to display the power usage of my home in a pretty graphic.

Contents

Hardware

PIC WEB in the cupboard
PIC WEB in the cupboard

The hardware for this project consists of 2 parts. The PIC WEB mini webserver. And a LDR to measure the pulses created by the power meter.

PIC WEB

The PIC WEB is a development board made by OLIMEX. This is a PIC microcontroller with the ENC28J60 Ethernet controller. This development board has all wanted features build in.

Sensor

The power meter is a new type which has is led build in. This led flashes 1000 times per used kilowatt hour. This knowledge is the base for the powermonitor project. The sensor is a LDR, when the sensor is in the dark the resistance is about 700k ohm. When it is put in bright licht, the resistance drop to about 30 ohms. This resistance drop is enough to trigger an external interrupt on te INT0 port of the microcontroller.

Firmware

The firmware is based on the olimex software. It is compiled with Microcip IDE matlab en Microchips own compiler C18. Please contact me if you are interrested in the firmware.

Software

The PIC WEB is only used to count the pulses the power meter genreates. The most interresting part of the software is ran on my personal webserver. This software contains of multiple scripts, which are started by a cronjob. The first perl script reads the value from the PIC WEB and puts in an RRD. If you dont know what a RRD is please use google. This is the command to create the rrd

rrdtool create power.rrd --start N --step 60 \ 
DS:power:GAUGE:300:0:U \
RRA:AVERAGE:0.5:1:1440 \
RRA:AVERAGE:0.5:60:24 \
RRA:AVERAGE:0.5:1440:365 \

This is the first script grab.pl, it prepares the PIC WEB with a post then it reads the data via a lynx call. The read dat is put into a rrd file.

#!/usr/bin/perl
$result = `lynx -dump -connect_timeout=2 http://192.168.0.13/0?0`;
$result = `lynx -dump -connect_timeout=2 http://192.168.0.13/power.cgi`;
# remove line feeds using regular expressions
$result =~ s/\x0A//g;
#remove the spaces using regular expressions
$result =~ s/\x20//g;
$pulses = $result;
# remove the prefix "pulses:"
$pulses =~ s/pulses://;
# multiply it by 60 to get real power usage
$pulses = $pulses*60;
# request time
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
# write is to the rrd
`rrdtool update /home/hans/powermonitor/power.rrd N:$pulses`;

The following reads the generates from the rrd file a nice graph.

# get the time
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
($seca,$mina,$houra,$mdaya,$mona,$yeara,$wdaya,$ydaya,$isdsta)=localtime(time-3600); 
$year = $year + 1900;
$mon = $mon + 1;
# if hour is 0 then $hourstart = 23
# else it it the previous hour
if ( $hour == 0){
        $file_name = "/var/www/html/pw/$yeara-$mona-$mdaya 23-to-$houra.png";
}else{
        $hourstart = $hour -1;
        $file_name = "/var/www/html/pw/$year-$mon-$mday $hourstart-to-$hour.png";
}

$rrdpath = "/home/hans/powermonitor/power.rrd";

$labelv = "power usage between $hourstart and $hour hour";
$labelt = "power in watts";
# write date to png
`rrdtool graph "$file_name" -a PNG -h 200 -w 500 -l 0 -v "$labelt" -t "$labelv" -s -3900 -e -300 DEF:x="$rrdpath":power:AVERAGE LINE:x#00CC00`;

the result of this scrip is:

PIC WEB in the cupboard