summaryrefslogtreecommitdiffstats
path: root/prometheus/home/httpd/html/weatherstation/updateweatherstation.pl
diff options
context:
space:
mode:
Diffstat (limited to 'prometheus/home/httpd/html/weatherstation/updateweatherstation.pl')
-rwxr-xr-xprometheus/home/httpd/html/weatherstation/updateweatherstation.pl83
1 files changed, 83 insertions, 0 deletions
diff --git a/prometheus/home/httpd/html/weatherstation/updateweatherstation.pl b/prometheus/home/httpd/html/weatherstation/updateweatherstation.pl
new file mode 100755
index 0000000..87b8893
--- /dev/null
+++ b/prometheus/home/httpd/html/weatherstation/updateweatherstation.pl
@@ -0,0 +1,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 );
+