send all readings as single line to influx
[air-quality] / iio2mqtt.pl
index a9dce9f..1879ce1 100755 (executable)
@@ -6,18 +6,22 @@ use Time::HiRes;
 
 # sudo apt install libiio-utils mosquitto-clients
 
 
 # sudo apt install libiio-utils mosquitto-clients
 
+my $influx_url = shift @ARGV || 'http://10.13.37.92:8086/write?consistency=any&db=rot13';
+
 my $hostname = `hostname -s`;
 chomp($hostname);
 
 while(1) {
 
        my $t = Time::HiRes::time;
 my $hostname = `hostname -s`;
 chomp($hostname);
 
 while(1) {
 
        my $t = Time::HiRes::time;
+       my $t_influx = int( $t * 1_000_000_000 );
 
        my $iio = `iio_info`;
 
        my $device;
        my $name;
 
 
        my $iio = `iio_info`;
 
        my $device;
        my $name;
 
+       my @influx;
        foreach ( split(/\n/, $iio) ) {
                if ( m/iio:device\d+:\s+(\S+)/ ) {
                        $device = $1;
        foreach ( split(/\n/, $iio) ) {
                if ( m/iio:device\d+:\s+(\S+)/ ) {
                        $device = $1;
@@ -31,10 +35,14 @@ while(1) {
                        my $topic = "iio/$hostname/$device/$name";
                        #print "$topic $val\n";
                        system "mosquitto_pub -h rpi2 -t $topic -m $val";
                        my $topic = "iio/$hostname/$device/$name";
                        #print "$topic $val\n";
                        system "mosquitto_pub -h rpi2 -t $topic -m $val";
+
+                       push @influx, "${device}_${name}=$val";
                } else {
                        #warn "# $_\n";
                }
        }
                } else {
                        #warn "# $_\n";
                }
        }
+       my $influx = "iio,dc=trnjanska,host=$hostname " . join(",", @influx) . " $t_influx";
+       system "curl --silent -XPOST '$influx_url' --data-binary '$influx'";
 
        sleep Time::HiRes::time + 1 - $t;
 }
 
        sleep Time::HiRes::time + 1 - $t;
 }