support multiple devices
[air-quality] / iio2mqtt.pl
index 4081540..e9ba122 100755 (executable)
@@ -21,9 +21,17 @@ 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/;
+                       }
+                       $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 +43,15 @@ 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";
                }
        }
 
+       $influx =~ s/,$/ $t_influx/;
+       system "curl --silent -XPOST '$influx_url' --data-binary '$influx'";
+       warn "$influx\n";
+
        sleep Time::HiRes::time + 1 - $t;
 }