61 lines
1.9 KiB
Perl
Executable file
61 lines
1.9 KiB
Perl
Executable file
#!/usr/bin/env perl
|
|
|
|
use v5.20;
|
|
use strict;
|
|
use warnings;
|
|
|
|
use Data::Dumper;
|
|
use LWP::UserAgent;
|
|
use JSON qw/decode_json/;
|
|
|
|
|
|
#curl -H 'Authorization: Bearer 764b1fd5a6845d04eda641d16991a95090ef9d33acec929f06f70cf4f91c734d' https://api-http.littlebitscloud.cc/v2/devices/243c200e2de4/input
|
|
|
|
my $lbua = LWP::UserAgent->new();
|
|
my $sfua = LWP::UserAgent->new();
|
|
|
|
$lbua->add_handler (
|
|
'response_data',
|
|
sub {
|
|
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!
|
|
|
|
$data =~ s/[\r\n]//g;
|
|
|
|
return 1 if (!$data || $data eq 'data:');
|
|
my $json=decode_json($data);
|
|
|
|
if ($json->{timestamp}/1000 - $last >= 60) {
|
|
# say Dumper($json);
|
|
my $ftemp = (($json->{absolute}/10 - 5.55)*1.8+32);
|
|
$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=>$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!
|
|
return 1;
|
|
},
|
|
);
|
|
|
|
my $req = HTTP::Request->new(GET => 'https://api-http.littlebitscloud.cc/v2/devices/243c200e2de4/input');
|
|
$req->header(Authorization => 'Bearer 764b1fd5a6845d04eda641d16991a95090ef9d33acec929f06f70cf4f91c734d');
|
|
|
|
$lbua->request($req);
|
|
|