Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Good advice. I will incorporate those suggestions. Thank you!

UPDATE: Thanks again for the feedback! I did run through to check on those things you noticed. As IFAIK, the repeated statements all only happen once the $dev->lookfor() completes. That said, looking again made me realize that the sleep(1) which I ignored even after looking at how sleep worked( could not determine if it reduced allocated resourced on the system or not), could be changed to 14 seconds as long as the timeout for reading character was increased a bit. I also moved all the processing of getting things into the DB into its own sub. The result is that the increase in sleep time reduced cpu system resources from the aforementioned 1.27 down to less between .23 to .37 on a 1 minute average and 0.4 on a 10 minute average, and that is with htop running and two ssh sessions going. So thanks again! /huck for taking the time!

Update 1: Figured I would repost the code that I ended up here for posterity sake, and maybe helpful to someone else...

#!/usr/bin/perl -w use Modern::Perl; use Device::SerialPort::Arduino; use DBI; #use Proc::Daemon; use DateTime; use JSON; my $dbfile = "/home/wjw/weather_data"; my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile" ,"",""); my ($key,$val, $q, $sth, $rec_cnt, $char, $timestamp, $json); #################################### # Set up the serial port my $dev = Device::SerialPort->new("/dev/ttyUSB0") || warn "Can't open +/dev/ttyUSB0 $!"; my $debug = 0; #set to 1 if debug print statements +are to be displayed 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( 1500 ); # 1 second per unfulfilled "read" call #################################### while (1) { #Proc::Daemon::Init; #my $continue = 1; #$SIG{TERM} = sub { $continue = 0 }; #while ($continue) { # Poll to see if any data is coming in 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 b +e two lines combined, skip it. my $line = $char; chomp($line); if ($debug == 1) { say $line; } $json = decode_json($line); process_data(\$json); # now process the competed inc +oming json object, inserting it into DB } sleep(14); # the arduino sends data every 1 +5 second (currently). Let the Perl script if($debug ==1) { # give up its process resources un +til we need it. Change here if changed in Ardduino!! say "return after 14 seconds of sleeping"; } } ################################ sub uts_to_iso { my $uts = shift; my $date = DateTime->from_epoch(epoch => $uts, time_zone => 'Ameri +ca/Chicago'); return $date->ymd().'T'.$date->hms().'Z'; } ################################ sub dt { my $request = shift(@_); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me(); if ($request =~ /date/) { $year = $year + 1900; return "$year" . "-" . "$mon" . "-" . "$mday"; } if ($request =~ /time/) { return "$hour" . ":" . "$min" . ":" . "$sec"; } } ################################# sub process_data { my $json_line = shift(@_); $timestamp = uts_to_iso(time()); if ( (exists($json->{'bbl'})) and (not exists($json->{'ra'}))) { $q = "insert into weather_data values (?, ?, ?, ?, ?, ?, ?, ?, + ?, ?, ?, ?, ?, ?, ?, ?, ?)"; my $sth = $dbh->prepare($q); $sth->execute(undef, $timestamp, dt('date'), dt('time'), $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,$timestamp, dt('date'), dt('time'), $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,$timestamp, dt('date'), dt('time'), $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,$timestamp, dt('date'), dt('time'), $json- +>{'wd'}, $json->{'wv'}, $json->{'tF'}, $json->{'tC'}, $json->{'bp'}, +$json->{'rh'}, $json->{'li'}, $json->{'ov'}, 'null', 'null', 'null', +'null', 'null' ); } };

...the majority is always wrong, and always the last to know about it...

A solution is nothing more than a clearly stated problem...


In reply to Re^2: Waiting for Device::Serialport by wjw
in thread Waiting for Device::Serialport by wjw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (3)
    As of 2021-01-21 01:37 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?