Send wind even if it hasn't changed
[dweather.git] / dumpraw.pl
1 #!/usr/bin/perl
2
3 use DBI;
4 use strict;
5 use Time::HiRes;
6 use Serial;
7 use Device::SerialPort;
8 use Mojo::IOLoop;
9 use Mojo::IOLoop::Stream;
10
11 my $devname = "/dev/davis";
12
13 #my $ob = Serial->new ($devname, 19200) || die;
14 my $ob = Device::SerialPort->new ($devname) || die;
15
16 $ob->user_msg(1);                               # misc. warnings
17 $ob->error_msg(1);                              # hardware and data errors
18
19 $ob->baudrate(19200);
20 $ob->parity("none");
21 #$ob->parity_enable(1);         # for any parity except "none"
22 $ob->databits(8);
23 $ob->stopbits(1);
24 $ob->handshake('none');
25 $ob->read_const_time(15000);    # ultimate timeout (15 seconds)
26 $ob->write_settings||die"setting failed";
27
28 $ob->read_char_time(0);
29 $ob->read_const_time(300);
30
31 my $awake=0;
32 my $attempts=0;
33 $ob->write("\n");                               # initial wak
34
35 my $awake = 0;
36 my $attempts;
37
38 while ($awake==0) {
39         $ob->write("\n");                                  # wake for real
40         my ($count, $result) = $ob->read(10); # read up to 10 chars
41         if ($result eq "\n\r") {
42                 print "awoke on attempt $attempts :)\n";
43                 $awake=1;
44         } else {
45                 print "wake error on attempt $attempts :(\n";
46         }
47         $attempts++;
48         die("failed to wake device - tried $attempts times\n") unless $attempts<6;
49 }
50
51 $ob->write("STRMON\n");
52
53 my $end;
54
55 $SIG{TERM} = $SIG{INT} = sub {++$end; print "\nending $end\n";};
56
57 while (!$end) {
58
59 #       print "end: $end\n";
60         
61         my ($count, $l) = $ob->read(255); 
62         next unless $count;
63
64         s|\cM|<CR>|g;
65         s|\cJ|<NL>|g;
66         s|\cI|<TAB>|g;
67         
68         my @l = split /[\cJ\cM]/, $l;
69         my ($wspeed, $wdir, $val1, $val2, $sort, $batt);
70
71         my @hex;
72         
73         for (@l) {
74                 next unless $_;
75                 my ($k, $v) = $_ =~ /^(\d)\s+=\s+([\dabcdef]+)/;
76                 next unless defined $k && defined $v;
77                 my $h = hex $v;
78                 push @hex, $h;
79                 if ($k == 0) {
80                         if (($h & 0xf0) == 0x80) {
81                                 $sort = "T";
82                         } elsif (($h & 0xf0) == 0xa0) {
83                                 $sort = 'H';
84                         } elsif (($h & 0xf0) == 0xe0) {
85                                 $sort = 'R';
86                         } elsif (($h & 0xf0) == 0x50) {
87                                 $sort = 'P';
88                         } else {
89                                 $sort = 'W';    # wind only
90                         }
91                         if ($h & 0x08) {        # new battery required
92                                 $batt = ' NB';
93                         }
94                 } elsif ($k == 1) {
95                         $wspeed = $h;
96                 } elsif ($k == 2) {
97                         $wdir = int($h * 360 / 255);
98                 } elsif ($k == 3) {
99                         $val2 = $val1 = $h;
100                 } elsif ($k == 4) {
101                         if ($sort eq 'H') {
102                                 $val2 = (($h >> 4) << 8) | $val2;
103                         } elsif ($sort eq 'T') {
104                                 $val2 = ($h << 8) | $val2;
105                         } else {
106                                 $val2 = ($val2 << 8) | $h;
107                         }
108                 }
109         }
110         if ($sort) {
111                 printf("%02X ", $_) for (@hex);
112                 if ($sort eq 'T') {     
113                         printf "$sort$batt %3d %3d %0.1f\n", $wspeed, $wdir, (($val2/160) - 32) * 5/9;
114                 } elsif ($sort eq 'H') {
115                         printf "$sort$batt %3d %3d %0.1f\n", $wspeed, $wdir, $val2/10;
116                 } elsif ($sort eq 'P') {
117                         printf "$sort$batt %3d %3d %0.2f\n", $wspeed, $wdir, $val2 / 1000;
118                 } elsif ($sort eq 'R') {
119                         printf "$sort$batt %3d %3d $val1\n", $wspeed, $wdir;
120                 } elsif ($sort eq 'W') {
121                         printf "$sort$batt %3d %3d\n", $wspeed, $wdir, $val2;
122                 } else {
123                         print "\n";
124                 }
125         }
126 }
127
128 $ob->write("STRMOFF\n");
129 $ob->read(255);
130 exit(0);
131
132
133