4 # sudo apt install libdevice-serialport-perl libdata-dump-perl
5 use Device::SerialPort;
7 use Data::Dump qw(dump);
9 my $port = shift @ARGV || '/dev/serial/by-path/platform-3f980000.usb-usb-0:1.4.2:1.0';
10 my $influx_url = shift @ARGV || 'http://10.13.37.92:8086/write?consistency=any&db=rot13';
12 my $s = new Device::SerialPort( $port ) || die $!;
17 $s->handshake('none');
18 $s->read_char_time(5);
19 $s->read_const_time(10);
25 # Usb serial which I'm using is buggy and blocks from time to time.
26 # This will ensure that we have passed here every 3 seconds
27 # or we will be killed and systemd will restart us
29 my ($len, $string) = $s->read(9);
30 my $t = int( Time::HiRes::time() * 1_000_000_000 );
31 die $! if ! defined($len);
33 my @v = unpack('C*', $string);
34 #warn "# $len ",dump($string), dump( @v ), $/;
37 # -2 is specified in datasheet, next byte is 0 so it's same as -1
38 foreach my $i ( 0 .. $#v - 2 ) {
45 my $pcnt = $v[3] + ( $v[4] / 100 );
46 if ( $v[0] == 0xff && $sum == $checksum ) {
47 my $influx = "zph02,dc=trnjanska pm25_pcnt=$pcnt $t";
49 system "curl --silent -XPOST '$influx_url' --data-binary '$influx'"