Created
April 26, 2016 08:21
-
-
Save nonoo/aaf18879d8edefecfa841971711d42c5 to your computer and use it in GitHub Desktop.
PHP script for fetching weather data from OMSZ FTP and sending to the APRS-IS network.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php5 | |
<?php | |
// Puts weather data from OMSZ into the nweather database. | |
ini_set('display_errors','On'); | |
error_reporting(E_ALL); | |
define('DB_NAME', 'ha5kdr.hu'); | |
define('DB_USER', 'ha5kdr.hu'); | |
define('DB_PASSWORD', ''); | |
define('DB_HOST', 'localhost'); | |
define('FTP_USERNAME', 'radioamator'); | |
define('FTP_PASS', ''); | |
define('FTP_TEMPFILE', '/tmp/gerecse-omsz.txt'); | |
define('APRS_SERVER', 'hun.aprs2.net'); | |
define('APRS_SERVER_PORT', 14580); | |
define('APRS_CALLSIGN', 'HA2KDR-3'); | |
define('APRS_PASSCODE', 19898); // Generate this with an APRS passcode generator | |
define('APRS_ALTINFEET', 2080); | |
// Use GPS coordinate format, see http://www.csgnetwork.com/gpscoordconv.html | |
define('APRS_COORD', '4740.55N/01829.60E'); | |
define('APRS_COMMENT', 'Mnt. Gerecse WX alt. 634m - www.ha5kdr.hu'); | |
$mysqlconn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); | |
if (!$mysqlconn) { | |
echo "can't connect to mysql database!\n"; | |
return 1; | |
} | |
$ftpconn = ftp_connect('ftp.met.hu'); | |
if (!ftp_login($ftpconn, FTP_USERNAME, FTP_PASS)) { | |
echo "can't connect to the ftp server!\n"; | |
$mysqlconn->close(); | |
return 1; | |
} | |
ftp_pasv($ftpconn, 1); | |
$filelist = ftp_nlist($ftpconn, '/'); | |
$retries = 0; | |
while (!in_array('/ot2radioamator.ok', $filelist)) { | |
sleep(5); | |
$filelist = ftp_nlist($ftpconn, '/'); | |
if ($retries++ == 5) { | |
echo "no ok file found after retries!\n"; | |
ftp_close($ftpconn); | |
$mysqlconn->close(); | |
return 1; | |
} | |
} | |
foreach ($filelist as $filename) { | |
if ($filename == '/sky') | |
continue; | |
echo "processing $filename...\n"; | |
$f = fopen(FTP_TEMPFILE, 'w+'); | |
if (!$f) { | |
echo "can't create temp file!\n"; | |
ftp_close($ftpconn); | |
$mysqlconn->close(); | |
return 1; | |
} | |
if (ftp_fget($ftpconn, $f, $filename, FTP_ASCII)) { | |
fseek($f, 0); | |
while (($line = fgets($f, 1024)) !== false) { | |
$line = iconv('iso-8859-2', 'utf8', $line); | |
// Removing whitespaces from inside the string. | |
$line = preg_replace('/\s+/', ' ', $line); | |
if (strlen($line) == 0 || $line[0] == '#') | |
continue; | |
$line_exploded = explode(' ', $line); | |
if ($line_exploded[1] != 'Gerecse-tető') | |
continue; | |
$date = date('Y-m-d H:i:s', strtotime($line_exploded[0])); | |
$temp = $line_exploded[2]; | |
$humidity = $line_exploded[3]; | |
$rain = $line_exploded[4]; | |
$winddir = $line_exploded[5]; | |
$windspeed = $line_exploded[6]; | |
if ($temp == '-') | |
continue; | |
$res = $mysqlconn->query('replace into `nweather-gerecse-omsz` (' . | |
'`date`, ' . | |
'`temp`, ' . | |
'`hum`, ' . | |
'`rain`, ' . | |
'`winddir`, ' . | |
'`windspeed`) values (' . | |
'"' . $mysqlconn->escape_string($date) . '", ' . | |
'"' . $mysqlconn->escape_string($temp) . '", ' . | |
'"' . $mysqlconn->escape_string($humidity) . '", ' . | |
'"' . $mysqlconn->escape_string($rain) . '", ' . | |
'"' . $mysqlconn->escape_string($winddir) . '", ' . | |
'"' . $mysqlconn->escape_string($windspeed) . '")'); | |
if (!$res) | |
echo " error inserting to the database\n"; | |
else | |
echo " ok\n"; | |
} | |
} | |
fclose($f); | |
} | |
unlink(FTP_TEMPFILE); | |
ftp_close($ftpconn); | |
echo "uploading info to aprs-is...\n"; | |
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); | |
if ($socket) { | |
$result = socket_connect($socket, APRS_SERVER, APRS_SERVER_PORT); | |
if ($result) { | |
// Authenticating | |
$tosend = 'user ' . APRS_CALLSIGN . ' pass ' . APRS_PASSCODE . "\n"; | |
socket_write($socket, $tosend, strlen($tosend)); | |
$authstartat = time(); | |
$authenticated = false; | |
while ($msgin = socket_read($socket, 50, PHP_NORMAL_READ)) { | |
if (strpos($msgin, APRS_CALLSIGN . ' verified') !== FALSE) { | |
$authenticated = true; | |
break; | |
} | |
// Timeout handling | |
if (time()-$authstartat > 5) | |
break; | |
} | |
if ($authenticated) { | |
$res = $mysqlconn->query('select * from `nweather-gerecse-omsz` order by `date` desc limit 1'); | |
if ($res) { | |
$row = $res->fetch_array(MYSQLI_ASSOC); | |
if ($row) { | |
$temp = $row['temp']; | |
$hum = $row['hum']; | |
$rain = $row['rain']; | |
$winddir = $row['winddir']; | |
$windspeed = $row['windspeed']; | |
$pressure = 0; | |
} | |
$res->close(); | |
} | |
$res = $mysqlconn->query('select `rain` from `nweather-gerecse-omsz` where unix_timestamp(`date`) < unix_timestamp()-3600 order by `date` desc limit 1'); | |
if ($res) { | |
$row = $res->fetch_array(MYSQLI_NUM); | |
if ($row && isset($row[0])) | |
$rain1hourago = $row[0]; | |
$res->close(); | |
} | |
$res = $mysqlconn->query('select `rain` from `nweather-gerecse-omsz` where unix_timestamp(`date`) < unix_timestamp()-86400 order by `date` desc limit 1'); | |
if ($res) { | |
$row = $res->fetch_array(MYSQLI_NUM); | |
if ($row && isset($row[0])) | |
$rain24hourago = $row[0]; | |
$res->close(); | |
} | |
if (isset($rain1hourago) && isset($rain24hourago)) { | |
$raininlast1hour = $rain-$rain1hourago; | |
if ($raininlast1hour < 0) | |
$raininlast1hour = 0; | |
$raininlast1hour *= 0.3937008; // Converting from cm to inches | |
$raininlast24hours = $rain-$rain24hourago; | |
if ($raininlast24hours < 0) | |
$raininlast24hours = 0; | |
$raininlast24hours *= 0.3937008; // Converting from cm to inches | |
$tempfahrenheit = $temp*(9/5)+32; | |
$humoutclamped = $hum; | |
if ($humoutclamped > 99) | |
$humoutclamped = 99; | |
$windspeedkph = ($windspeed/1000)*3600; | |
$windspeedmph = $windspeedkph/1.609344; | |
// See: http://aprs.org/APRS-docs/WX.TXT | |
// http://homepage.ntlworld.com/wadei/aprs/APRSDEC%20demo%20output.txt | |
// http://aprs.org/APRS-docs/PROTOCOL.TXT | |
$tosend = sprintf(APRS_CALLSIGN . '>APRS,TCPIP*:@' . date('dHi') . 'z' . APRS_COORD . '_' . | |
"%03d/%03dg...t%03dr%03dp%03dh%02db%05d " . APRS_COMMENT . "\n", | |
$winddir, $windspeedmph, | |
$tempfahrenheit, $raininlast1hour*100, $raininlast24hours*100, | |
$humoutclamped, $pressure); | |
socket_write($socket, $tosend, strlen($tosend)); | |
} | |
} | |
} | |
socket_close($socket); | |
} else | |
echo " failed to connect\n"; | |
$mysqlconn->close(); | |
return 0; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment