-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathklimalogg.pl
executable file
·61 lines (48 loc) · 1.88 KB
/
klimalogg.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/perl -w
use strict;
use IO::Socket;
use DBI;
use DBD::Pg;
my $dbh = DBI->connect("dbi:Pg:dbname=sensors");
$dbh->do("set application_name to 'klimalogg.pl'");
my $sth = $dbh->prepare('INSERT INTO klimalogg(hygro, temp, raw) VALUES (?,?,?)');
my $server = IO::Socket::INET->new(LocalPort => 35000,
Proto => "udp")
or die "Couldn't be a UDP server: $@\n";
sub bin2dec ($){
return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
}
while (my$sender = $server->recv($_, 1500)) {
y/\x00\x01/01/;
print STDERR "$_\n";
foreach my$telegram (/(?:01){5,10}1101111(.{168})1(?:01){20}0{10}/g) {
$telegram =~ s/1([01])/$1/g;
my ($prefix, $id, $bcd0, $bcd1, $bcd2, $bcd3, $hygro,
$unk, $count, $sum) =
$telegram =~ m/^([01]{12}) # prefix
([01]{16}) # id
([01]{4}) # bcd0
([01]{4}) # bcd1
([01]{4}) # bcd2
([01]{4}) # bcd3
([01]{8}) # hygro
([01]{12}) # unk
([01]{4}) # count
01101010
([01]{8}) # sum/x;
next unless defined($count);
# print $telegram . "\n";
print "decoded: hygro=" . bin2dec(reverse("$hygro")) . "%";
my $temp = (bin2dec(reverse("$bcd0")) - 4) * 10 +
(bin2dec(reverse("$bcd3"))) + 0.1 * (bin2dec(reverse("$bcd2")));
printf " temp=%02.1f°C" , $temp;
printf " count=%x" , bin2dec(reverse("$count"));
printf " id=%x" , bin2dec(reverse("$id"));
printf " sum=%02x" , bin2dec(reverse("$sum"));
printf " unk=%x" , bin2dec(reverse("$unk"));
printf " bcd1=%x" , bin2dec(reverse("$bcd1"));
printf " prefix=%x" , bin2dec(reverse("$prefix"));
print "\n";
$sth->execute(bin2dec(reverse("$hygro")), $temp, $telegram)
}
}