send zph02 values to influx
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 29 Sep 2019 08:36:11 +0000 (10:36 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 29 Sep 2019 08:36:11 +0000 (10:36 +0200)
zph02.pl [new file with mode: 0755]

diff --git a/zph02.pl b/zph02.pl
new file mode 100755 (executable)
index 0000000..50aadcb
--- /dev/null
+++ b/zph02.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+# sudo apt install libdevice-serialport-perl libdata-dump-perl
+use Device::SerialPort;
+use Data::Dump qw(dump);
+
+my $port = shift @ARGV || '/dev/ttyUSB0';
+my $influx_url = shift @ARGV || 'http://10.13.37.229:8186/write?db=telegraf';
+$influx_url = 'http://10.13.37.92:8086/write?db=rot13';
+
+my $s = new Device::SerialPort( $port ) || die $!;
+$s->baudrate(9600);
+$s->databits(8);
+$s->parity('none');
+$s->stopbits(1);
+$s->handshake('none');
+$s->read_char_time(5);
+$s->read_const_time(10);
+
+
+while (1) {
+
+       my ($len, $string) = $s->read(9);
+       if ( $len > 0 ) {
+               my @v = unpack('C*', $string);
+               warn "# $len ",dump($string), dump( @v ), $/;
+               my $pcnt = $v[3] + ( $v[4] / 100 );
+               if ( $v[0] == 0xff ) {
+                       my $influx = "zph02,dc=trnjanska pm25_pcnt=$pcnt";
+                       print "$influx\n";
+                       system "curl --silent -XPOST '$influx_url' --data-binary '$influx'"
+               }
+       }
+
+};
+
+
+$s->close;