use pvoutput.php to export corrected data

create_rrd.php creates rrd's from old data
removed useless mode fied from inverter rrd
moved shared functions to functions.php
This commit is contained in:
2012-11-27 09:45:09 +01:00
parent e47d24d0fe
commit f9ee047981
5 changed files with 185 additions and 16 deletions

View File

@@ -30,7 +30,7 @@ use_rrdtool = 1 # 0 = NO, 1 = YES to export data to rrdtoo
[secs]
datapoll_freq = 5
pvoutput_freq = 300 # every 5-10mins per your setting in http://pvoutput.org
timeout = 2
timeout = 20
reinit = 10 # -1 = infinite num of times (ie dont die)
#------------------
@@ -47,7 +47,8 @@ other = "/opt/inverter/data" # unix/linux
[scripts]
pvoutput = "perl pvoutput.pl" # to export data to http://pvoutput.org
create_rrd = "perl create_rrd.pl" # to export data to rrdtool for graphing
pvoutput_php = "./pvoutput.php" # to export data to http://pvoutput.org
create_rrd = "./create_rrd.pl" # to export data to rrdtool for graphing
rrdtool_exe_win = "rrdtool" # windows
rrdtool_exe_oth = "/usr/bin/rrdtool" # unix/linux

View File

@@ -1,6 +1,5 @@
#!/usr/local/bin/php
#!/usr/bin/php
<?php
$aRRDFile = 'today.rrd';
$sDataDirectory = 'data';
$aRRDCreate = array(
'inverter.rrd' => 'create %s

View File

@@ -0,0 +1,32 @@
<?php
define('TWILIGHT_FILE', 'data/twilight_%d.csv');
function getHour($sTime = null) {
$iTime = $sTime === null ? time() : strtotime($sTime);
return date('H', $iTime) + date('i', $iTime) / 60;
}
function getTwilight($iYear, $iDay) {
$sTwilightFile = sprintf(TWILIGHT_FILE, $iYear);
if (file_exists($sTwilightFile)) {
$aDays = explode("\n", file_get_contents($sTwilightFile));
if (isset($aDays[$iDay])) {
$aDay = explode(',', $aDays[$iDay]);
if ($aDay[0] == $iDay) {
return $aDay;
}
}
}
return null;
}
function command($sCommand) {
ob_start();
system($sCommand);
return ob_get_clean();
}
function clean() {
clearstatcache();
gc_collect_cycles();
}

View File

@@ -30,7 +30,7 @@
# + added %HoH, %HASH
# + added writeToPort() & added warnings to readbuf()
# + edited calc of sleep so dont have to keep DATAPOLL_FREQ_SECS set to 60.
# + edited pvoutput code so dont have to keep DATAPOLL_FREQ_SECS set to 60.
# + edited code so dont have to keep DATAPOLL_FREQ_SECS set to 60.
# + added DESCR to $HoH hash of hashes & used in parseDataFmt()
# + added check if ( $seconds > 0 ) before 'sleep $seconds'
# + added DESCR to parseData() & replaced die with warning in writeToFile()
@@ -99,6 +99,7 @@ $config->define( "secs_reinit=s" );
$config->define( "paths_windows=s" );
$config->define( "paths_other=s" );
$config->define( "scripts_pvoutput=s" );
$config->define( "scripts_pvoutput_php=s");
$config->define( "scripts_create_rrd=s" );
$config->define( "scripts_rrdtool_exe_win=s" );
$config->define( "scripts_rrdtool_exe_oth=s" );
@@ -979,8 +980,9 @@ sub getErrFileName {
#
# Return RRD File Name: [path]/inverter_[serial#].rrd
#
sub getRrdFileName {
sub getRrdFileName(@) {
my $rrdfile = "";
my $suffix = shift;
#
# set path
@@ -995,7 +997,7 @@ sub getRrdFileName {
#
# append filename
#
$rrdfile .= "/inverter_" . $HASH{SERIAL} . ".rrd";
$rrdfile .= "/inverter_" . $HASH{SERIAL} . (defined $suffix ? ("_" . $suffix) : "") . ".rrd";
return $rrdfile;
}
@@ -1454,7 +1456,7 @@ sub writeToFile() {
if ($config->flags_use_rrdtool) {
my $rrdexe = $config->scripts_rrdtool_exe_oth; # Unix/Linux/other
if ($^O eq 'MSWin32') { # Win32 (ActivePerl)
if ($^O eq 'MSWin32') { # Win32 (ActivePerl)
$rrdexe = $config->scripts_rrdtool_exe_win;
}
@@ -1477,11 +1479,18 @@ sub writeToFile() {
"$HoH{FAC}{VALUE}:" .
"$HoH{PAC}{VALUE}:" .
"$etotal:" .
"$htotal:" .
"$HoH{MODE}{VALUE}:" .
"$HoH{ETODAY}{VALUE}";
print "Ran: $rrdexe update $rrdfile $rrdLine\n";
system( "$rrdexe update $rrdfile $rrdLine" );
$rrdfile = getRrdFileName("today");
if (-e $rrdfile) {
$rrdLine = "$unixTimeStamp:" .
"$HoH{PAC}{VALUE}:" .
"$HoH{ETODAY}{VALUE}";
print "Ran: $rrdexe update $rrdfile $rrdLine\n";
system( "$rrdexe update $rrdfile $rrdLine" );
}
}
}
@@ -1559,11 +1568,13 @@ while (1) {
if ($config->flags_use_pvoutput) {
$nextPvoutputTime = $lastPvoutputTime + $config->secs_pvoutput_freq;
if ( $lastPvoutputTime == 0 || $nextPvoutputTime <= time ) {
my $date = getDate_YYYYMMDD(time);
my $time = getTime_HHMM(time);
print "PVOUTPUT as at " . getDateTime(time) . " ...\n";
print " ran: " . $config->scripts_pvoutput . " " . ($HoH{ETODAY}{VALUE} * 1000) . "$HoH{PAC}{VALUE} $HoH{VAC}{VALUE} $date $time $HASH{SERIAL}\n";
system ($config->scripts_pvoutput . " " . ($HoH{ETODAY}{VALUE} * 1000) . " $HoH{PAC}{VALUE} $HoH{VAC}{VALUE} $date $time $HASH{SERIAL}" );
#my $date = getDate_YYYYMMDD(time);
#my $time = getTime_HHMM(time);
#print "PVOUTPUT as at " . getDateTime(time) . " ...\n";
#print " ran: " . $config->scripts_pvoutput . " " . ($HoH{ETODAY}{VALUE} * 1000) . " $HoH{PAC}{VALUE} $HoH{VAC}{VALUE} $date $time $HASH{SERIAL}\n";
#system ($config->scripts_pvoutput . " " . ($HoH{ETODAY}{VALUE} * 1000) . " $HoH{PAC}{VALUE} $HoH{VAC}{VALUE} $date $time $HASH{SERIAL}" );
print " ran: " . $config->scripts_pvoutput_php . " $HoH{ETODAY}{VALUE} $HoH{PAC}{VALUE} $HoH{VAC}{VALUE} $HASH{SERIAL}\n";
system ($config->scripts_pvoutput_php . " $HoH{ETODAY}{VALUE} $HoH{PAC}{VALUE} $HoH{VAC}{VALUE} $HASH{SERIAL}");
$lastPvoutputTime = time;
}
}

126
opt/inverter/pvoutput.php Normal file
View File

@@ -0,0 +1,126 @@
#!/usr/bin/php
<?php
require_once 'functions.php';
define('RRD_FILE', 'data/inverter_%s_today.rrd');
define('RRD_FETCH', 'rrdtool fetch %s AVERAGE -r %d -s %d -e %d');
define('PVOUTPUT_URL', 'http://pvoutput.org/service/r1/addstatus.jsp');
define('TODAY_FILE', 'data/today_%s.csv');
define('FIELD', 'PAC');
define('RESOLUTION', 5);
define('MARGIN', 0.1);
$aSystems = array(
'1204DQ0116' => array('16e7a916d69656e354d00461a4da1d2e40cfa4f1', '12419')
);
/* Fetch command line arguments */
$fToday = floatval($argv[1]);
$fPower = floatval($argv[2]);
$fVoltage = floatval($argv[3]);
$sSerial = $argv[4];
/* Fetch twilight data */
$iDay = date('z');
$aTwilight = getTwilight(date('Y'), $iDay);
/* Fetch today data */
$sTodayFile = sprintf(TODAY_FILE, $sSerial);
$aToday = array();
if (file_exists($sTodayFile)) {
$aToday = explode(',', file_get_contents($sTodayFile));
}
if (count($aToday) != 3 || $aToday[0] != $iDay) {
$aToday = array($iDay, 0, strtotime($aTwilight[1]));
}
$iLast = $aToday[2];
/* Extract fields */
$iTime = time();
$sData = command(sprintf(RRD_FETCH, sprintf(RRD_FILE, $sSerial), RESOLUTION, $iLast, $iTime));
$aData = explode("\n", trim($sData));
$aFields = preg_split("~[\s]+~", array_shift($aData));
array_shift($aData);
$aFields = array_flip($aFields);
/* Process data */
$bFirst = true;
$fEnergy = 0;
if (isset($aFields[FIELD])) {
$iField = $aFields[FIELD] + 1;
array_shift($aData);
foreach ($aData as $sRow) {
$aRow = explode(' ', $sRow);
$iDate = substr($aRow[0], 0, -1);
$iInterval = $bFirst ? (($bFirst = false) || RESOLUTION) : $iDate - $iLast;
if (($fValue = floatval($aRow[$iField])) > 0) {
$fEnergy += $iInterval * $fValue;
}
$iLast = $iDate;
}
}
/* Store today data */
$aToday[1] += $fEnergy / 1000 / 3600;
$aToday[2] = $iTime;
file_put_contents($sTodayFile, implode(',', $aToday));
/* Test */
file_put_contents('test', sprintf("[%s],%d,%s,%f,%f,%f,%f\n", date('r'), time(), $sSerial, $fToday, $fPower, $aToday[1], $fVoltage), FILE_APPEND);
/* Correct today data */
$fToday = $aToday[1] > ((1 + MARGIN) * $fEnergy) ? $fEnergy : $aToday[1];
/* Send data to PVOutput */
if (isset($aSystems[$sSerial])) {
$rCurl = curl_init();
curl_setopt_array($rCurl, array(
CURLOPT_URL => PVOUTPUT_URL,
CURLOPT_HTTPHEADER => array(
sprintf('X-Pvoutput-Apikey: %s', $aSystems[$sSerial][0]),
sprintf('X-Pvoutput-SystemId: %s', $aSystems[$sSerial][1])),
CURLOPT_POSTFIELDS => http_build_query(array(
'd' => date('Ymd', $iTime),
't' => date('H:i', $iTime),
'v1' => 1000 * $fToday, // Wh
'v2' => $fPower,
'v6' => $fVoltage)),
CURLOPT_RETURNTRANSFER => true));
$sResult = curl_exec($rCurl);
file_put_contents('pvtest', sprintf("[%s] %s\n", date('r'), $sResult), FILE_APPEND);
}
/*
$a = file_get_contents('test');
$b = explode("\n", $a);
array_pop($b);
foreach ($b as $c) {
$d = explode(',', $c);
$iTime = $d[2];
$fToday = $d[4];
$fEnergy = $d[6];
$fToday = $fToday > ((1 + MARGIN) * $fEnergy) ? $fEnergy : $fToday;
$fPower = $d[5];
$sSerial = $d[3];
$e = sprintf("%s,%s,%s\n", date('H:i', $iTime), $fToday * 1000, $fPower);
file_put_contents('do.csv', $e, FILE_APPEND);
/*
$rCurl = curl_init();
curl_setopt_array($rCurl, array(
CURLOPT_URL => PVOUTPUT_URL,
CURLOPT_HTTPHEADER => array(
sprintf('X-Pvoutput-Apikey: %s', $aSystems[$sSerial][0]),
sprintf('X-Pvoutput-SystemId: %s', $aSystems[$sSerial][1])),
CURLOPT_POSTFIELDS => http_build_query(array(
'd' => date('Ymd', $iTime),
't' => date('H:i', $iTime),
'v1' => 1000 * $fToday, // Wh
'v2' => $fPower)),
CURLOPT_RETURNTRANSFER => true));
echo $sResult = curl_exec($rCurl);
sleep(61);*
}
exit;*/