In this blog, I share my code on getting extended weather forecasts (multiple days and hours within each day) for any city around the world using openweathermap API.
I used Python for this as usual. First, as a developer, I need to get a developer API key from http://api.openweathermap.org which is free for reasonable amount of use. However, I don’t want to share my API key with the world and have it abused, so I modified my code to take the API key from the user so that they can enter their own API key, then the program will fetch the weather data for the specified city, state and/or country.
The API documentation is here: https://openweathermap.org/api/geocoding-api
The code below is customized specifically for pacific time zone (PST or PDT depending on the season) and shows weather conditions by hour…every 3 hours from 1AM until 10PM for each day. The number of counts (lines) depends on what time (remember, this is customized for my timezone: Pacific) the app is executed because the response we get from the server starts from the next nearest time slot (increments of 3 hours) until 10PM of the day of execution. Therefore, if I run it at 6PM, it’ll show weather info from 7PM, then 10PM for the day, and then for the next day starting from 1AM, 4AM…4PM, 7PM, 10PM. So, you see for a given day, it can have a maximum of 8 entries and a minimum of 1.
To see the source code, click on ‘Show files’ icon on embedded control below. To run it, click Run button on its right or the play button on the cover image. Note, you’ll need your own API key to run it successfully.
The API can fetch daily, historical weather reports from around the world as well. However, those require subscription and for brevity, I’ve not included those variations here.
Once you have the API, just copy to the clipboard and run this app; when prompted for the API key, right-click on the prompt cursor and paste as plain text.
Here’s a sample output:
The code is also publicly shared on Replit (which you can access from the control above by clicking on ‘Open on Replit’ button.
Hope this was interesting to you!
Interested in creating programmable, cool electronic gadgets? Give my newest book on Arduino a try: Hello Arduino!▟