okay slept on it and it came to me in the middle of the night. Now it error corrects if the data sets become out of sync on the second failed set. now it produce graphs with out any data holes.. i t even seams to keep reading in conditions the ted device fails to read.. ( it just re sync more often in those conditions)
script name
ted1a.pl
#!/usr/bin/perl -w -s
system("stty -F /dev/ttyUSB0 1200 cs8 raw");
open INFILE, "/dev/ttyUSB0"
or die "\nCannot open /dev/ttyUSB0!\n";
@data = ();
$buf = "";
$begin = 0;
$started = 0;
sub processPacket($);
while (read(INFILE, $buf, 1)) {
$d = ord($buf) ^ 0xff;
if ($d == 0x55 && $started == 0) {
$started = 1;
$a = 0;
}
if ($started == 1) {
$data[$a] = $d;
$sum += $data[$a];
# printf("0x%02X ", $data[$a]);
$a++;
if ($a >= 11) {
$sum -= $data[9];
$sum &= 0xff;
if ($sum == 0) { processPacket($data); }
$started=1;
$a = 0;
}
}
if ( $begin <= 22) {
$cnt = ($begin + 1);
$begin = $cnt;}
print "$begin bit ";
if ($begin >= 23) {close INFILE;}
}
# Working sub process packet stuff
sub processPacket($) {
local($data) = @_;
# your personal setup info
# $dir= ted data folder - $ted = name of ted rrd file
$dir = '/home/user/';
$ted = 'ted';
$ted1 = 'ted1';
$ted2 = 'ted2';
$mtu = $data[1];
# the $mtu equals your mtu number put in a Decimal to Hexadecimal Converter )
if ($begin <= 22) {
$begin = 1;
if ($mtu == 0xCE ) {
# If the power reading is way off, uncomment the other power line
$p = (($data[5]^0xff)<<8) + ($data[4]^0xff);
# print "$p ";
$pa = ($data[5]<<8) + $data[4];
# print "$pa ";
if ($p <= 5000) {
$voltage = ($data[8] << 8) | $data[7];
$voltage = sprintf("%.3f",123.6 + ($voltage - 27620) / 85 * 0.2);
$p = sprintf("%.3f",(1.19 + 0.84 * (($p - 288.0) / 204.0)));
@tda = localtime(time);
$td = ($tda[4]+1)."/$tda[3]/".(1900+$tda[5])." $tda[2]:$tda[1]:$tda[0]";
print "$td, Hydro $p, Voltage $voltage\n";
system("rrdtool updatev $dir/$ted.rrd N:$p:$voltage");
$begin = 0;
}
if ($pa <= 5000) {
# $power = ($data[5]<<8) + $data[4];
$voltage = ($data[8] << 8) | $data[7];
$voltage = sprintf("%.3f",123.6 + ($voltage - 27620) / 85 * 0.2);
$pa = sprintf("%.3f",(0 - (1.19 + 0.84 * (($pa - 288.0) / 204.0))));
@tda = localtime(time);
$td = ($tda[4]+1)."/$tda[3]/".(1900+$tda[5])." $tda[2]:$tda[1]:$tda[0]";
print "$td, Hydro $pa, Voltage $voltage\n";
system("rrdtool updatev $dir/$ted.rrd N:$pa:$voltage");
$begin = 0;
}
}
if ($mtu == 0xA0 ) {
# If the power reading is way off, uncomment the other power line
$w = (($data[5]^0xff)<<8) + ($data[4]^0xff);
# print "$w ";
$wa = ($data[5]<<8) + $data[4];
# print "$wa ";
if ($w <= 5000) {
$voltage1 = ($data[8] << 8) | $data[7];
$voltage1 = sprintf("%.3f",123.6 + ($voltage1 - 27620) / 85 * 0.2);
$w = sprintf("%.3f",(1.19 + 0.84 * (($w - 288.0) / 204.0)));
@tda = localtime(time);
$td = ($tda[4]+1)."/$tda[3]/".(1900+$tda[5])." $tda[2]:$tda[1]:$tda[0]";
print "$td, Wind $w, Voltage $voltage1\n";
system("rrdtool updatev $dir/$ted2.rrd N:$w:$voltage1");
$begin = 0;
}
if ($wa <= 5000) {
# $power = ($data[5]<<8) + $data[4];
$voltage1 = ($data[8] << 8) | $data[7];
$voltage1 = sprintf("%.3f",123.6 + ($voltage1 - 27620) / 85 * 0.2);
$wa = sprintf("%.3f",(0 - (1.19 + 0.84 * (($wa - 288.0) / 204.0))));
@tda = localtime(time);
$td = ($tda[4]+1)."/$tda[3]/".(1900+$tda[5])." $tda[2]:$tda[1]:$tda[0]";
print "$td, Wind $wa, Voltage $voltage1\n";
system("rrdtool updatev $dir/$ted2.rrd N:$wa:$voltage1");
$begin = 0;
}
}
if ($mtu == 0xB1 ) {
# If the power reading is way off, uncomment the other power line
$s = (($data[5]^0xff)<<8) + ($data[4]^0xff);
# print "$s ";
$sa = ($data[5]<<8) + $data[4];
# print "$sa ";
if ($s <= 5000) {
$voltage2 = ($data[8] << 8) | $data[7];
$voltage2 = sprintf("%.3f",123.6 + ($voltage2 - 27620) / 85 * 0.2);
$s = sprintf("%.3f",(1.19 + 0.84 * (($s - 288.0) / 204.0)));
@tda = localtime(time);
$td = ($tda[4]+1)."/$tda[3]/".(1900+$tda[5])." $tda[2]:$tda[1]:$tda[0]";
print "$td, Solar $s, Voltage $voltage2\n";
system("rrdtool updatev $dir/$ted1.rrd N:$s:$voltage2");
$begin = 0;
}
if ($sa <= 5000) {
# $power = ($data[5]<<8) + $data[4];
$voltage2 = ($data[8] << 8) | $data[7];
$voltage2 = sprintf("%.3f",123.6 + ($voltage2 - 27620) / 85 * 0.2);
$sa = sprintf("%.3f",(0 - (1.19 + 0.84 * (($sa - 288.0) / 204.0))));
@tda = localtime(time);
$td = ($tda[4]+1)."/$tda[3]/".(1900+$tda[5])." $tda[2]:$tda[1]:$tda[0]";
print "$td, Solar $sa, Voltage $voltage2\n";
system("rrdtool updatev /$dir/$ted1.rrd N:$sa:$voltage2");
$begin = 0;
}
}
}
else {close INFILE;}
}
close INFILE;
but it is important that you use this program to start your data collection ( just place tedStart.pl and ted1a.pl in /bin of your root directory and set to run at boot in your rc.local or similar
tedStart.pl
#!/usr/bin/perl -W -s
for( ; ; )
{
printf "killing ted process.\n";
system("pkill -f ted1a.pl");
printf "Starting Ted data collection.\n";
sleep(1);
system("ted1a.pl")
}
to create your rrd files just rename ted.rrd > ted1.rrd etc this one keeps 3 months of data
rrdtool create ted.rrd --step 3 DS:kw:GAUGE:5:0:U DS:volt:GAUGE:5:0:U RRA:MIN:0.5:12:2592000 RRA:AVERAGE:0.5:1:2592000 RRA:MAX:0.5:1:2594000
for graph creation just adjust time scale
#!/bin/bash
now=$(date +%s)
now_formatted=$(date +%s | awk '{printf "%s\n", strftime("%c",$1)}' | sed -e 's/:/\\:/g')
/usr/bin/rrdtool graph 10min.png \
-w 543 -h 267 -a PNG \
--slope-mode \
--start -600 --end now \
--title "Power: For the Last 10 minutes" \
--vertical-label "KILOWATTS (KW)" \
DEF:kw=ted.rrd:kw:AVERAGE \
VDEF:Last=kw,LAST \
VDEF:First=kw,FIRST \
VDEF:Min=kw,MINIMUM \
VDEF
eak=kw,MAXIMUM \
VDEF:Average=kw,AVERAGE \
CDEF:kWh=kw,10,/,10,* \
CDEF:Cost=kWh,.0665,* \
LINE1:kw#005199FF:"POWER " \
AREA:kw#00519933:"" \
GPRINT:Last:"%6.4lf%sKW" \
COMMENT:"\\n" \
HRULE:Average#9595FF:"Average" \
GPRINT:kw:AVERAGE:"%6.4lf%sKW" \
COMMENT:"\\n" \
HRULE
eak#ff0000:"Peak " \
GPRINT:kw:MAX:"%6.3lf%sKW" \
COMMENT:"\\n" \
HRULE:Min#58FAF4:"Min " \
GPRINT:kw:MIN:"%6.4lf%sKW" \
COMMENT:"\\n" \
GPRINT:kWh:AVERAGE:" total %6.2lfkWh\\n" \
GPRINT:Cost:AVERAGE:" cost %6.2lf $\\n" \
GPRINT:Cost:AVERAGE:"$(printf \\" cost %11s\\" $%.2lf | sed 's/\$/\$ /g')\\n" \
COMMENT:" \\n" \
GPRINT:First:"Showing from %c\\n":strftime \
GPRINT:Last:" to %c\\n":strftime \
COMMENT:" Created at $now_formatted"
if you want to compare this week to last week
Code:
#!/bin/bash
now=$(date +%s)
now_formatted=$(date +%s | awk '{printf "%s\n", strftime("%c",$1)}' | sed -e 's/:/\\:/g')
# create power graph for last week
/usr/bin/rrdtool graph power-week.png \
--start end-7d --width 543 --height 267 --end $now-1min --slope-mode \
--vertical-label "KiloWatts" --lower-limit 0 \
--alt-autoscale-max \
--title "Power: Last week vs. week before" \
--watermark "(©) $(date +%Y) Alyn R. Tiedtke" \
--font WATERMARK:8 \
DEF:kw=ted.rrd:kw:AVERAGE \
DEF:Power2=ted.rrd:kw:AVERAGE:end=$now-7d1min:start=end-7d \
VDEF:Last=kw,LAST \
VDEF:First=kw,FIRST \
VDEF:Min=kw,MINIMUM \
VDEF:Peak=kw,MAXIMUM \
VDEF:Average=kw,AVERAGE \
CDEF:kWh=kw,10,/,168,* \
CDEF:Cost=kWh,.0665,* \
SHIFT:Power2:604800 \
LINE1:Power2#00CF00FF:"Last Week\\n" \
HRULE:Min#58FAF4:"Min " \
GPRINT:kw:MIN:"%6.4lf%sKW" \
COMMENT:"\\n" \
LINE1:kw#005199FF:"Power " \
AREA:kw#00519933:"" \
GPRINT:Last:"%6.4lf%sKW" \
COMMENT:"\\n" \
HRULE:Average#9595FF:"Average" \
GPRINT:kw:AVERAGE:"%6.4lf%sKW" \
COMMENT:"\\n" \
HRULE:Peak#ff0000:"Peak " \
GPRINT:kw:MAX:"%6.3lf%sKW" \
COMMENT:"\\n" \
GPRINT:kWh:AVERAGE:" total %6.4lfkWh\\n" \
GPRINT:Cost:AVERAGE:" cost %6.2lf $\\n" \
GPRINT:Cost:AVERAGE:"$(printf \\" cost %11s\\" $%.2lf | sed 's/\$/\$ /g')\\n" \
COMMENT:" \\n" \
GPRINT:First:"Showing from %c\\n":strftime \
GPRINT:Last:" to %c\\n":strftime \
COMMENT:" Created at $now_formatted"