I'm getting around to using the MagTag that I got in AdaBox to setup the Daily Weather Forecast (https://learn.adafruit.com/magtag-weather). But I seem to be having a problem getting the correct weather for my city. The problem is that I'm trying to get the weather for Wilmington, DE but the https://openweathermap.org/find shows 7 different cities when entering "Wilmington, US". I think I'm getting Wilmington, NC weather displayed instead of Wilmington, DE because I don't know how to discriminate between the cities that openweathermap replies with. Is there a way to specify a forecast report using the openweathermap API with a city code like I can in a web browser to get Delaware's Wilmington? https://openweathermap.org/city/4145381
Should I open an issue on GitHub for this?
Choosing the right city from OpenWeatherMap for MagTag Weath
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- sembazuru
- Posts: 141
- Joined: Sat Mar 30, 2013 2:50 am
- DJDevon3
- Posts: 210
- Joined: Wed Mar 06, 2019 11:02 am
Re: Choosing the right city from OpenWeatherMap for MagTag W
You don't need to open a GitHub bug report for this because it's not a problem with the MagTag library. The OpenWeather example is strictly from the learn guide. The example isn't in the MagTag library though it probably should be because it's a particularly awesome example.
It's specified in the get_data_source_url of the MagTag example using OpenWeather OneCall API. Onecall is currently at 3.0 and this example is using 2.5 of their API. Probably time for someone to update the MagTag openweather example. In OneCall 3.0 they now use lat/lon instead of city name. My guess is because issues like this started happening. You can use their API to change it to the 3.0 format.
Here's the piece of code that's using the 2.5 API in the example.
and here's what the request url should look like for their 3.0 API (I've input lat/lon for Wilmington for you)
If you even attempt to test it without the API key in the URL it will fail. Just add yours and give it a whirl. Lat, lon, and key are the minimal 3 required parameters for even the most basic of calls, everything else is optional.
DT Timestamp format is in Unix Epoch format (Unix time, UTC time zone), e.g. dt=1586468027. Time is required if you want to pull up historical data even if it was from 15 minutes ago.
Everything you need to know about how to use the API is documented in their one call api page. It's really long and their json formatting probably changed, there's a ton of parameters to choose from. Start with the basics.
Technically it's not a Circuit Python or even a MagTag library bug but an issue with an outdated OpenWeather example which is only found in the learn guide. I've notified the author their example needs updating. Thank you for bringing this issue up.
It's specified in the get_data_source_url of the MagTag example using OpenWeather OneCall API. Onecall is currently at 3.0 and this example is using 2.5 of their API. Probably time for someone to update the MagTag openweather example. In OneCall 3.0 they now use lat/lon instead of city name. My guess is because issues like this started happening. You can use their API to change it to the 3.0 format.
Here's the piece of code that's using the 2.5 API in the example.
Code: Select all
def get_data_source_url(api="onecall", location=None):
"""Build and return the URL for the OpenWeather API."""
if api.upper() == "FORECAST5":
URL = "https://api.openweathermap.org/data/2.5/forecast?"
URL += "q=" + location
elif api.upper() == "ONECALL":
URL = "https://api.openweathermap.org/data/2.5/onecall?exclude=minutely,hourly,alerts"
URL += "&lat={}".format(location[0])
URL += "&lon={}".format(location[1])
else:
raise ValueError("Unknown API type: " + api)
return URL + "&appid=" + secrets["openweather_token"]
Code: Select all
https://api.openweathermap.org/data/3.0/onecall?lat=39.74&lon=-75.54&exclude=hourly,daily&appid={API key}
DT Timestamp format is in Unix Epoch format (Unix time, UTC time zone), e.g. dt=1586468027. Time is required if you want to pull up historical data even if it was from 15 minutes ago.
Code: Select all
https://api.openweathermap.org/data/3.0/onecall/timemachine?lat={lat}&lon={lon}&dt={time}&appid={API key}
Technically it's not a Circuit Python or even a MagTag library bug but an issue with an outdated OpenWeather example which is only found in the learn guide. I've notified the author their example needs updating. Thank you for bringing this issue up.
- adafruit_support_carter
- Posts: 29168
- Joined: Tue Nov 29, 2016 2:45 pm
Re: Choosing the right city from OpenWeatherMap for MagTag W
Actually, when it came to the weather forecast query, lat/lon was already being used. Allowing location to be specified via a string in secrets.py was done for ease of use. But dealing with it in code was a bit of a hack - a 2nd API was used behind the scenes to determine lat/lon based on city name. And there was no obvious way to deal with ambiguous city names like above.
There's a PR here:
https://github.com/adafruit/Adafruit_Le ... /pull/2232
that updates the code to allow specifying location as lat/lon or city name. That'd be the fall back for cases like above. Will update the guide to cover this, but usage is easy. Just use a lat/lon tuple in secrets.py:
There's a PR here:
https://github.com/adafruit/Adafruit_Le ... /pull/2232
that updates the code to allow specifying location as lat/lon or city name. That'd be the fall back for cases like above. Will update the guide to cover this, but usage is easy. Just use a lat/lon tuple in secrets.py:
Code: Select all
'openweather_location' : (47.6062, -122.3321),
- DJDevon3
- Posts: 210
- Joined: Wed Mar 06, 2019 11:02 am
Re: Choosing the right city from OpenWeatherMap for MagTag W
Honestly even though it’s right there in the code I pasted for the 2.5 api I never noticed it. Didn’t know it was used like that. I always choose NY for time. For weather that’s not great if you’re in Miami. Lat/Lon makes a lot more sense plus can provide easier integration with weather maps. Your solution to the problem was much easier than what I thought would be required. Thanks!
- adafruit_support_carter
- Posts: 29168
- Joined: Tue Nov 29, 2016 2:45 pm
Re: Choosing the right city from OpenWeatherMap for MagTag W
OK, the changes have been merged and should now show up in the guide. The verbiage has also been updated to mention being able to use lat/lon for location:
https://learn.adafruit.com/magtag-weath ... on-3079579
@sembazuru Please take a look and try updating and see if using lat/lon helps get around the ambiguous city name issue.
Also - I tried also updating to the 3.0 API, but kept getting invalid key responses. Maybe it requires generating a new key? Unknown. But the 2.5 calls are still working for now. And there's nothing about the 3.0 API that's needed for this project. So for now, sticking with 2.5.
https://learn.adafruit.com/magtag-weath ... on-3079579
@sembazuru Please take a look and try updating and see if using lat/lon helps get around the ambiguous city name issue.
Also - I tried also updating to the 3.0 API, but kept getting invalid key responses. Maybe it requires generating a new key? Unknown. But the 2.5 calls are still working for now. And there's nothing about the 3.0 API that's needed for this project. So for now, sticking with 2.5.
- sembazuru
- Posts: 141
- Joined: Sat Mar 30, 2013 2:50 am
Re: Choosing the right city from OpenWeatherMap for MagTag W
@adafruit_support_carter
My apologies for not getting back to this thread until today. Life Happens™.
I downloaded the new code in the learning guide and changed my location from the city name to a two member tuple with my latitude and longitude. After updating to CircuitPython 7.3.2 (I honestly don't remember which version of CP I had installed on my MagTag but the updated code on the guide was giving me an error) and updating the python libraries (because of the breaking changes to the mpy format brought on by CP7 as mentioned on the download page for the library archives) I am reliably getting my forecast instead of the forecast almost 500 miles south of me. :-D
My apologies for not getting back to this thread until today. Life Happens™.
I downloaded the new code in the learning guide and changed my location from the city name to a two member tuple with my latitude and longitude. After updating to CircuitPython 7.3.2 (I honestly don't remember which version of CP I had installed on my MagTag but the updated code on the guide was giving me an error) and updating the python libraries (because of the breaking changes to the mpy format brought on by CP7 as mentioned on the download page for the library archives) I am reliably getting my forecast instead of the forecast almost 500 miles south of me. :-D
- adafruit_support_carter
- Posts: 29168
- Joined: Tue Nov 29, 2016 2:45 pm
Re: Choosing the right city from OpenWeatherMap for MagTag W
Cool. Glad it worked. Thanks for letting us know :)
- DJDevon3
- Posts: 210
- Joined: Wed Mar 06, 2019 11:02 am
Re: Choosing the right city from OpenWeatherMap for MagTag Weath
Finally got around to poking at the 3.0 api and I think you’re right carter. I was also getting invalid key responses like a brick wall. I made a new key and that did not help. I was running into the same exact issue you were, can’t be a coincidence. 3.0 might be for their subscription based clients only? Agree on sticking with 2.5 for the foreseeable future. If you couldn’t get it to work then I doubt anyone else could.
One of the potential perks for using lat/lon instead of city name is a better vantage point with maps. I think maps are for subscribers only? It’s a good habit for people to use lat/lon in case they want/need to upgrade to a subscription in the future.
One of the potential perks for using lat/lon instead of city name is a better vantage point with maps. I think maps are for subscribers only? It’s a good habit for people to use lat/lon in case they want/need to upgrade to a subscription in the future.
- DJDevon3
- Posts: 210
- Joined: Wed Mar 06, 2019 11:02 am
Re: Choosing the right city from OpenWeatherMap for MagTag Weath
Just an update after playing with the maps api. Some basic maps are available for 2.5 but they’re in png format which isn’t natively supported in circuit Python yet. Would need a middleman website with imagemagik type of capability to make use of them. 😔 I have confirmed their 3.0 api is currently for subscribers only.
Please be positive and constructive with your questions and comments.