better filter for sox output master
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 11 Jan 2020 08:20:39 +0000 (09:20 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 11 Jan 2020 08:20:39 +0000 (09:20 +0100)
README
audio2mqtt.sh [new file with mode: 0755]
datasheets/MH-Z19B.pdf [new file with mode: 0644]
dsm501.pl [new file with mode: 0755]
mh-z19b.pl [new file with mode: 0755]
zph02.pl

diff --git a/README b/README
index aa1c861..e54c333 100644 (file)
--- a/README
+++ b/README
@@ -1,6 +1,8 @@
 Experiments with air quality sensors
 
-Scripts in this directory talk to sensors and store data into influxdb.
+Scripts in this directory read from sesor serial port output (which
+is 3.3V serial connected to usb serial dongle) and store data to
+influxdb using curl.
 
 When receiving data from sensors, they check  header and checksum to
 avoid sending garbage data. This also helps when you select wrong
diff --git a/audio2mqtt.sh b/audio2mqtt.sh
new file mode 100755 (executable)
index 0000000..af23cbb
--- /dev/null
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+while true ; do
+
+arecord -d 1 -r 96000 /dev/shm/tmp.wav
+time=`date +'%s%N'`
+info=`sox /dev/shm/tmp.wav -n stat 2>&1 | grep '[0-9][0-9][0-9]*$' | sed -e 's/[()]//g' -e 's/: */=/' -e 's/  */_/g' | tr '\n' ',' | sed 's/[, ]*$//'`
+curl --silent -XPOST http://10.60.0.92:8086/write?db=rot13 --data-binary "fan,dc=trnjanska $info $time"
+
+done
diff --git a/datasheets/MH-Z19B.pdf b/datasheets/MH-Z19B.pdf
new file mode 100644 (file)
index 0000000..9b762ae
Binary files /dev/null and b/datasheets/MH-Z19B.pdf differ
diff --git a/dsm501.pl b/dsm501.pl
new file mode 100755 (executable)
index 0000000..8e183c8
--- /dev/null
+++ b/dsm501.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+# sudo apt install libdevice-serialport-perl libdata-dump-perl
+use Device::SerialPort;
+use Time::HiRes;
+use Data::Dump qw(dump);
+
+my $port = shift @ARGV || '/dev/ttyUSB2';
+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 $measurement = $ENV{MEASUREMENT} || 'dsm501';
+
+my $s = new Device::SerialPort( $port ) || die $!;
+$s->baudrate(115200);
+$s->databits(8);
+$s->parity('none');
+$s->stopbits(1);
+$s->handshake('none');
+$s->read_char_time(0); # don't wait for each char
+$s->read_const_time(200); # ms for complete read
+
+
+while (1) {
+
+       my ($len, $string) = $s->read(128);
+       my $t = int( Time::HiRes::time() * 1_000_000_000 );
+       die $! if ! defined($len);
+       if ( $len > 0 ) {
+               warn "# len=$len ",dump($string);
+               if ( $string !~ m/^#/ ) {
+                       $string =~ s/[\r\n]+$//;
+                       $string =~ s/\s/,/g;
+                       my $influx = "$measurement,dc=trnjanska $string $t";
+                       print "$influx\n";
+                       system "curl --silent -XPOST '$influx_url' --data-binary '$influx'"
+               } else {
+                       warn "SKIP: $string\n";
+               }
+       }
+
+};
+
+
+$s->close;
diff --git a/mh-z19b.pl b/mh-z19b.pl
new file mode 100755 (executable)
index 0000000..c925661
--- /dev/null
@@ -0,0 +1,55 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+# sudo apt install libdevice-serialport-perl libdata-dump-perl
+use Device::SerialPort;
+use Time::HiRes;
+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(0);
+$s->read_const_time(10);
+$s->debug(1);
+
+$s->write("\xFF\x01\x86\x00\x00\x00\x00\x00\x79");
+
+while (1) {
+
+       my ($len, $string) = $s->read(9);
+       my $t = int( Time::HiRes::time() * 1_000_000_000 );
+       die $! if ! defined($len);
+       if ( $len > 0 ) {
+               my @v = unpack('C*', $string);
+               #warn "# $len ",dump($string), dump( @v ), $/;
+
+               my $sum = 0;
+               foreach my $i ( 1 .. $#v - 1 ) {
+                       $sum += $v[$i];
+                       $sum = $sum & 0xff;
+               }
+               $sum = 0xff - $sum + 1;
+
+               my $checksum = $v[8];
+               my $co2 = $v[2] * 255 + $v[3];
+               if ( $v[0] == 0xff && $sum == $checksum ) {
+                       my $influx = "mh-z19b,dc=trnjanska co2=$co2 $t";
+                       print "$influx\n";
+                       system "curl --silent -XPOST '$influx_url' --data-binary '$influx'"
+               }
+               sleep 1;
+               $s->write("\xFF\x01\x86\x00\x00\x00\x00\x00\x79");
+       }
+
+};
+
+
+$s->close;
index d0797d9..aeaaffb 100755 (executable)
--- a/zph02.pl
+++ b/zph02.pl
@@ -6,7 +6,8 @@ use Device::SerialPort;
 use Time::HiRes;
 use Data::Dump qw(dump);
 
-my $port = shift @ARGV || '/dev/ttyUSB0';
+my $port = shift @ARGV || '/dev/ttyUSB1';
+$port = '/dev/serial/by-path/pci-0000:00:1a.7-usb-0:5.4:1.0-port0';
 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';