-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscover_emonTH.pl
executable file
·65 lines (54 loc) · 1.93 KB
/
discover_emonTH.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
59
60
61
62
63
64
65
#!/usr/bin/perl
# Discover and synchronize (=read) emonTH sensor input from RFM69Pi
# The script will store the retrieved sensor input values into a cache file read by read_emonTH.pl
# The discovery will return the following keys for each discovered item:
# {#NODE}: Discovered node number
use Storable;
use File::Find;
# Read a line from our message queue
chomp(my $line = `echo 'RPOP emonTH' | redis-cli`);
# Read the message queue until it's empty
while (defined $line && $line ne "")
{
my ($node, $i1, $i2, $i3, $i4, $i5, $i6, $i7, $i8, $rssi) = $line =~ /^OK (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) \((-?\d+)\)$/;
if ($node && $rssi) {
# Convert to SI units
my $internal_temperature = ($i2 * 256 + $i1) / 10.0;
my $external_temperature = ($i4 * 256 + $i3) / 10.0;
my $internal_humidity = ($i6 * 256 + $i5) / 10.0;
my $battery_voltage = ($i8 * 256 + $i7) / 10.0;
my %hash = (
node => "$node",
internal_temperature => "$internal_temperature",
external_temperature => "$external_temperature",
internal_humidity => "$internal_humidity",
battery_voltage => "$battery_voltage",
rssi => "$rssi"
);
# print "internal temperature $hash{internal_temperature}\n";
# print "external temperature $hash{external_temperature}\n";
# print "internal humidity $hash{internal_humidity}\n";
# print "battery voltage $hash{battery_voltage}\n";
# Write the latest measurement to a file
my $filename = "/tmp/$node.emonTH";
store(\%hash, "$filename") or die "Could not write to file $filename";
}
chomp($line = `echo 'RPOP emonTH' | redis-cli`);
}
# Finally discover all the nodes that have been transmitting data
print "{\"data\":[";
$first = 1;
find(\&discover, "/tmp");
print "]}";
exit(0);
sub discover
{
my $file = $_;
my ($node) = $file =~ /^(\d+).emonTH$/;
if ($node)
{
print "," if not $first;
$first = 0;
print "{ \"{#NODE}\":\"$node\"}";
}
}