]>
Git — Sourcephile - julm/air-duino.git/blob - collect.pl
2 # Licence: WTFPLv2 <http://www.wtfpl.net/txt/copying/>
3 # Copyright 2015: Julien Moutinho <julm+air@autogeree.net>
7 use DateTime
::Duration
;
9 use Device
::SerialPort
qw( 0.07 );
13 use Log
::Log4perl
qw(:easy);
15 #use Data::Dumper qw(Dumper);
17 my $step = 30; # NOTE: sampling interval in seconds
20 Log
::Log4perl-
>easy_init(
22 , category
=> 'rrdtool'
27 ( time_zone
=> 'local'
28 #, locale => $config{locale}
29 ); # ->set_time_zone('floating');
30 my $dir = File
::Spec-
>catdir(dirname
($0), 'rrd', (sprintf '%0d', $now->year()));
31 my $file = File
::Spec-
>catfile($dir, (sprintf '%02d.rrd', $now->month()));
32 File
::Path
::make_path
($dir);
33 my $rrd = RRDTool
::OO-
>new(file
=> $file);
35 my $one_month = DateTime
::Duration-
>new(months
=> 1, end_of_month
=> 'limit');
36 my $month_begin = $now->clone->truncate(to
=> 'month');
37 my $month_end = $month_begin->clone->add_duration($one_month);
38 my $month_seconds = $month_end->epoch() - $month_begin->epoch();
46 , heartbeat
=> 2 * $step # seconds
49 { name
=> "temperature"
53 , heartbeat
=> 2 * $step # seconds
60 , heartbeat
=> 2 * $step # seconds
67 , heartbeat
=> 2 * $step # seconds
72 , rows
=> $month_seconds / $step
73 , xff
=> 0.99 # ignore unknowns values (U)
81 my $dev = Device
::SerialPort-
>new($file);
82 $dev->baudrate(9600); # MUST: match *uino Serial.begin()
83 #$dev->buffers(4096, 4096); # NOTE: no-op on POSIX
85 $dev->dtr_active(1); # NOTE: reset the *uino on serial connection
86 $dev->handshake('none');
88 $dev->read_char_time(0); # NOTE: don't wait for each character
89 $dev->read_const_time(1000); # NOTE: 1 second per unfulfilled "read" call
97 my $dev = dev_init
($ARGV[0]);
100 my $read_timeout = 60;
101 my $timeout = $read_timeout;
106 my $curr_day = (localtime)[3];
107 if ($curr_day == 1) {
108 # NOTE: month changed, change RRD
109 # FIXME: avoid to check that every second...
112 my ($count, $saw) = $dev->read(1);
113 # NOTE: this could have read up to 255 chars (max for portability)
114 # but reading only 1 char at a time
115 # enables to add a more accurate timestamp.
116 if (defined $count and $count > 0) {
118 my @lines = split /\r\n/, $buffer;
121 # NOTE: process only ended lines
122 $buffer = pop @lines;
124 my ($counter, $uptime, $humidity, $temperature, $quality, $particles)
125 = $_ =~ m/^([0-9]+);([0-9]+);([0-9]+.[0-9]+);([0-9]+.[0-9]+);([0-9]+);([0-9]+.[0-9]+)$/;
126 if (defined $particles) {
127 #print STDOUT ($time, ";", $_, "\n");
128 $collect[0] = $humidity if $humidity ne '';
129 $collect[1] = $temperature if $temperature ne '';
130 $collect[2] = $quality if $quality ne '';
131 $collect[3] = $particles if $particles ne '';
132 if ($time >= $last_time + $step && @collect == 4) {
135 , values => [join (':', @collect)]
142 print STDERR
($_, "\n");
146 $timeout = $read_timeout;
153 die "Connection timeout (after ${read_timeout}s)\n";