Last active
September 20, 2020 23:59
-
-
Save bonelifer/66a32b69fe542704cbc9b1f40f53a814 to your computer and use it in GitHub Desktop.
Get weather for today
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
<?php | |
require_once 'noaa/Forecaster.php'; | |
$config = new \noaa\weather\Configuration(); | |
$myWritableCacheDirectory = dirname(__FILE__) . '/cache'; | |
$config->setCache(new \noaa\weather\cache\FileCache($myWritableCacheDirectory)); | |
$forecaster = new \noaa\Forecaster($config); | |
// Function to calculate HeatIndex | |
function heatIndex($currTempF,$currentHumidity){ | |
## Function Found at https://github.com/RunnenLate/HeatIndexCalculator/blob/master/HeatIndexCalculator/src/HeatIndexCalculator.java#L38 | |
$c1 = -42.379; | |
$c2 = 2.04901523; | |
$c3 = 10.14333127; | |
$c4 = -0.22475541; | |
$c5 = -0.00683783; | |
$c6 = -0.05481717; | |
$c7 = 0.00122874; | |
$c8 = 0.00085282; | |
$c9 = -0.00000199; | |
$HeatIndex=$c1+ | |
($c2*$currTempF)+ | |
($c3*$currentHumidity)+ | |
($c4*$currTempF*$currentHumidity)+ | |
($c5*($currTempF*$currTempF))+ | |
($c6*($currentHumidity*$currentHumidity))+ | |
($c7*($currTempF*$currTempF)*$currentHumidity)+ | |
($c8*$currTempF*($currentHumidity*$currentHumidity))+ | |
($c9*($currTempF*$currTempF)*($currentHumidity*$currentHumidity)); | |
return $HeatIndex; | |
} | |
// Begin Change These Varibles | |
$stationId = 'KCXW'; | |
$lat = '35.1509'; | |
$lng = '-92.7441'; | |
$offset = -5; # Your UTC/timezone offset (ex CDT-5/CDT-6) | |
$startTime = date('Y-m-d', time()); | |
$numDays = 1; | |
// End Change These Varibles | |
// Zenith Calculation: https://blog.ip2location.com/knowledge-base/display-sunrise-sunset-time-using-php-and-mysql-database/ | |
// zenith = 90 + (50/60) | |
// zenith = 90.833333333 | |
// | |
// Don't mess with this variable or you'll end up with strange/incorrect Sunrise/Sunset predictions. | |
$zenith = 90.833333333; | |
try { | |
// returns a CurrentWeather object or throws an exception on API error | |
$current = $forecaster->getCurrentWeather($stationId); | |
} catch (\Exception $e) { | |
echo "Error: " . $e->getMessage() . "\n"; | |
} | |
$forecast = $forecaster->getForecastByLatLng($lat, $lng, $startTime, $numDays); | |
$day = $forecast->getDay(0); | |
echo "Current Conditions: " . $day->getConditions() . "\n"; | |
echo "Current Temp: " .$current->getTemperatureF() . "°F\n"; | |
$high = ($day->getHighTemperature() === null) ? 'NULL' : $day->getHighTemperature(); | |
echo "High: " . $high . "°F\n"; | |
$low = ($day->getLowTemperature() === null) ? 'NULL' : $day->getLowTemperature(); | |
echo "Low: " . $low . "°F\n"; | |
echo "Wind Speed: " . $current->getWindSpeedMPH() . " MPH (" . $current->getWindSpeedKnots() . " KT)\n"; | |
echo "Wind Direction: " . $current->getWindDirection() . "\n"; | |
echo "Visibility: " . $current->getVisibilityMiles() . " miles\n"; | |
echo "Humidity: " . $current->getRelativeHumidity() . "%\n"; | |
echo "Dew Point: " . $current->getDewPointF() . "%\n"; | |
# $precipDay = ($day->getPrecipitationProbabilityDay() === null) ? 'NULL' : $day->getPrecipitationProbabilityDay(); | |
# echo "Precipitation Chance Day: " . $precipDay . "%\n"; | |
# $precipNight = ($day->getPrecipitationProbabilityNight() === null) ? 'NULL' : $day->getPrecipitationProbabilityNight(); | |
# echo "Precipitation Chance Night: " . $precipNight . "%\n"; | |
$currTempF = $current->getTemperatureF(); | |
$currentHumidity = $current->getRelativeHumidity(); | |
$heatindex = round(heatIndex($currTempF,$currentHumidity),0); | |
echo "Heat Index: " . $heatindex . "°F\n"; | |
$hazards = $forecast->getHazards(); | |
if (count($hazards) > 0) { | |
echo "HAZARDS: " . implode(', ', $hazards) . "\n"; | |
} | |
echo("Sunrise: " . date("F j, Y") . " at "); | |
$sunrise_time = date_sunrise(time(),SUNFUNCS_RET_STRING,$lat,$lng,$zenith,$offset); | |
echo date('g:i', strtotime($sunrise_time)); | |
echo("AM"); | |
echo("\nSunset: " . date("F j, Y") . " at "); | |
$sunset_time = date_sunset(time(),SUNFUNCS_RET_STRING,$lat,$lng,$zenith,$offset); | |
echo date('g:i', strtotime($sunset_time)); | |
echo("PM\n"); | |
echo "\nLocation: ". $current->getStationId() . " -- " . $current->getLocation(); | |
echo("\nTime Observed: "); | |
$timeChecked = ($current->getObservationTime()); | |
echo date('F j, Y g:iA', strtotime($timeChecked)); | |
echo ("\n"); | |
echo "Time Checked: "; | |
$startChecked = date('F j, Y g:iA', time()); | |
echo (" " . $startChecked); | |
// WRITE URL to url.txt | |
// This Bitly URL is the NWS forecast page for my local station, you'll | |
// need to create your own Bitly URL or at least replace this with the | |
// NWS url directly. You'll see this when you click on the notification | |
// in the phone app. This page will show longer range forcasts for | |
// your area. | |
$myURL = fopen("url.txt", "w") or die("Unable to open file!"); | |
$URL = "https://bit.ly/3hIyJOj\n"; | |
fwrite($myURL, $URL); | |
fclose($myURL); | |
// END WRITE URL to url.txt | |
// WRITE TITLE to title.txt | |
// This outputs the title for the Pushover message. | |
$todaysDate = date('F j Y', time()); | |
$todaysSunrise = date('g:i', strtotime($sunrise_time)) . "AM"; | |
$myTITLE = fopen("title.txt", "w") or die("Unable to open file!"); | |
$title = "Todays sunrise - $todaysDate at $todaysSunrise"; | |
fwrite($myTITLE, $title); | |
fclose($myTITLE); | |
// END WRITE TITLE to title.txt | |
?> |
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
<?php | |
/** | |
* @author Chris Schalenborgh <chris.s@kryap.com> | |
* @version 0.1 | |
*/ | |
include('Pushover.php'); | |
$push = new Pushover(); | |
$push->setToken('yourAPPorGroupToken'); | |
$push->setUser('yourUSERtoken'); | |
$getTitle = file_get_contents('./title.txt', true); | |
$push->setTitle($getTitle); | |
$dateTime = date('F j, Y g:iA TO', time()); | |
$getWeather = file_get_contents('./weather.txt', true); | |
$push->setMessage($getWeather); | |
$getURL = file_get_contents('./url.txt', true); | |
$push->setUrl($getURL); | |
$push->setUrlTitle('Weather Forecast'); | |
#$push->setDevice('nexus5x'); | |
$push->setPriority(1); | |
#$push->setRetry(60); //Used with Priority = 2; Pushover will resend the notification every 60 seconds until the user accepts. | |
#$push->setExpire(3600); //Used with Priority = 2; Pushover will resend the notification every 60 seconds for 3600 seconds. After that point, it stops sending notifications. | |
#$push->setTimestamp(time()); | |
#$push->setDebug(true); | |
$push->setSound('pushover'); | |
$go = $push->send(); | |
#$receipt = $push->getReceipt(); | |
?> |
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
php fc.php > ./weather.txt | |
php pushoverweather.php |
The main weather forecast file can be piped to a text file like this:
php fc.php > ./weather.txt
Sample results:
Current Conditions: Mostly Sunny
Current Temp: 81°F
High: 79°F
Low: 58°F
Wind Speed: 11.5 MPH (10 KT)
Wind Direction: East
Visibility: 6 miles
Humidity: 45%
Dew Point: 57.2%
Heat Index: 81°F
Sunrise: September 20, 2020 at 6:56AM
Sunset: September 20, 2020 at 7:11PM
Location: KCXW -- Conway - Cantrell Field, AR
Time Observed: September 20, 2020 3:55PM
Time Checked: September 20, 2020 5:14PM
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the php NOAA Weather library I used. This is where I got the Heat Index calculations from: click link. I use this PHP pushover client: click here
Find your NWS StationID by going to this site and entering your City, State in the field on the left hand side. You'll also find your Latitude and Longitude on the page that is displayed. Enter the StationID and Latitude and Longitude in the variables at the top of fc.php. You'll also need to get your pushover user token and app/group token from pushover.net in order to use the pushover script, along with downloading the pushover library file.