I got sick of opening up the weather app or website and searching a location to see how windy is was gonna be in x amount of days time. Whilst I could get my answer, it wasn’t quick plus the app and website had saturation’s of stuff I didn’t want. I wanted a clear and simple way to check if it was good to fly my drone at a location or not.
The process
Trusty PHP was going to be my back end for the little web app. The weather api was going to be through OpenWeatherMap in which I had played around with before, but I created a new account anyway and got a fresh api key with limits as it’s the free tier.
I wouldn’t or didn’t expect to come across any rate limits fetching a dozen locations weather data but I did want to follow good web dev practices. Calling the api endpoint every 2 hours via a cron job and filling/refreshing a JSON file for each location was the plan. Given many of my locations actually aren’t unique towns and needed more defining I went with the postcode, country code definer for the weather location. My call looked like this:
$data = json_decode(file_get_contents("https://api.openweathermap.org/data/2.5/forecast?zip=" . $postcode . ",au&units=metric&appid=MYAPIKEYHERE"), true);
Functions; time savers and cleanliness
For ease and to avoid repetitive code functions would feature heavily in this project. I made a function to:
- Call and save/refresh the data into the location specific JSON file.
- Make an array storing the weather values for a certain day in advanced and time.
That last function was hard to figure out a method to. The api response I used was a 5 day forecast which gave data for every 3 hours from the closest 3 hour interval to the call to 5 days away, I only wanted the 9am and 3pm data for each of the days. Sheesh. I knew if I could crack this problem I was over the hill here is the important code that does it:
$today = date("Y-m-d"); $nine = "09:00:00"; $three = "15:00:00"; if ($day == 0 && $time == 9) { $datetime = date("Y-m-d $nine", strtotime("+0 day")); } elseif ($day == 0 && $time == 3) { $datetime = date("Y-m-d $three", strtotime("+0 day")); } elseif ($day == 1 && $time == 9) { $datetime = date("Y-m-d $nine", strtotime("+1 day")); } elseif ($day == 1 && $time == 3) { $datetime = date("Y-m-d $three", strtotime("+1 day")); } elseif ($day == 2 && $time == 9) { $datetime = date("Y-m-d $nine", strtotime("+2 day")); } elseif ($day == 2 && $time == 3) { $datetime = date("Y-m-d $three", strtotime("+2 day")); } elseif ($day == 3 && $time == 9) { $datetime = date("Y-m-d $nine", strtotime("+3 day")); } elseif ($day == 3 && $time == 3) { $datetime = date("Y-m-d $three", strtotime("+3 day")); } elseif ($day == 4 && $time == 9) { $datetime = date("Y-m-d $nine", strtotime("+4 day")); } elseif ($day == 4 && $time == 3) { $datetime = date("Y-m-d $three", strtotime("+4 day")); } elseif ($day == 5 && $time == 9) { $datetime = date("Y-m-d $nine", strtotime("+5 day")); } elseif ($day == 5 && $time == 3) { $datetime = date("Y-m-d $three", strtotime("+5 day")); } else { }
Its hard to read but does as i wanted it to. We fetch the current date and add days on to get the forecasted dates, to ensure we get the 9am and 3pm data i simple add on either 09:00:00 or 15:00:00 to our added up date and see if it equals the weather data date and time. If so we grab the data.
My other smaller but still important functions did:
- Metres per second to kilometres per hour conversion.
- Convert postcode to town name.
- Change wind direction degrees to compass direction name (North, South west etc etc).
- Compare the wind + temp change from 9am to 3pm on a day.
- Compare the wind + temp change between any specified day and time.
A bigger function that finds the best conditioned date and time for a location, this involved building an array for that location with all the forecasted wind speeds and finding the lowest value.
I also made some functions which featured heavy if and elseif statements around the wind speed, temperature and cloud level to state if it was good to drone or not.
Creating a full 5 day forecast table with all the values for the defined postcode. This meant I could make a page that consisted of all locations in handy tables displaying the forecasted weather data nicely. It wasn’t the instant “can I drone or not” method but still handy.
I also made functions that returned tables for the best date and time + the differences or changes between a locations date and times.
How to transition to the front end
I considered using leaflet the maps frameworks to display the data graphically but then I would need to dig into some JS in which I wasn’t keen, but hey maybe in the future. The main page for this webapp needed to simply display all locations and the conditions for droning, Not just yes or no but rather what the conditions are “Slightly windy, clear and warm” or “No wind, cloudy and cold”.
I also wanted easy links to each location to see the table data and compare the changes between times and days. More functions where in check, but I wanted to shape this main landing page.