send data directly to influx
[vaillant-thermostat] / serial.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Device::SerialPort;
5 use Data::Dump qw(dump);
6 use Time::HiRes qw(time);
7 use LWP::UserAgent;
8
9 my $read_len = 255;
10
11 my $path = shift @ARGV || '/dev/serial/by-path/platform-20980000.usb-usb-0:1.2:1.0-port0';
12
13 $|=1;
14
15 my $ua = LWP::UserAgent->new;
16
17 connect_again:
18
19 my $s = new Device::SerialPort( $path ) || die $!;
20 $s->baudrate(2400);
21 $s->databits(8); # 7
22 $s->parity('none');
23 $s->stopbits(1);
24 $s->handshake('none');
25 $s->read_char_time(2);
26 $s->read_const_time(3);
27
28 #$s->write('o');
29
30 my $t = time();
31
32 my $sym;
33
34 sub _mqtt_pub {
35         my ( $t, $m ) = @_;
36         my $cmd = "mosquitto_pub -h rpi2 -t '$t' -m '$m'";
37         #warn "# _mqtt_pub [$t] = [$m]\n";
38         #warn "## $cmd";
39         system $cmd;
40 }
41
42 while (1) {
43         my ($len, $string) = $s->read($read_len);
44         goto connect_again if ! defined $len;
45         my $ts = time();
46
47         if ( $len > 0 ) {
48                 my $hex = unpack('H*',$string);
49                 $hex =~ s/(..)/$1 /g;
50
51                 my $temp = `curl --silent localhost:3000/temp.txt`;
52                 $temp =~ s/\s+/ /gs;
53                 $temp =~ s/^\s+//;
54
55                 #printf "%s %2d  %s\n", time(), $len, $hex;
56                 printf "%8.4f %6.2f %2d  %s | %s\n", $ts, $ts - $t, $len, $hex, $temp;
57                 $t = time();
58
59                 if ( $hex =~ m/^.+aa fc 39 aa (.+)/ ) {
60                         my @l = split(/\s+/,$1);
61                         my @d = (
62                                 hex( $l[0] . $l[1] ),
63                                 hex( $l[3] . $l[4] ),
64                                 hex( $l[6] ),
65                                 hex( $l[12] ),
66                                 hex( $l[14] ), # * 300,
67                                 hex( $l[15] ), # * 400,
68                                 hex( $l[18] ),
69                         );
70
71                         _mqtt_pub "stat/boiler/" . chr(ord('a') + $_) => $d[$_] foreach 0 .. $#d;
72                 }
73
74                 my @v = split(/\s+/, $temp, 4);
75                 my @n = qw( DS18B20/temperature DHT22/temperature DHT22/humidity DHT22/errors );
76                    @n = qw( DS18B20/temperature DHT22/temperature DHT22/humidity DHT22/errors ) if $#v == 1; # no DS18B20
77                 foreach (@v) {
78                         _mqtt_pub "stat/boiler/" . shift(@n) => $_;
79                 }
80
81                 my $t = int( $ts * 1_000_000_000 );
82                 my @a = split(/ /, $hex);
83                 if ( $#a == 66 ) {
84                         my $measurement = "boiler " . join(',',
85                                 "o19=".hex( $a[19] ),
86                                 "o25=".hex( $a[35] . $a[36] ),
87                                 "o30=".hex( $a[38] . $a[39] ),
88                                 "o41=".hex( $a[41] ),
89                                 "o47=".hex( $a[47] ),
90                                 "o49=".hex( $a[49] ),
91                                 "o50=".hex( $a[50] ),
92                                 "o51=".hex( $a[51] ),
93                                 "o53=".hex( $a[53] ),
94                                 "ds18b20_temp=$v[0]", "dht22_temp=$v[1]", "dht22_hum=$v[2]"
95                         );
96                         my $response = $ua->post( "http://10.60.0.92:8086/write?db=rot13", Content => $measurement );
97                         chomp(my $content = $response->content());
98                         if ($response->is_success()) {
99                                 #warn "OK $content\n";
100                         } else {
101                                 warn "ERROR $content\n";
102                                 die $response->status_line;
103                         }
104                 }
105         }
106 }
107
108 $s->close;