Tue 23 Oct 2012
solar monitoring stuff
Posted by Internat under Bash, Debian, Linux
No Comments
I cant take any credit for this script, I took the orginal from the solar.js.cx page and changed it to meet my requirements.
Here is my version
#!/usr/bin/perl
############################################
## This software is licensed under the LGPL.
## Visit http://solar.js.cx for more info.
############################################
use Device::SerialPort;
use LWP::UserAgent;
use Time::Local;
use HTTP::Status qw(:constants :is status_message);
$debug = 0;
$submit = 1;
$serial_port = "/dev/ttyr00";
$pvoutput_api ="<apikeyhere>";
$pvoutport_systemid = "<systemkeyhere>";
$serial_lock = "/tmp/ttyr00.lock";
## Wait until unlocked
while (-e $serial_lock)
{
sleep (1);
}
$serial_port = new Device::SerialPort ($serial_port, "", $serial_lock);
$serial_port->baudrate(9600) || die "failed setting baudrate";
$serial_port->parity("none") || die "failed setting parity";
$serial_port->databits(8) || die "failed setting databits";
$serial_port->handshake("none") || die "failed setting handshake";
$serial_port->write_settings || die "no settings";
## Might need to tweak this if data is truncated.
$serial_port->read_const_time(40);
$serial_port->write("INV?\r");
($count, $xantrex_status) = $serial_port->read(255);
$serial_port->write("KWHTODAY?\r");
($count, $xantrex_kwhtoday) = $serial_port->read(255);
$serial_port->write("TIME?\r");
($count, $xantrex_time) = $serial_port->read(255);
$serial_port->write("POUT?\r");
($count, $xantrex_pout) = $serial_port->read(255);
$serial_port->close || warn "close failed";
$xantrex_wtoday = $xantrex_kwhtoday * 1000;
#$time = timelocal($sec,$min,$hour,$mday,$mon,$year);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$mon += 1;
$date = sprintf "%04d%02d%02d", $year,$mon,$mday;
$ptime = sprintf "%02d:%02d", $hour, $min;
if ($debug == 1) {
print "Current date/time is: $date - $ptime\n";
print "Current inverter status is: $xantrex_status\n";
print "Total KWH today is: $xantrex_kwhtoday\n";
print "Total WH today is: $xantrex_wtoday\n";
print "Current output is: $xantrex_pout\n";
}
chomp($xantrex_status);
my $length = length($xantrex_status);
$length -=1;
$status = substr $xantrex_status, 0, $length;
if (($submit == 1) && ($status eq "ON")) {
if ($debug == 1) {
print "Submitting data\n";
}
my $ua = new LWP::UserAgent;
$ua->default_header( 'X-Pvoutput-Apikey' => $pvoutput_api, 'X-Pvoutput-SystemId' => $pvoutport_systemid );
my $response
= $ua->post('http://pvoutput.org/service/r2/addstatus.jsp',
{ 'v2' => $xantrex_pout,
'v1' => $xantrex_wtoday,
'd' => $date,
't' => $ptime
});
my $content = $response->content;
if ($debug == 1) {
print "$content\n";
}
if ($response->is_error) {
print "Error updating pvoutput.org $content\r\n";
}
}
