Ceci est une ancienne révision du document !
First things first. Last month, I gave you a quick overview of the RTK.GPIO board, so I thought I’d give you a quick update. I tried some “normal” GPIO type tests, and they worked well. However, when I tried an I2C program to a simple I2C device, it failed to run. I’ll try to keep playing and let you know.
Now, on to the Raspberry Pi Pico board. Yes it did come in. The darn thing is so tiny, and I’m so old with bad eyesight, that it was hard for me to solder the pins for the breadboard. I did it, without any shorts (or melting the board), but it took me twice as long as it would have 4 years ago. It worked pretty well, I must say. There are lots of possibilities for the RP2040 chipset and I can see many powerful microcontroller boards coming out in the future based on it. As they say in Texas, “They done did GOOD!” (I don’t say that, but ‘they’ do).
This month, we’ll revisit free weather APIs on the Internet. Why? Well many of them have either closed down or gone to a full pay model, so the choices have changed. When I’m sitting in my living room, I want to be able to check the weather outside and get the forecast for my location. Since I live in an apartment, a proper freestanding weather station is just not possible, so I have to rely on an outside source.
I did a quick check the other day and found one that has a plan that I can get behind. It’s called Weather API, and you can visit their homepage at https://www.weatherapi.com/. They offer 5 plans, one of which is free. Their free plan offers the ability to make 1,000,000 calls per month which works out to over 2,000 calls per day. Plenty to get a reasonable source of information.
They offer return data in either XML or JSON format. We’ll explore both formats, and some of the data that is returned.
Before you can get anything, you need to sign up for a free API key. Point your favorite browser to https://www.weatherapi.com/signup.aspx, and you will be connected to the Sign-up page. It’s very simple. Just type your email address twice, create a password and enter it twice, and click the “I’m not a robot” box, then agree to the Terms and Conditions. Finally, click the “Sign Up” button. You’ll get an email asking you to verify your email, and you’ll receive a key. Be sure to save this key somewhere, because you’ll need it as part of any queries to the system.
As we have done before, this API has you build the http query with the query type, the location you want, and your API key. Here is what the simplest format (with my API key obscured) looks like…
http://api.weatherapi.com/v1/current.json?key=xxxxxxxxxxxxxxxxxxx&q=78748
Here’s the breakdown of the URL:
Base URL Address: http://api.weatherapi.com/v1/
Type of query: current.json Your API Key: ?key=xxxxxxxxxxxxxxxxxxx
Location: &q=78748
Additional Parms (if any)
The URL can easily be constructed from variables and an “f-string”…
Base = ‘http://api.weatherapi.com/v1/’
Query = ‘current.json’
Key = ‘?key=xxxxxxxxxxxxxxxxxxx’
Location = ‘&q=78748’
link = f“{Base}{Query}{Key}{Location}”
The location can be “US Zipcode, UK Postcode, Canada Postalcode, IP address, Latitude/Longitude (decimal degree), or city name.” Of course, the IP address needs to be your EXTERNAL IP address, not your local internal IP address.
You can paste the URL into the browser of your choice, and the result looks something like the code shown right.
You can ask for the data to be returned in JSON format (as we did here), or XML format.
Here is the link to the API docs: https://www.weatherapi.com/docs/. There are many options here. As I was writing this article, they added two new options, Weather Alerts and Air Quality.
Before we get into code, let’s take a quick look at the forecast call.
http://api.weatherapi.com/v1/forecast.json?key=xxxxxxxxxxxxxxxxxx&q=78748&days=3&aqi=yes&alerts=yes
For the free API account, you can have a forecast for up to 3 days. The number of days depends on the level of account you have signed up for.
So the big changes from our previous current-weather query is the “forecast.json” instead of “current.json” string, and the addition of days, aqi=yes (aqi stands for Air Quality Index), and the alerts=yes. There is also a language option that you can use, but you should look at the documentation to get the correct parameter.
When we send this query to the API, you’ll get back a LARGE amount of data. I’ll just paste a portion here (below).
You get the location information and current condition information, just like when we did the current query. In addition to that, you also get the Air Quality information, since we asked for it (bottom left).
Next comes the forecast information. Under the general [‘forecast’] header, you get a forecastday group of data for each of the days that you requested (3 in this case), each of which starts with “date” and “date_epoch”, then goes on to giving a day overview followed by the astronomy data for that day (sunrise, sunset, etc), then 24 hours of data grouped by hour. Then it repeats for each extra day that you requested, then finally any alert data that might have been issued. Each of these data groups are pretty much the same as the current condition data.
It’s a lot of data – which is why I used JSON format for the ease of picking out what I want on any call.
The Code
Now we can concentrate on the code. Luckily, it is very similar to the logic that we’ve used before. As always, we start with our imports…
import requests
import json
We now define some of our variables.
When we run the program, our output will look something like that shown right.
That’s it. Very simple and easily modifiable to add or delete elements as you wish – to customize the output to your needs.
I’ve placed the code on my github repository at https://github.com/gregwa1953/FCM-167.
One final thing. Last month, I promised that I would give my first impressions of the Raspberry Pi Pico microcontroller. I had originally intended doing the update here, but even before I got the Pico board in, I was so excited that I talked to Ronnie about creating another series of articles under a separate title. He said that it would be no problem, so I will now be trying to do two articles a month, one on “standard Python” and one on working with Microcontrollers using MicroPython and CircuitPython. I’m sure that every once-in-a-while, there will be an occasional crossover project that will take up both.
Be sure to check out my new article series called “Micro This Micro That” in this and future issues of Full Circle!
As always, until next time; stay safe, healthy, positive and creative!