Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
P is for Practical
 
PerlMonks  

Measure temperature outside in Perl

by chessgui (Scribe)
on Feb 04, 2012 at 09:37 UTC ( #951782=perlquestion: print w/ replies, xml ) Need Help??
chessgui has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

Is there a module, a sub, a regular expression or put it simply: a WAY to measure the tempreture outside the room in Perl.

My code presently:
my $temperature=what? (i mean outside);
I'm receiving the following error:
syntax error at temp.pl line 1, near ");" Execution of temp.pl aborted due to compilation errors.
What to do now?

Comment on Measure temperature outside in Perl
Select or Download Code
Re: Measure temperature outside in Perl
by Anonymous Monk on Feb 04, 2012 at 09:49 UTC

    What room?

    weather? Weather::Google? Yahoo::Weather? WWW::Wunderground::API?

    #!/usr/bin/perl -- use strict; use warnings; use Path::Class; use DateTime; use LWP::Simple qw' $ua getstore '; our $here = file(__FILE__)->absolute->dir; chdir $here; $ua->show_progress(1); my $now = DateTime->now( qw[ time_zone local ] )->strftime('%F-%H-%M-% +S%z'); my $file = "$now-LA.rss"; my $url = 'https://www.wunderground.com/auto/rss_full/CA/Los_Angeles.x +ml?units=english'; for(1..3){ getstore( $url, $file ); last if -e $file and -s _; } print "#~ $file "; use XML::XPath; use XML::XPath::XMLParser; my $xp = XML::XPath->new(filename => $file); print $xp->findvalue('//item[1]/title'); print " $1 " if $xp->findvalue('//item[1]/description') =~ /(Humidity[ +^\|]+)/; print "\n"; __END__ ** GET https://www.wunderground.com/auto/rss_full/CA/Los_Angeles.xml?u +nits=english ==> 500 Can't connect to www.wunderground.com:443 (Bad h +ostname) (2s) ** GET https://www.wunderground.com/auto/rss_full/CA/Los_Angeles.xml?u +nits=english ==> 200 OK #~ 2012-02-04-01-53-41-0800-LA.rss Current Conditions : 61.5F, Clear + - 1:47 AM PST Feb. 4 Humidity: 27%
Re: Measure temperature outside in Perl
by choroba (Abbot) on Feb 04, 2012 at 09:58 UTC
    Yes, Perl philosophy is "Do what I mean", but the technology is not yet advanced enough to understand your syntax (you'd have gotten "i should be capitalized" warning anyway). As Google shows, there are some USB thermometers around, so you might use one if you do not want to get the information from the web, as suggested by Anonymous Monk.
Re: Measure temperature outside in Perl
by McDarren (Abbot) on Feb 04, 2012 at 09:59 UTC
    Is there a module, a sub, a regular expression or put it simply: a WAY to measure the tempreture outside the room in Perl.
    Of course not.
    To measure temperature you need a device that's designed for measuring temperature. Something like, oh, a Thermometer for example.
    Now if you had an electronic thermometer and you wanted to process the data it produces... well that's a completely different question.
    I'm receiving the following error:
    Yes, well as they say.. you can't just make stuff up and expect the computer to know what you mean.
Re: Measure temperature outside in Perl
by tobyink (Abbot) on Feb 04, 2012 at 10:31 UTC
    use 5.010; use strict; use Web::Magic 0.007; my %opts = ( 'w' => 26191, # Yahoo WOEID 'u' => 'c', # Unit: 'c' or 'f' ); my ($temperature) = Web::Magic -> new(q<http://weather.yahooapis.com/forecastrss>, %opts) -> findnodes('//yweather:condition/@temp') -> map(sub { $_->value }); say "Temperature is $temperature";

    Works for me!

    Update: Yahoo WOEIDs (Where On Earth Identifier) are numeric identifiers used by Yahoo for places. Here's a tool to look up WOEIDs. The WOEID in the script above is for Lewes, East Sussex.

Re: Measure temperature outside in Perl
by marto (Chancellor) on Feb 04, 2012 at 10:36 UTC
    Until scientists invent magic you're going to have to resort to one of the more traditional methods for measuring things. If by 'outside the room' you mean out in the open, there are services you can query to find the approximate temperature. Other options include building a wireless temperature sensor which you can query from your code. Think arduino + wireless module.
Re: Measure temperature outside in Perl
by Anonymous Monk on Feb 04, 2012 at 11:25 UTC

    What to do now?

    Sacrifice the queen!

Re: Measure temperature outside in Perl
by iguanodon (Curate) on Feb 04, 2012 at 13:52 UTC
    If you're looking for something to hack on, see:

    1wire
    OWFS
    DigiTemp

    None of this requires Perl, but you could for example write some Perl code to read from sensors using DigiTemp or OWFS, store in a database, create graphs...

Re: Measure temperature outside in Perl
by oko1 (Deacon) on Feb 04, 2012 at 22:48 UTC

    Now that you've *asked*, I *suppose* we'll have to tell you The Secret. It's all part of the Conspiracy (but There Is No Conspiracy, of course.)

    # Perl itself actually measures the computer's temperature, # subtracts CPU temp, extrapolates the location of the closest # window or door, catches the temp whenever it was last opened, # and uses a heterogenous mean deteriorating sliding semi- # automated average to compute the current temp +-0.0001 degrees. # All part of DWIM, of course. my $leonard_of_quirm_var = perl->secret_built-in_outside-the-room-temp +erature-measuring-device(); print $leonard_of_quirm_var;

    Don't tell anyone else, 'K?

    -- 
    Education is not the filling of a pail, but the lighting of a fire.
     -- W. B. Yeats
      Converting to Celsius or Fahrenheit based on your locale since 5.8.4.

        Converting to Celsius or Fahrenheit based on your locale since 5.8.4.

        Remember you can use the special variable $^F to control this. If set to 0, uses Celsius; if set to 1, uses Fahrenheit; by default it's set to 2 which auto-detects based on your locale. If you don't believe me (and I have no idea why you wouldn't) then run the following on a sufficiently modern perl:

        perl -E'say $^F'

        I hear that 5.16 is going to modularise temperature scales so that you can download custom ones from CPAN, and come bundled with an additional scale for Kelvin.

         

        Fahrenheit is a funny scale. The Celsius scale is, as everybody knows, based on the freezing and boiling points of water. The freezing point was designated 0; the boiling point 100; and everything in between was divided equally into a hundred degrees. (Although named after Anders Celsius, his scale actually labelled the boiling point 0, and the freezing point 100. The modern Celsius scale was developed after his death and named in his honour.)

        Initially I thought Daniel Gabriel Fahrenheit must have followed a similar process but with a different liquid. After all, its zero doesn't seem to bear any relation to water.

        But actually it turns out he used water after all. However, rather than dividing it into a scale of 100 parts, he used 180, and rather than defining the freezing point of water to be zero, he defined it as 32, because he knew he could achieve a lower zero by mixing water, salt and ammonium.

        This is why, unlike miles versus kilometres, or pounds versus kilograms, you can use a simple formula to convert precisely between Celsius and Fahrenheit.

        $f = 1.8 * $c + 32;

        And it gives you an exact conversion (within the bounds of floating point arithmetic).

        For fun, see what happens when you plug $c = -40 into that.

Re: Measure temperature outside in Perl
by Anonymous Monk on Feb 06, 2012 at 04:08 UTC

    I've done this by using Perl to parse the output of open2300 but I've got a weather station sitting in my yard so your mileage may vary.

Re: Measure temperature outside in Perl
by tobyink (Abbot) on Feb 06, 2012 at 14:02 UTC

    Mwahahahaha! It can be done!

    use 5.010; use Acme::UseStrict; use Acme::What; use Web::Magic; sub WHAT { 'use strict'; my @args = $_[0] =~ m{\w+}g; my $woeid = pop @args; $woeid = do { warn "If you want to know the weather $woeid, " . "define the YAHOO_WOEID environment variable. " . "Assuming you live where TOBYINK lives" unless exists $ENV{YAHOO_WOEID} && defined $ENV{YAHOO_WOEID}; ($ENV{YAHOO_WOEID} // 26191) } if $woeid =~ m{^(outside|here)$}i; my $unit = $^F==1 ? 'F' : 'C'; # if $^F == 2 then detect unit by locale. Belize and US use Fahren +heit. if ($^F > 1 and $ENV{LC_MEASUREMENT} =~ /^.._(US|BZ)/i) { $unit = 'F'; } my %opts = ( 'w' => $woeid, 'u' => lc $unit, ); my ($temperature) = Web::Magic -> new(q<http://weather.yahooapis.com/forecastrss>, %opts) -> findnodes('//yweather:condition/@temp') -> map(sub { $_->value }); return "$temperature $unit"; } local $^F = 1; my $temperature=what? (i mean outside); say "The temperature is $temperature";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://951782]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-04-17 07:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (440 votes), past polls