]>
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);
19 Log
::Log4perl-
>easy_init(
21 , category
=> 'rrdtool'
26 ( time_zone
=> 'local'
27 #, locale => $config{locale}
28 ); # ->set_time_zone('floating');
29 my $dir = File
::Spec-
>catdir(dirname
($0), 'rrd', (sprintf '%0d', $now->year()));
30 my $file = File
::Spec-
>catfile($dir, (sprintf '%02d.rrd', $now->month()));
31 File
::Path
::make_path
($dir);
32 my $rrd = RRDTool
::OO-
>new(file
=> $file);
34 my $one_month = DateTime
::Duration-
>new(months
=> 1, end_of_month
=> 'limit');
35 my $month_begin = $now->clone->truncate(to
=> 'month');
36 my $month_end = $month_begin->clone->add_duration($one_month);
37 my $month_seconds = $month_end->epoch() - $month_begin->epoch();
38 my $step = 30; # NOTE: sampling interval in seconds
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
80 my $dev = Device
::SerialPort-
>new($file);
81 $dev->baudrate(9600); # MUST: match *uino Serial.begin()
82 #$dev->buffers(4096, 4096); # NOTE: no-op on POSIX
84 $dev->dtr_active(1); # NOTE: reset the *uino on serial connection
85 $dev->handshake('none');
87 $dev->read_char_time(0); # NOTE: don't wait for each character
88 $dev->read_const_time(1000); # NOTE: 1 second per unfulfilled "read" call
91 $dev->lookclear; # NOTE: discard buffered data within the TTY device
97 my $dev = dev_init
($ARGV[0]);
100 my $read_timeout = 60;
101 my $timeout = $read_timeout;
104 my $last_day = (localtime)[3];
106 my $curr_day = (localtime)[3];
107 if ($curr_day < $last_day) {
108 # NOTE: month changed, change RRD
110 $last_day = $curr_day;
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.
118 my @lines = split /\r\n/, $buffer;
121 # NOTE: process only ended lines
122 $buffer = pop @lines;
125 #print STDOUT ($time, ";", $_, "\n");
126 my ($counter, $uptime, $humidity, $temperature, $quality, $particules)
128 if ($time > $last_time) {
131 , values => [$humidity,$temperature,$quality,$particules]
137 print STDERR
($_, "\n");
141 $timeout = $read_timeout;
148 die "Connection timeout (after ${read_timeout}s)\n";