| 1 | package Device::WH1091; | ||
| 2 | |||
| 3 | 0 | ▶ | use Inline C => DATA => LIBS => '-lusb'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 4 | use strict; | ||
| 5 | |||
| 6 | BEGIN { | ||
| 7 | use Exporter (); | ||
| 8 | use vars qw($VERSION $VENDOR_ID $PRODUCT_ID $CONFIG_NO $INTERFACE_NO | ||
| 9 | $INTERFACES_NUM $REQTYPE $REQ $VAL $GET_SIZE $TIMEOUT | ||
| 10 | @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS | ||
| 11 | ); | ||
| 12 | $VERSION = '0.03'; | ||
| 13 | @ISA = qw(Exporter); | ||
| 14 | #Give a hoot don't pollute, do not export more than needed by default | ||
| 15 | @EXPORT = qw(); | ||
| 16 | @EXPORT_OK = qw(); | ||
| 17 | %EXPORT_TAGS = (); | ||
| 18 | $VENDOR_ID = 0x1941; | ||
| 19 | $PRODUCT_ID = 0x8021; | ||
| 20 | |||
| 21 | $CONFIG_NO = 1; | ||
| 22 | $INTERFACE_NO = 0; | ||
| 23 | |||
| 24 | $INTERFACES_NUM = 2; | ||
| 25 | |||
| 26 | $REQTYPE = 0xA1; | ||
| 27 | |||
| 28 | $REQ = 0x1; | ||
| 29 | $VAL = 0x300; | ||
| 30 | $GET_SIZE = 8; | ||
| 31 | $TIMEOUT = 500; | ||
| 32 | |||
| 33 | |||
| 34 | } | ||
| 35 | |||
| 36 | sub new | ||
| 37 | { | ||
| 38 | my ($class, %parameters) = @_; | ||
| 39 | my $self = bless ({}, ref ($class) || $class); | ||
| 40 | $self->{'error'} = undef; | ||
| 41 | $self->{'data'} = undef; | ||
| 42 | |||
| 43 | return $self; | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | sub error { | ||
| 48 | my $self = shift; | ||
| 49 | return $self->{'error'}; | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | sub DESTROY { | ||
| 54 | my $self = shift; | ||
| 55 | |||
| 56 | if($self->{'dev'}) { | ||
| 57 | $self->{'dev'}->release_interface($INTERFACE_NO); | ||
| 58 | |||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | sub getdata { | ||
| 64 | |||
| 65 | my $self = shift; | ||
| 66 | |||
| 67 | my $buffer2; | ||
| 68 | my $current={}; | ||
| 69 | my @dir = ['N','NNE','NE','ENE','E','SEE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW']; | ||
| 70 | |||
| 71 | getweather($buffer2); | ||
| 72 | |||
| 73 | $self->{tindoor} = (get_bufferval($buffer2,242,243)) / 10 ; | ||
| 74 | $self->{toutdoor} = (get_bufferval($buffer2, 245, 246) ) / 10; | ||
| 75 | $self->{hindoor} = get_bufferval($buffer2, 241); | ||
| 76 | $self->{houtdoor} = get_bufferval($buffer2, 244); | ||
| 77 | $self->{windspeed} = get_bufferval($buffer2, 249)/10; | ||
| 78 | $self->{windgust} = get_bufferval($buffer2, 250)/10; | ||
| 79 | $self->{winddir} = get_bufferval($buffer2, 252) * 22.5; | ||
| 80 | $self->{winddirtext} = $dir[$self->{winddir}]; | ||
| 81 | $self->{pressure} = get_bufferval($buffer2, 247, 248)/10; | ||
| 82 | $self->{raintot} = get_bufferval($buffer2,253,254) * 3 / 10; | ||
| 83 | return 1; | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | |||
| 90 | sub get_bufferval { | ||
| 91 | |||
| 92 | my $buffer = shift; | ||
| 93 | my $byte1 = shift; | ||
| 94 | my $byte2 = shift; | ||
| 95 | |||
| 96 | |||
| 97 | my $ret = ord(substr($buffer,$byte1,1)); | ||
| 98 | if( defined $byte2 ) { | ||
| 99 | $ret = $ret + ord(substr($buffer,$byte2,1)) *256; | ||
| 100 | } | ||
| 101 | return $ret; | ||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | =head1 NAME | ||
| 110 | |||
| 111 | Device::WH1091 - Access data from the WH1081/1091 weather station. | ||
| 112 | |||
| 113 | =head1 SYNOPSIS | ||
| 114 | |||
| 115 | use Device::WH1091; | ||
| 116 | my $weather=Device::WH1091->new(); | ||
| 117 | |||
| 118 | $weather->getdata(); | ||
| 119 | |||
| 120 | |||
| 121 | my $tindoor = $weather->{tindoor}; # Indoor Temp | ||
| 122 | my $toutdoor = $weather->{toutdoor}; # Outdoor Temp | ||
| 123 | my $hindoor = $weather->{hindoor}; # Indoor Humidity | ||
| 124 | $houtdoor = $weather->{houtdoor}; # OutDoor Humidity | ||
| 125 | $windspeed = $weather->{windspeed}; # WindSpeed (m/s) | ||
| 126 | $windgust = $weather->{windgust}; # Wind Gust (m/s) | ||
| 127 | $winddir = $weather->{winddir}; # Wind Direction Degrees | ||
| 128 | $winddirtext = $weather->{winddirtext}; # Wind Direction Text | ||
| 129 | $pressure = $weather->{pressure}; # Air Pressure | ||
| 130 | $raintot = $weather->{raintot}; # Total Rain | ||
| 131 | |||
| 132 | =head1 DESCRIPTION | ||
| 133 | |||
| 134 | Provides an interface to the WH1081/WH1091 weather stations (and others based on the same hardware). | ||
| 135 | |||
| 136 | Requires libusb to be installed so essentially limited at this stage to Linux/Unix platforms that have libusb. | ||
| 137 | Currently, the usb code is inlined C, however this will bechanging to Perl USB at some stage so that this platform dependency is removed. | ||
| 138 | |||
| 139 | |||
| 140 | =head1 USAGE | ||
| 141 | |||
| 142 | Instatiate an instance by call WH1091->new(); | ||
| 143 | |||
| 144 | Whenever you want data, call getdata() and the object variables mention in the SYNOPSIS will be populated. | ||
| 145 | When you want more data, call getdata() again. | ||
| 146 | |||
| 147 | Be aware that the usb weather station only gets wireless updates from the weather head every 30 seconds, so polling more often than that would be pointless. | ||
| 148 | |||
| 149 | =head1 BUGS | ||
| 150 | |||
| 151 | I am still not 100% confident with the rain data. Your mileage may vary. | ||
| 152 | |||
| 153 | I have had this running a continuous loop getting data on an NSLU2 with openwrt for over three months now. | ||
| 154 | |||
| 155 | =head1 AUTHOR | ||
| 156 | |||
| 157 | David Peters | ||
| 158 | CPAN ID: DAVIDP | ||
| 159 | davidp@electronf.com | ||
| 160 | http://www.electronf.com | ||
| 161 | |||
| 162 | =head1 COPYRIGHT | ||
| 163 | |||
| 164 | This program is free software; you can redistribute | ||
| 165 | it and/or modify it under the same terms as Perl itself. | ||
| 166 | |||
| 167 | The full text of the license can be found in the | ||
| 168 | LICENSE file included with this module. | ||
| 169 | |||
| 170 | |||
| 171 | =head1 SEE ALSO | ||
| 172 | |||
| 173 | perl(1). | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | #################### main pod documentation end ################### | ||
| 178 | |||
| 179 | |||
| 180 | 1; | ||
| 181 | |||
| 182 | |||
| 183 | |||
| 184 | |||
| 185 | __DATA__ | ||