diff --git a/opt/inverter/config.ini b/opt/inverter/config.ini index b3df0b3..63bc8cb 100644 --- a/opt/inverter/config.ini +++ b/opt/inverter/config.ini @@ -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 diff --git a/opt/inverter/create_rrd.php b/opt/inverter/create_rrd.php index 37cc748..5e2f71f 100644 --- a/opt/inverter/create_rrd.php +++ b/opt/inverter/create_rrd.php @@ -1,6 +1,5 @@ -#!/usr/local/bin/php +#!/usr/bin/php 'create %s diff --git a/opt/inverter/functions.php b/opt/inverter/functions.php new file mode 100644 index 0000000..fe6e8f5 --- /dev/null +++ b/opt/inverter/functions.php @@ -0,0 +1,32 @@ + 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; } } @@ -1579,4 +1590,4 @@ while (1) { } -####################################################################### \ No newline at end of file +####################################################################### diff --git a/opt/inverter/pvoutput.php b/opt/inverter/pvoutput.php new file mode 100644 index 0000000..21d2029 --- /dev/null +++ b/opt/inverter/pvoutput.php @@ -0,0 +1,126 @@ +#!/usr/bin/php + 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;*/ \ No newline at end of file