projects
/
air-quality
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
4c4ac1b
)
check checksum before sending data to influx
author
Dobrica Pavlinusic
<dpavlin@rot13.org>
Sun, 29 Sep 2019 15:02:50 +0000
(17:02 +0200)
committer
Dobrica Pavlinusic
<dpavlin@rot13.org>
Sun, 29 Sep 2019 15:02:50 +0000
(17:02 +0200)
zph02.pl
patch
|
blob
|
history
diff --git
a/zph02.pl
b/zph02.pl
index
50aadcb
..
4233660
100755
(executable)
--- a/
zph02.pl
+++ b/
zph02.pl
@@
-24,9
+24,19
@@
while (1) {
my ($len, $string) = $s->read(9);
if ( $len > 0 ) {
my @v = unpack('C*', $string);
my ($len, $string) = $s->read(9);
if ( $len > 0 ) {
my @v = unpack('C*', $string);
- warn "# $len ",dump($string), dump( @v ), $/;
+ #warn "# $len ",dump($string), dump( @v ), $/;
+
+ my $sum = 0;
+ # -2 is specified in datasheet, next byte is 0 so it's same as -1
+ foreach my $i ( 0 .. $#v - 2 ) {
+ $sum += $v[$i];
+ $sum = $sum & 0xff;
+ }
+ $sum = ~$sum & 0xff;
+
+ my $checksum = $v[8];
my $pcnt = $v[3] + ( $v[4] / 100 );
my $pcnt = $v[3] + ( $v[4] / 100 );
- if ( $v[0] == 0xff ) {
+ if ( $v[0] == 0xff
&& $sum == $checksum
) {
my $influx = "zph02,dc=trnjanska pm25_pcnt=$pcnt";
print "$influx\n";
system "curl --silent -XPOST '$influx_url' --data-binary '$influx'"
my $influx = "zph02,dc=trnjanska pm25_pcnt=$pcnt";
print "$influx\n";
system "curl --silent -XPOST '$influx_url' --data-binary '$influx'"