I decided to have a quick go at a bit of simple weather compensation for my heating control system. Rather than adding it into my server software I decided to write a simple perl script that acts as a client.
First thing is to scrape the temperature from wunderground.com. Using lynx and getting the text in perl is easy.
Then for getting the programs into the server, I considered various complicated options, but then I thought of having a couple of pre-cooked programs based on temperature ranges. So in my script I determine which of 3 ranges it is in, currently less than 3 degrees, 3-9, 9-13 or 13+ and send a set of commands to the server based on the outcome.
use strict;
use warnings;
use Net::Telnet;
use Data::Dumper;
my $host = "localhost";
my $port = 2349;
$|=1;
my $command = "lynx --dump http://www.wunderground.com/q/zmw:00000.1.WEGAE";
print "Retrieve weather data...";
my $current_weather = `$command`;
print "\n";
#use the feels like temperature
# Feels Like 12 °C
# Feels Like 12.5 °C
my $temp = -99;
if(($temp) = $current_weather =~ /Feels Like\s+(.*?)\s+/){
print "Temperature is $temp\n";
if($temp > 13){
print "temp too high, doing nothing\n";
} elsif ($temp > 9) {
#warmest programs
send_programs("p_warm.txt");
} elsif ($temp > 3) {
#medium
send_programs("p_medium.txt");
} else {
#coldest temp program
send_programs("p_cold.txt");
}
}
sub send_programs{
my ($file) = @_;
print "About to use data from file $file \n";
my $t = new Net::Telnet( Timeout => 5, Dump_Log => "dump.log.filename.txt" );
$t->open( Host => $host, Port => $port );
$t->put("ClientName Perl Weather Compensation ($temp)\n");
open(IN, $file) || die "cant read file $file : $!";
while(my $line = <IN>)
{
chomp $line;
#replace multiple spaces with singles
$line=~s/\s+/ /g;
print "send: $line\n";
$t->put("$line\n");
}
}
No comments:
Post a Comment