Average results to get a more stable measurment.
This commit is contained in:
parent
5e77e8dc0e
commit
7dfbd75566
1 changed files with 16 additions and 4 deletions
20
sendtemps.pl
Normal file → Executable file
20
sendtemps.pl
Normal file → Executable file
|
@ -17,7 +17,9 @@ my $sfua = LWP::UserAgent->new();
|
|||
$lbua->add_handler (
|
||||
'response_data',
|
||||
sub {
|
||||
state $last=0;
|
||||
state $last=0;
|
||||
state $total=0;
|
||||
state $count=0;
|
||||
my ($response, $ua, $h, $data) = @_;
|
||||
# Your chunk of response is now in $data, do what you need
|
||||
# If you plan on reading an infinite stream, it's a good idea to clean the response so it doesn't grow infinitely too!
|
||||
|
@ -27,14 +29,24 @@ $lbua->add_handler (
|
|||
return 1 if (!$data || $data eq 'data:');
|
||||
my $json=decode_json($data);
|
||||
|
||||
if ($json->{timestamp}/1000 - $last >= 10) {
|
||||
if ($json->{timestamp}/1000 - $last >= 60) {
|
||||
# say Dumper($json);
|
||||
my $ftemp = (($json->{absolute}/10 - 5.55)*1.8+32);
|
||||
say "FTEMP: $ftemp";
|
||||
$total+=$ftemp;
|
||||
$count++;
|
||||
my $atemp = eval {$total/$count} // 0;
|
||||
say "FTEMP: $atemp";
|
||||
my $uri = URI->new('https://data.sparkfun.com/input/xR418bzGQxTW7O7dbw8o');
|
||||
$uri->query_form(private_key => 'Zawd1n7RJNI7121KRmNX', temp=>$ftemp);
|
||||
$uri->query_form(private_key => 'Zawd1n7RJNI7121KRmNX', temp=>$atemp);
|
||||
my $res = $sfua->get($uri);
|
||||
$last = $json->{timestamp}/1000;
|
||||
|
||||
$total=0;
|
||||
$count=0;
|
||||
} else {
|
||||
my $ftemp = (($json->{absolute}/10 - 5.55)*1.8+32);
|
||||
$total+=$ftemp;
|
||||
$count++;
|
||||
}
|
||||
$response->content(undef);
|
||||
# Important to return a true value if you want to keep reading the response!
|
||||
|
|
Loading…
Add table
Reference in a new issue