X-Git-Url: http://git.rot13.org/?p=air-quality;a=blobdiff_plain;f=iio2mqtt.pl;h=855e8ca215660b68075f0e88a17cc2e97d5c8df4;hp=40815400627b94709c4894f2cce456128d198dc8;hb=a62e5f83f1b7e8830c258771a7601146e115997f;hpb=3891fbda4dc76328c3c53d890609cb1b31070359 diff --git a/iio2mqtt.pl b/iio2mqtt.pl index 4081540..855e8ca 100755 --- a/iio2mqtt.pl +++ b/iio2mqtt.pl @@ -6,7 +6,9 @@ use Time::HiRes; # sudo apt install libiio-utils mosquitto-clients -my $influx_url = shift @ARGV || 'http://10.13.37.92:8086/write?consistency=any&db=rot13'; +my $influx_url = shift @ARGV || 'http://10.60.0.92:8086/write?consistency=any&db=rot13'; + +my $delay = $ENV{DELAY} || 1; my $hostname = `hostname -s`; chomp($hostname); @@ -21,9 +23,19 @@ while(1) { my $device; my $name; + my $influx = ''; + foreach ( split(/\n/, $iio) ) { if ( m/iio:device\d+:\s+(\S+)/ ) { $device = $1; + + if ( $influx =~ m/,$/ ) { + $influx =~ s/,$/ $t_influx\n/; + } elsif ( $influx =~ m/ $/ ) { # only device + $influx =~ s/\S+\s$//; + } + $influx .= "iio,dc=trnjanska,host=$hostname,device=$device "; + } elsif ( m/(\S+):\s+\(input\)/ ) { $name = $1; } elsif ( m/attr\s+0:\s+input\svalue: (\d+[\.\d]+)/ ) { @@ -35,12 +47,18 @@ while(1) { #print "$topic $val\n"; system "mosquitto_pub -h rpi2 -t $topic -m $val"; - my $influx = "iio,dc=trnjanska,host=$hostname,device=$device $name=$val $t_influx"; - system "curl --silent -XPOST '$influx_url' --data-binary '$influx'"; + $influx .= "$name=$val,"; } else { #warn "# $_\n"; } } - sleep Time::HiRes::time + 1 - $t; + $influx =~ s/,$/ $t_influx/; + system "curl --silent -XPOST '$influx_url' --data-binary '$influx'"; + warn "$influx\n"; + + my $dt = Time::HiRes::time + $delay - $t - 100; + if ( $dt > 0 ) { + sleep $dt; + } }