summaryrefslogtreecommitdiffstats
path: root/prometheus/home/httpd/html/weatherstation/updateweatherstation.pl
blob: 87b889353da5e848a1e0d23188bd298ac1bd85d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/perl

#use CGI qw(:standard);
#use Sys::Syslog qw(:standard :macros);
use Net::MQTT::Simple;
use IO::Socket;

#openlog("weather", "", "local0");

my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
  gmtime(time);

my @mons = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my @days = qw(Sun Mon Tue Wed Thr Fri Sat);

my $dt = sprintf(
    "%s, %02d %s %04d %02d:%02d:%02d GMT",
    $days[$wday], $mday, $mons[$mon], 1900 + $year,
    $hour, $min, $sec
);

print "Content-Type: text/plain\r\n";
print "Date: ", $dt, "\n";
print "\n";
print "success\n";

my $mqtt = Net::MQTT::Simple->new("10.32.139.1");

for my $t ( split( /&/, $ENV{'QUERY_STRING'} ) ) {
    $t =~ s/%20/ /g;

    next unless $t =~ /^([^=]*)=([^=]*)$/;

    my ( $wot, $value ) = ( $1, $2 );

    if ( $wot =~ /f$/ ) {
        $wot =~ s/f$/c/;
        $value = sprintf( "%.1f", 5.0 * ( ( $value - 32.0 ) / 9.0 ) );
    }
    elsif ( $wot eq "baromin" ) {
        $wot   = "barommb";
        $value = sprintf( "%.1f", $value * 33.8639 );
    }
    elsif ( $wot =~ /in$/ ) {
        $wot =~ s/in$/mm/;
        $value = sprintf( "%.1f", $value * 25.4 );
    }

    if ( $wot eq "baromin" ) {
        $wot   = "barommb";
        $value = sprintf( "%.1f", $value * 33.8639 );
    }

    if ( $wot eq "tempc" ) {
        $mqtt->retain( "tele/weather/" . $wot, $value );
    }
    else {
        $mqtt->publish( "tele/weather/" . $wot, $value );
    }

    #syslog(LOG_WARNING,"fish: $wot => $value");
}

$mqtt->tick(0);
$mqtt->disconnect;

alarm(5);

my $sock = IO::Socket::INET->new(
    PeerAddr => "88.96.137.6",
    PeerPort => 30956,
    Proto    => 'tcp',
    Timeout  => 5
);

exit 0 unless defined $sock;

$sock->print( "GET /weatherstation/updateweatherstation.php?"
      . $ENV{'QUERY_STRING'}
      . " HTTP/1.0\r\n" );
$sock->print("\r\n");
$sock->shutdown( $socket, 1 );