#!/usr/bin/perl -w use Modern::Perl; use Device::SerialPort::Arduino; use DBI; #use Proc::Daemon; use DateTime; use JSON; my $dbfile = "/var/www/data/weather_data.sqlt"; #use Data::Dumper; #my $dt = DateTime->now(); my ($key,$val, $q, $sth, $rec_cnt, $char, $json); my $debug = 0; #set to 1 if debug print statements are to be displayed my $delay = 29; #must be set to less than in the arduino rpt_ms/1000 ################################################################################## # Set up the serial port if ($debug) { say "Setting up serial port\n"; } my $dev = Device::SerialPort->new("/dev/ttyACM0") || warn "Can't open /dev/ttyACM0 $!"; my $profile = 0; # set to '1' when running NYTProfile. Limits run length... $dev->baudrate(115200); # you may change this value $dev->databits(8); # but not this and the two following $dev->parity("none"); $dev->stopbits(1); $dev->read_char_time( 0 ); # don't wait for each character $dev->read_const_time( 1000 ); # 1 second per unfulfilled "read" call $dev->read_const_time( 1500 ); # 1.5 seconds per unfulfilled "read" call ##################################################################################### while (1) { # Poll to see if any data is coming in if ($debug) { say "Entering polling...." } my $char = $dev->lookfor(); if ($char) { if ($debug == 1) { say "Raw input is -> $char"; } $rec_cnt ++; next if $rec_cnt == 1; # The first line incoming tends to be two lines combined, skip it. my $line = $char; chomp($line); $json = decode_json($line); process_data(\$json); # now process the competed incoming json object, inserting it into DB } if($debug == 1) { # give up its process resources until we need it. Change here if changed in Ardduino!! say "return after $delay seconds of sleeping\n"; } sleep($delay); # the arduino sends data every 30 second (currently). Let the Perl script } ####################################################################################### sub process_data { if ($debug) { say "Processing polled data...." } my $json_line = shift(@_); my $dt = DateTime->now(); $dt->set_time_zone( 'America/Chicago' ); my $sth; my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile" ,"",""); if ( (exists($json->{'bbl'})) and (not exists($json->{'ra'}))) { $q = "insert into weather_data values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; $sth = $dbh->prepare($q); $sth->execute(undef, $dt->datetime(), $dt->ymd(), $dt->hms(), $json->{'wd'}, $json->{'wv'}, $json->{'tF'}, $json->{'tC'}, $json->{'bp'}, $json->{'rh'}, $json->{'li'}, $json->{'ov'}, $json->{'lux'}, $json->{'bbl'}, $json->{'irl'}, 'null', 'null' ); } elsif ( (exists($json->{'ra'})) and (exists($json->{'bbl'}))) { $q = "insert into weather_data values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; $sth = $dbh->prepare($q); $sth->execute(undef,$dt->datetime(), $dt->ymd(), $dt->hms(), $json->{'wd'}, $json->{'wv'}, $json->{'tF'}, $json->{'tC'}, $json->{'bp'}, $json->{'rh'}, $json->{'li'}, $json->{'ov'}, $json->{'lux'}, $json->{'bbl'}, $json->{'irl'}, $json->{'ra'}, $json->{'rr'} ); } elsif ( (exists($json->{'ra'})) and (not exists($json->{'bbl'}))) { $q = "insert into weather_data values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; $sth = $dbh->prepare($q); $sth->execute(undef,$dt->datetime(), $dt->ymd(), $dt->hms(), $json->{'wd'}, $json->{'wv'}, $json->{'tF'}, $json->{'tC'}, $json->{'bp'}, $json->{'rh'}, $json->{'li'}, $json->{'ov'}, 'null', 'null', 'null', $json->{'ra'}, $json->{'rr'} ); } elsif ( (not exists($json->{'ra'})) and (not exists($json->{'bbl'}))) { $q = "insert into weather_data values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; $sth = $dbh->prepare($q); $sth->execute(undef,$dt->datetime(), $dt->ymd(), $dt->hms(), $json->{'wd'}, $json->{'wv'}, $json->{'tF'}, $json->{'tC'}, $json->{'bp'}, $json->{'rh'}, $json->{'li'}, $json->{'ov'}, 'null', 'null', 'null', 'null', 'null' ); } $sth->finish; $dbh->disconnect; }