use Time::HiRes;
my $devname = "/dev/davis";
-my $dbsort = "SQLite";
+my $dbsort = "SQLite";
my $db="weather.db";
my $host="";
my $userid="";
$ob->read_const_time(15000); # ultimate timeout (15 seconds)
$ob->write_settings||die"setting failed";
-
my $awake=0;
my $attempts=0;
$ob->write("\n"); # initial wake
while ($awake==0) {
- $ob->write("\n"); # wake for real
- $ob->read_interval(600); # wait for a max of 600ms
+ $ob->write("\n"); # wake for real
+ $ob->read_interval(600); # wait for a max of 600ms
($count, $result) = $ob->read(10); # read up to 10 chars
if ($result eq "\n\r") {
- print "awoke on attempt $attempts :)\n";
+ print "awoke on attempt $attempts :)\n";
$awake=1;
- } else {
+ }
+ else {
print "wake error on attempt $attempts :(\n";
}
$attempts++;
}
$ob->write("LAMPS 0\n");
-$ob->read_interval(300); # wait for a max of 300ms
+$ob->read_interval(300); # wait for a max of 300ms
($count, $result) = $ob->read(8); # read up to 8 chars
$result=~s/(\r|\n)//g;
if ($result eq "OK") {
print "lamps on :)\n";
-} else {
+}
+else {
dienice("lamp error '$result'");
}
#do_dmpaft();
while (1) {
- get_loop(); # if you do anything after here - you need to wake up the device again
+ get_loop(); # if you do anything after here - you need to wake up the device again
print "** do something else\n";
sleep 5;
- $ob->write("\r"); # wake
- $ob->read_interval(500); # wait for a max of 300ms
+ $ob->write("\r"); # wake
+ $ob->read_interval(500); # wait for a max of 300ms
my ($count, $result) = $ob->read(4096); # read any crap up to 4096 chars
}
#$ob->write("WRD\x12\x4d\n");
-undef $ob;
+undef $ob;
exit 0;
sub dienice
exit 9;
}
-sub parse_loop_blck
+sub parse_loop_blck
{
- my $blk = shift;
- my $loo = substr $blk,0,3;
+ my $blk = shift;
+ my $loo = substr $blk,0,3;
unless ( $loo eq 'LOO') {
warn("Block invalid loo -> $loo\n"); return "";
- }
+ }
my $t;
-
- #$hsh{'next_rec'} = unpack("s", substr $blk,5,2);
-
+
+ #$hsh{'next_rec'} = unpack("s", substr $blk,5,2);
+
$hsh{'Barometric_Trend'} = unpack("C", substr $blk,3,1);
$hsh{'Barometric_Trend_txt'} = $bar_trend{$hsh{'Barometric_Trend'}};
- $t = unpack("s", substr $blk,7,2) / 1000;
- # $hsh{'Barometric_Press_hg'} = $t;
- $hsh{'Barometric_Press_mb'} = sprintf("%.2f",$t*33.8637526);
-
+ $t = unpack("s", substr $blk,7,2) / 1000;
+# $hsh{'Barometric_Press_hg'} = $t;
+ $hsh{'Barometric_Press_mb'} = sprintf("%.2f",$t*33.8637526);
+
$t = unpack("s", substr $blk,9,2) / 10;
- # $hsh{'Air_Temp_Inside_f'} = $t;
+# $hsh{'Air_Temp_Inside_f'} = $t;
$hsh{'Air_Temp_Inside_c'} = sprintf("%.1f",($t - 32) * 5/9);
- my $tf = unpack("s", substr $blk,12,2) / 10;
- # $hsh{'Air_Temp_Outside_f'} = $tf;
+ my $tf = unpack("s", substr $blk,12,2) / 10;
+# $hsh{'Air_Temp_Outside_f'} = $tf;
$hsh{'Air_Temp_Outside_c'} = sprintf("%.1f",($tf - 32) * 5/9);
-
- $hsh{'Wind_Speed_mph'} = unpack("C", substr $blk,14,1);
- # $hsh{'Wind_Speed_mps'} = sprintf("%.1f",$hsh{'Wind_Speed_mph'}*0.44704);
+
+ $hsh{'Wind_Speed_mph'} = unpack("C", substr $blk,14,1);
+# $hsh{'Wind_Speed_mps'} = sprintf("%.1f",$hsh{'Wind_Speed_mph'}*0.44704);
$hsh{'Wind_Speed_10min_Avg_mph'} = unpack("C", substr $blk,15,1);
- # $hsh{'Wind_Speed_10min_Avg_mps'} = sprintf("%.1f",$hsh{'Wind_Speed_10min_Avg_mph'}*0.44704);
+# $hsh{'Wind_Speed_10min_Avg_mps'} = sprintf("%.1f",$hsh{'Wind_Speed_10min_Avg_mph'}*0.44704);
$hsh{'Wind_Dir'} = unpack("s", substr $blk,16,2);
-
-
+
+
$hsh{'Humidity_Outside'} = unpack("C", substr $blk,33,1);
$hsh{'Humidity_Inside'} = unpack("C", substr $blk,11,1);
$hsh{'Dew_Point'} = dew_point($tf, $hsh{'Humidity_Outside'});
-
- # $hsh{'UV'} = unpack("C", substr $blk,43,1);
- # $hsh{'Solar'} = unpack("s", substr $blk,44,2); # watt/m**2
-
+
+# $hsh{'UV'} = unpack("C", substr $blk,43,1);
+# $hsh{'Solar'} = unpack("s", substr $blk,44,2); # watt/m**2
+
$hsh{'Rain_Rate'} = (unpack("s", substr $blk,41,2) / 100) * 25.4; # Inches per hr converted to mm
$hsh{'Rain_Storm'} = (unpack("s", substr $blk,46,2) / 100) * 25.4; # Inches per storm
#$hsh{'Storm_Date'} = unpack("s", substr $blk,48,2); # Need to parse data (not sure what this is)
- $hsh{'Rain_Day'} = (unpack("s", substr $blk,50,2)/100) * 25.4;
- $hsh{'Rain_Month'} = (unpack("s", substr $blk,52,2)/100) * 25.4;
- $hsh{'Rain_Year'} = (unpack("s", substr $blk,54,2)/100) * 25.4;
-
- $hsh{'ET_Day'} = unpack("s", substr $blk,56,2)/1000;
- $hsh{'ET_Month'} = unpack("s", substr $blk,58,2)/100;
- $hsh{'ET_Year'} = unpack("s", substr $blk,60,2)/100;
-
- #$hsh{'Alarms_Inside'} = unpack("b8", substr $blk,70,1);
- #$hsh{'Alarms_Rain'} = unpack("b8", substr $blk,70,1);
- #$hsh{'Alarms_Outside'} = unpack("b8", substr $blk,70,1);
-
+ $hsh{'Rain_Day'} = (unpack("s", substr $blk,50,2)/100) * 25.4;
+ $hsh{'Rain_Month'} = (unpack("s", substr $blk,52,2)/100) * 25.4;
+ $hsh{'Rain_Year'} = (unpack("s", substr $blk,54,2)/100) * 25.4;
+
+ $hsh{'ET_Day'} = unpack("s", substr $blk,56,2)/1000;
+ $hsh{'ET_Month'} = unpack("s", substr $blk,58,2)/100;
+ $hsh{'ET_Year'} = unpack("s", substr $blk,60,2)/100;
+
+ #$hsh{'Alarms_Inside'} = unpack("b8", substr $blk,70,1);
+ #$hsh{'Alarms_Rain'} = unpack("b8", substr $blk,70,1);
+ #$hsh{'Alarms_Outside'} = unpack("b8", substr $blk,70,1);
+
$hsh{'Batt_Transmitter'} = unpack("C", substr $blk,86,1); # * 0.005859375
- $hsh{'Batt_Console'} = unpack("s", substr $blk,87,2) * 0.005859375;
-
- $hsh{'Forecast_Icon'} = unpack("C", substr $blk,89,1);
- $hsh{'Forecast_Rule'} = unpack("C", substr $blk,90,1);
-
- $hsh{'Sunrise'} = sprintf( "%04d", unpack("S", substr $blk,91,2) );
- $hsh{'Sunrise'} =~ s/(\d{2})(\d{2})/$1:$2/;
- $hsh{'Sunset'} = sprintf( "%04d", unpack("S", substr $blk,93,2) );
+ $hsh{'Batt_Console'} = unpack("s", substr $blk,87,2) * 0.005859375;
+
+ $hsh{'Forecast_Icon'} = unpack("C", substr $blk,89,1);
+ $hsh{'Forecast_Rule'} = unpack("C", substr $blk,90,1);
+
+ $hsh{'Sunrise'} = sprintf( "%04d", unpack("S", substr $blk,91,2) );
+ $hsh{'Sunrise'} =~ s/(\d{2})(\d{2})/$1:$2/;
+ $hsh{'Sunset'} = sprintf( "%04d", unpack("S", substr $blk,93,2) );
$hsh{'Sunset'} =~ s/(\d{2})(\d{2})/$1:$2/;
+
+ #my $nl = ord substr $blk,95,1;
+ #my $cr = ord substr $blk,96,1;
- #my $nl = ord substr $blk,95,1;
- #my $cr = ord substr $blk,96,1;
-
- my $crc = unpack "%n", substr($blk,97,2);
- my $crc_calc = CRC_CCITT($blk);
-
+ my $crc = unpack "%n", substr($blk,97,2);
+ my $crc_calc = CRC_CCITT($blk);
+
if ($crc_calc==0) {
return 0;
}
#delete($hsh{crc})||die"cant delete crc";
#delete($hsh{crc_calc})||die"cant delete crc_calc";
#delete($hsh{next_rec})||die"cant delete next_rec";
-
+
}
sub dew_point
{
- my $temp = shift @_;
- my $rh = shift @_;
-
- # Using the simplified approximation for dew point
- # Accurate to 1 degree C for humidities > 50 %
+ my $temp = shift @_;
+ my $rh = shift @_;
+
+ # Using the simplified approximation for dew point
+ # Accurate to 1 degree C for humidities > 50 %
# http://en.wikipedia.org/wiki/Dew_point
- my $dew_point = $temp - ( (100 - $rh)/5 );
-
- return $dew_point;
+ my $dew_point = $temp - ( (100 - $rh)/5 );
+
+ return $dew_point;
}
sub CRC_CCITT
{
- # Expects packed data...
+ # Expects packed data...
my $data_str = shift @_;
my $crc = 0;
my @lst = split //, $data_str;
foreach my $data (@lst) {
- my $data = unpack("c",$data);
-
+ my $data = unpack("c",$data);
+
my $crc_prev = $crc;
my $index = $crc >> 8 ^ $data;
my $lhs = $crc_table[$index];
#print "lhs=$lhs, crc=$crc\n";
my $rhs = ($crc << 8) & 0xFFFF;
$crc = $lhs ^ $rhs;
-
-
+
+
}
-
+
return $crc;
}
0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0xed1, 0x1ef0
);
- $bar_trend{-60} = "Falling Rapidly";
+ $bar_trend{-60} = "Falling Rapidly";
$bar_trend{196} = "Falling Rapidly";
- $bar_trend{-20} = "Falling Slowly";
+ $bar_trend{-20} = "Falling Slowly";
$bar_trend{236} = "Falling Slowly";
$bar_trend{0} = "Steady";
$bar_trend{20} = "Rising Slowly";
if ($cnt_in==0) {
dienice("read error cnt_in=$cnt_in, str='$str'");
}
-
+
my $ck = CRC_CCITT(substr($str,1,9));
if ( $ck ) {
warn "checksum error"; return 0;
}
my @rsp_lst = split //, $str;
- shift @rsp_lst;
- @rsp_lst = map ord, @rsp_lst;
- return \@rsp_lst;
+ shift @rsp_lst;
+ @rsp_lst = map ord, @rsp_lst;
+ return \@rsp_lst;
}
sub settime
{
- my $s_time = [ localtime() ];
- $s_time->[4] += 1;
-
+ my $s_time = [ localtime() ];
+ $s_time->[4] += 1;
+
$ob->write("SETTIME\n");
$ob->read_interval(300);
my ($cnt_in, $str) = $ob->read(1);
- my $ack = ord $str;
+ my $ack = ord $str;
if ( $ack != 6 ) {
warn "SETTIME not set ack $ack !"; return 0;
}
my ($sec, $min, $hour, $day, $mon, $yr) = @{$s_time};
- $str = join "", map chr, ($sec, $min, $hour, $day, $mon, $yr);
+ $str = join "", map chr, ($sec, $min, $hour, $day, $mon, $yr);
my $ck = CRC_CCITT($str);
- $str = $str . pack("n",$ck);
+ $str = $str . pack("n",$ck);
$ob->write($str);
($cnt_in, $str) = $ob->read(1);
if ( ord($str) != 6 ) {
warn "SETTIME not set!"; return 0;
}
- sleep 3; # The console seems to need to some time here...
+ sleep 3; # The console seems to need to some time here...
}
sub get_loop
$ob->read_interval(0);
($count, $result) = $ob->read(1);
for (my $i=0; $i<$loops; $i+=1) {
- my $rc=loop_dump();
+ my $rc=loop_dump();
if ($rc!=0) {
last;
}
#my $self = shift @_;
open(DMP,">dump.txt");
my $vDateStamp = shift @_;
- my $vTimeStamp = shift @_;
+ my $vTimeStamp = shift @_;
# If not date/time stamp then assume 0 which will down load the entire archive
unless ( $vDateStamp ) {
$vDateStamp = 0;
- }
+ }
unless ( $vTimeStamp ) {
$vTimeStamp = 0;
- }
-
- #my $port_obj = $self->{'port_obj'};
-
- my $datetime = pack("ss",$vDateStamp, $vTimeStamp);
-
+ }
+
+ #my $port_obj = $self->{'port_obj'};
+
+ my $datetime = pack("ss",$vDateStamp, $vTimeStamp);
+
my $crc = CRC_CCITT($datetime);
- my $cmd = pack("ssn",$vDateStamp,$vTimeStamp,$crc);
+ my $cmd = pack("ssn",$vDateStamp,$vTimeStamp,$crc);
- #-----------------------
- #my $str = unpack("H*", $cmd);
- #$str =~ s/(\w{2})/$1 /g;
+ #-----------------------
+ #my $str = unpack("H*", $cmd);
+ #$str =~ s/(\w{2})/$1 /g;
# Documentation is wrong! The example should be <0xC6><0x06><0xA2><0x03> in section X
- #print "cmd : $str \n";exit;
- #-----------------------
+ #print "cmd : $str \n";exit;
+ #-----------------------
- #sleep 2; # Needed after loop
- #$self->wake_up();
-
- # Ok let's start the communication sequence....
+ #sleep 2; # Needed after loop
+ #$self->wake_up();
+
+ # Ok let's start the communication sequence....
my $cnt_out = $ob->write("DMPAFT\n");
unless ($cnt_out) {
warn "write failed\n";
}
;
-
+
$ob->read_interval(300);
my ($cnt_in, $str) = $ob->read(1);
-
- my $ack = ord $str;
+
+ my $ack = ord $str;
unless ($ack == 6) {
warn "Ack not received on DMPAFT command: $ack"; exit -1;
}
-
+
$cnt_out = $ob->write($cmd);
unless ($cnt_out) {
warn "write failed\n";
}
;
- ($cnt_in, $str) = $ob->read(7);
-
- $ack = ord substr($str,0,1);
-
- my $ls = unpack("H20",substr($str,1,4) );
- $ls =~ s/(\w{2})/$1 /g;
-
- my $pages = unpack("s",substr($str,1,2) );
- my $rec_start = unpack("s",substr($str,3,2) );
-
+ ($cnt_in, $str) = $ob->read(7);
+
+ $ack = ord substr($str,0,1);
+
+ my $ls = unpack("H20",substr($str,1,4) );
+ $ls =~ s/(\w{2})/$1 /g;
+
+ my $pages = unpack("s",substr($str,1,2) );
+ my $rec_start = unpack("s",substr($str,3,2) );
+
$crc = CRC_CCITT(substr($str,1,6) );
- print "Pages = $pages : rec = $rec_start Datestamp $vDateStamp $crc\n";
-
+ print "Pages = $pages : rec = $rec_start Datestamp $vDateStamp $crc\n";
+
$cnt_out = $ob->write( pack("h", 0x06) );
-
+
#if ($pages == 513 ) { return -1 }
-
- my @arc_rec_lst;
+
+ my @arc_rec_lst;
foreach my $page (1..$pages) {
- my $page_sz = 267;
+ my $page_sz = 267;
$ob->read_interval(0);
my ($cnt_in, $str) = $ob->read($page_sz); #,3
printf("len=%s\n",length($str));
if ($cnt_in!=$page_sz) {
dienice("hmm, dmpaft only got $cnt_in bytes. was expecting $page_sz");
}
- print "Page $page\n";
+ print "Page $page\n";
#print DMP $str,"\n";
#print "ACK receipt of page $page\n";
#$ob->write( pack("h", 0x06) );
my $crc = unpack "%n", substr($str,265,2);
print "page crc=$crc, calc_crc=$calc_crc\n";
my $rec_sz = 52;
- my $date_prev = 0;
+ my $date_prev = 0;
my %hsh;
-
+
foreach my $rec ( 0..4 ) {
if ( ($page == 1) && ($rec < $rec_start ) ) {
next;
- } # Find the right starting point...
+ } # Find the right starting point...
- my $start_ptr = 1 + ($rec * $rec_sz );
+ my $start_ptr = 1 + ($rec * $rec_sz );
my $rec_str = substr($str, $start_ptr ,52);
-
- #print "$start_ptr \t > " . unpack( "h*", $rec_str) . "\n";
-
- my $date = substr($rec_str,0,2);
+
+ #print "$start_ptr \t > " . unpack( "h*", $rec_str) . "\n";
+
+ my $date = substr($rec_str,0,2);
my $date_curr = unpack "s", $date;
-
- # Check if we have wrapped...
+
+ # Check if we have wrapped...
if ( $date_curr < $date_prev ) {
last;
- }
- $date_prev = $date_curr;
-
- $hsh{'date_stamp'} = $date_curr;
- $hsh{'time_stamp'} = unpack "s", substr($rec_str,2,2);
-
- $hsh{'day'} = unpack( "c", $date & pack("c",0x1F) );
- $hsh{'month'} = ( $hsh{'date_stamp'} >> 5) & 0xF;
- $hsh{'year'} = ( $hsh{'date_stamp'} >> 9) + 2000;
-
- $hsh{'hour'} = sprintf("%02d", int ( $hsh{'time_stamp'} / 100 ));
-
- $hsh{'min'} = $hsh{'time_stamp'} - ($hsh{'hour'} * 100);
- $hsh{'min'} = sprintf("%02d", $hsh{'min'});
-
- $hsh{'time_stamp_fmt'} = "$hsh{'hour'}:$hsh{'min'}:00";
- $hsh{'date_stamp_fmt'} = "$hsh{'year'}_$hsh{'month'}_$hsh{'day'}";
+ }
+ $date_prev = $date_curr;
+
+ $hsh{'date_stamp'} = $date_curr;
+ $hsh{'time_stamp'} = unpack "s", substr($rec_str,2,2);
+
+ $hsh{'day'} = unpack( "c", $date & pack("c",0x1F) );
+ $hsh{'month'} = ( $hsh{'date_stamp'} >> 5) & 0xF;
+ $hsh{'year'} = ( $hsh{'date_stamp'} >> 9) + 2000;
+
+ $hsh{'hour'} = sprintf("%02d", int ( $hsh{'time_stamp'} / 100 ));
+
+ $hsh{'min'} = $hsh{'time_stamp'} - ($hsh{'hour'} * 100);
+ $hsh{'min'} = sprintf("%02d", $hsh{'min'});
+
+ $hsh{'time_stamp_fmt'} = "$hsh{'hour'}:$hsh{'min'}:00";
+ $hsh{'date_stamp_fmt'} = "$hsh{'year'}_$hsh{'month'}_$hsh{'day'}";
#$hsh{'unixtime'} = timelocal(0,$hsh{min}, $hsh{hour}, $hsh{day}, $hsh{month}-1, $hsh{year}-1900);
-
- $hsh{'Air_Temp'} = unpack("s", substr($rec_str,4,2)) / 10;
- $hsh{'Air_Temp_Hi'} = unpack("s", substr($rec_str,6,2)) / 10;
+
+ $hsh{'Air_Temp'} = unpack("s", substr($rec_str,4,2)) / 10;
+ $hsh{'Air_Temp_Hi'} = unpack("s", substr($rec_str,6,2)) / 10;
$hsh{'Air_Temp_Lo'} = unpack("s", substr($rec_str,8,2)) / 10;
$hsh{'Rain_Clicks'} = unpack("s", substr($rec_str,10,2));
$hsh{'Rain_Rate'} = unpack("s", substr($rec_str,12,2)) / 100; # Inches per hour
- $hsh{'Barometric_Press'} = unpack("s", substr $rec_str,14,2) / 1000;
+ $hsh{'Barometric_Press'} = unpack("s", substr $rec_str,14,2) / 1000;
$hsh{'Solar'} = unpack("s", substr $rec_str,16,2); # watt/m**2
- $hsh{'Wind_Samples'} = unpack("s", substr $rec_str,18,2);
- $hsh{'Air_Temp_Inside'} = unpack("s", substr $rec_str,20,2) / 10;
+ $hsh{'Wind_Samples'} = unpack("s", substr $rec_str,18,2);
+ $hsh{'Air_Temp_Inside'} = unpack("s", substr $rec_str,20,2) / 10;
$hsh{'Relative_Humidity_Inside'} = unpack("C", substr $rec_str,22,1);
$hsh{'Relative_Humidity'} = unpack("C", substr $rec_str,23,1);
- $hsh{'Wind_Speed'} = unpack("C", substr($rec_str,24,1));
+ $hsh{'Wind_Speed'} = unpack("C", substr($rec_str,24,1));
$hsh{'Wind_Gust_Max'} = unpack("C", substr($rec_str,25,1));
$hsh{'Wind_Dir_Max'} = unpack("C", substr($rec_str,26,1));
$hsh{'Wind_Dir'} = unpack("C", substr($rec_str,27,1));
$hsh{'UV'} = unpack("C", substr($rec_str,28,1)) / 10;
$hsh{'ET'} = unpack("C", substr($rec_str,29,1)) / 1000;
- $hsh{'Solar_Max'} = unpack("s", substr($rec_str,30,2));
+ $hsh{'Solar_Max'} = unpack("s", substr($rec_str,30,2));
$hsh{'UV_Max'} = unpack("C", substr($rec_str,32,1));
-
+
$hsh{'Forecast_Rule'} = unpack("C", substr($rec_str,33,1));
-
- # $hsh{'Dew_Point'} = _dew_point($hsh{'Air_Temp'},$hsh{'Relative_Humidity'});
-
+
+# $hsh{'Dew_Point'} = _dew_point($hsh{'Air_Temp'},$hsh{'Relative_Humidity'});
+
# Miscellaneous others omitted for now
-
- print "date> $hsh{'Air_Temp'} $hsh{'time_stamp'} $hsh{'time_stamp_fmt'} $hsh{'date_stamp'} $hsh{'date_stamp_fmt'}\n";
- #print Dumper \%hsh;
-
- push @arc_rec_lst, {%hsh};
- }
-
+
+ print "date> $hsh{'Air_Temp'} $hsh{'time_stamp'} $hsh{'time_stamp_fmt'} $hsh{'date_stamp'} $hsh{'date_stamp_fmt'}\n";
+ #print Dumper \%hsh;
+
+ push @arc_rec_lst, {%hsh};
+ }
+
#$in = <STDIN>; # Testing step through facility
#if ($in =~ /q/i ) { $port_obj->write( pack("h", 0x1B) ); last; }
#else { $port_obj->write( pack("h", 0x06) ); }
print "ACK receipt of page\n";
$ob->write( pack("h", 0x06) );
-
+
}
close DMP;
}