X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=serial.pl;h=cfceb37bb160586d0d0314d0b13b0ee52a8d47e3;hb=0e25565f4c36c6dcf79db411e3f00027155049a2;hp=63372d02516fc0d7d2a4f4becfdf2aebcb9a591e;hpb=6823b6c5a9d0df3479960376916998a96d926bfe;p=vaillant-thermostat diff --git a/serial.pl b/serial.pl index 63372d0..cfceb37 100755 --- a/serial.pl +++ b/serial.pl @@ -7,11 +7,15 @@ use Time::HiRes qw(time); my $read_len = 255; +my $path = shift @ARGV || '/dev/serial/by-path/platform-20980000.usb-usb-0:1.3.1:1.0-port0'; + $|=1; -my $s = new Device::SerialPort( '/dev/ttyUSB0' ) || die $!; +connect_again: + +my $s = new Device::SerialPort( $path ) || die $!; $s->baudrate(2400); -$s->databits(8); +$s->databits(8); # 7 $s->parity('none'); $s->stopbits(1); $s->handshake('none'); @@ -22,16 +26,54 @@ $s->read_const_time(3); my $t = time(); +my $sym; + +sub _mqtt_pub { + my ( $t, $m ) = @_; + my $cmd = "mosquitto_pub -h rpi2 -t '$t' -m '$m'"; + #warn "# _mqtt_pub [$t] = [$m]\n"; + warn "## $cmd"; + system $cmd; +} + while (1) { my ($len, $string) = $s->read($read_len); + goto connect_again if ! defined $len; my $ts = time(); if ( $len > 0 ) { my $hex = unpack('H*',$string); $hex =~ s/(..)/$1 /g; + + my $temp = `curl localhost:3000/temp.txt`; + $temp =~ s/\s+/ /gs; + $temp =~ s/^\s+//; + #printf "%s %2d %s\n", time(), $len, $hex; - printf "%8.4f %0.4f %2d %s\n", $ts, $ts - $t, $len, $hex; + printf "%8.4f %6.2f %2d %s | %s\n", $ts, $ts - $t, $len, $hex, $temp; $t = time(); + + if ( $hex =~ m/^.+aa fc 39 aa (.+)/ ) { + my @l = split(/\s+/,$1); + my @d = ( + hex( $l[0] . $l[1] ), + hex( $l[3] . $l[4] ), + hex( $l[6] ), + hex( $l[12] ), + hex( $l[14] ), # * 300, + hex( $l[15] ), # * 400, + hex( $l[18] ), + ); + + _mqtt_pub "stat/boiler/" . chr(ord('a') + $_) => $d[$_] foreach 0 .. $#d; + } + + my @v = split(/\s+/, $temp, 3); + my @n = qw( DS18B20/temperature DHT22/temperature DHT22/humidity ); + foreach (@v) { + _mqtt_pub "stat/boiler/" . shift(@n) => $_; + } + } }