#!/usr/bin/perl -w ### # ver 0.5 25 october 2023 Added retry to connect to sqm-le if connection failed # hansi@htk.se use IO::Socket; use Astro::Coord::ECI; use Astro::Coord::ECI::Sun; use Astro::Coord::ECI::TLE; use Astro::Coord::ECI::Utils qw{ :all }; use LWP::Simple qw(get); $|=1; # Make STDOUT immediate (non-buffered) my $sqm_ip_addres = '192.168.10.130'; # IP address of your SQM-LE my $sqm_port = 10001; # port of your SQM-LE (default) my $lightpollutionmap_key = 'SRwmK5l4wfFNFa3l'; # input your key my $latitude = 59.0700704; my $longitude = 16.1881969; my $max_sun_elevation = -18; # do not read if sun is above this value my $read_interval = 180; # in seconds my $retry = 10; # if sqm-le is busy, retry in seconds while ( 1 ) { my $elevation = sunElevation(); print "Sun elevation ", $elevation, "\n"; if ( $elevation < $max_sun_elevation ) { $sqm = readSQM(); # if sqm = 0 then we couldnt connect to the meter. if ( $sqm != 0 ) { chomp($timeNow = `date -u "+%Y-%m-%dT%H:%M:%S"`); my $url = "https://www.lightpollutionmap.info/sqm/submit.ashx?key=$lightpollutionmap_key&sqm=$sqm&utctime=$timeNow"; my $html = get $url; print $url, "\n"; print $html, "\n"; } } sleep $read_interval; } sub readSQM { # try 10 times to read the sqm-le device READ: { for ( my $i = 0; $i < 10; $i++ ) { my $remote = IO::Socket::INET->new(PeerAddr => $sqm_ip_addres, PeerPort => $sqm_port, Proto => 'tcp'); if ( ! defined $remote || $remote eq '' ) { print "unable to connect to sqm-le\n"; print "i = ", $i, "\n"; sleep $retry; } else { $remote->autoflush(1); $str=""; #-- Send request to remote SQM print $remote "rx"; #-- Add response to string $str .= <$remote>; #-- Close remote socket $remote->close; @foo = split(/,/,$str); $sqm = $foo[1]; #r, 00.00m,0000566034Hz,0000000000c,0000000.000s, 018.3C print "SQM = ", $sqm, "\n"; $sqm =~ s/m//; $sqm =~ s/\s+//; #remove 0.11 for the Waterproof housing window. if ( $sqm > 1.0 ) { $sqm = $sqm - 0.11; } last READ; } } } return $sqm; } sub sunElevation { #Calculate the Sun elevation my $lat = deg2rad( $latitude ); my $lon = deg2rad( $longitude ); my $elev = 0; # Record the time my $time = time (); # Set up observer's location my $loc = Astro::Coord::ECI->geodetic ($lat, $lon, $elev); # Uncomment to turn off atmospheric refraction if desired. # $loc->set( refraction => 0 ); # Instantiate the Sun. my $sun = Astro::Coord::ECI::Sun->universal ($time); # Figure out if the Sun is up at the observer's location. my ($azimuth, $elevation, $range) = $loc->azel ($sun); #print "The Sun is ", rad2deg ($elevation), # " degrees above the horizon.\n"; my $elevationDegres = rad2deg ($elevation); return $elevationDegres; }