4 use Device::SerialPort;
5 use Data::Dump qw(dump);
6 use Time::HiRes qw(time);
11 # bottom serial on rpi
12 my $path = shift @ARGV || '/dev/serial/by-path/platform-1c14400.usb-usb-0:1:1.0-port0';
16 my $ua = LWP::UserAgent->new;
20 my $s = new Device::SerialPort( $path ) || die $!;
25 $s->handshake('none');
26 $s->read_char_time(2);
27 $s->read_const_time(3);
37 my $cmd = "mosquitto_pub -h rpi2 -t '$t' -m '$m'";
38 #warn "# _mqtt_pub [$t] = [$m]\n";
44 my ($len, $string) = $s->read($read_len);
45 goto connect_again if ! defined $len;
49 my $hex = unpack('H*',$string);
52 my $temp = `curl --silent rpi:3000/temp.txt`;
56 #printf "%s %2d %s\n", time(), $len, $hex;
57 printf "%8.4f %6.2f %2d %s | %s\n", $ts, $ts - $t, $len, $hex, $temp;
60 if ( $hex =~ m/^.+aa fc 39 aa (.+)/ ) {
61 my @l = split(/\s+/,$1);
67 hex( $l[14] ), # * 300,
68 hex( $l[15] ), # * 400,
72 _mqtt_pub "stat/boiler/" . chr(ord('a') + $_) => $d[$_] foreach 0 .. $#d;
75 my @v = split(/\s+/, $temp, 4);
76 my @n = qw( DS18B20/temperature DHT22/temperature DHT22/humidity DHT22/errors );
77 @n = qw( DS18B20/temperature DHT22/temperature DHT22/humidity DHT22/errors ) if $#v == 1; # no DS18B20
79 _mqtt_pub "stat/boiler/" . shift(@n) => $_;
82 my $t = int( $ts * 1_000_000_000 );
83 my @a = split(/ /, $hex);
85 my $measurement = "boiler " . join(',',
87 "o25=".hex( $a[35] . $a[36] ),
88 "o30=".hex( $a[38] . $a[39] ),
95 "ds18b20_temp=$v[0]", "dht22_temp=$v[1]", "dht22_hum=$v[2]"
97 my $response = $ua->post( "http://10.60.0.92:8086/write?db=rot13", Content => $measurement );
98 chomp(my $content = $response->content());
99 if ($response->is_success()) {
100 #warn "OK $content\n";
102 warn "ERROR $content\n";
103 die $response->status_line;