Free Code: Accessing OpenWeatherMap.org API

I started a library very similar to OpenWatherMap when I found out that Wunderground has so much more features. It’s not public (not even uploaded yet), but I could do so and add you privately if you send me your GitHub handle.

Hi Ulrich,

Thank you so much for your help. That would be great. My GitHub handle is Knightlite.

Invitation sent. It’s very similar to the other one and should give you an idea how to retrieve values from the JSON. Again, you need to enter your developer ID into the API_ID constant of the Wundergrund (i missed an “o”) class …

Thank you Ulrich,

I will take a look at it this afternoon. Thank you so much for sending it. I really appreciate it.

For the sake of completeness, this is the action event of pushbutton1 of your example project rewritten to handle the encoding and parse the json to show temperature and wind. It assumes you’ve chosen the “conditions” weather feature when running it.

[code]Dim d As Xojo.Core.Dictionary
dim t, result, eol as text

eol = CType(EndOfLine, String).ToText

t = Weather(pmFeatures.Text,txtCity.Text,txtState.Text,pmType.Text,pmRadius.text).DefineEncoding(Encodings.utf8).ToText
d = Xojo.Data.ParseJSON(t)

if not d.HasKey(“current_observation”) then
return
end if

d = d.Value(“current_observation”)

if d.HasKey(“temperature_string”) then
result = result + "Temperature: " + d.Value(“temperature_string”) + eol
end if

if d.HasKey(“wind_string”) then
result = result + "Wind: " + d.Value(“wind_string”) + eol
end if

TextArea1.Text = result.Trim[/code]

For my location it returns:

[quote]
Temperature: 93.0 F (33.9 C)
Wind: From the NNE at 2.0 MPH Gusting to 2.0 MPH[/quote]

Hi Scott,

Thank you so much for showing how to parse the JSON. That now makes sense to me. Between your code and Ulrich’s project, I think I can figure things out from here.

Thank you so much for your help!

Reopening this old thread… NOW the WeatherUnderground has now stopped their service. I was wondering if anyone knows if there are any replacement APIs that could work with Xojo? I need to have the current weather conditions generated for one of my apps.

Oh! No API anymore?
OpenWeatherMap we talked about before is still active. Would that be sufficient? I see they added more (payable) options which should be easy to include: https://gitlab.com/UBogun/Xojo-OpenWeatherMapOrg-API

https://blog.rapidapi.com/access-global-weather-data-with-these-weather-apis/

Hi Ulrich,

Thank you for the quick reply. I was able to get one of my old projects working that had your OpenWeatherMap code in it. It works really well, but the only thing, it does not seem to give to local weather when you enter a zip code. For instance if I enter the zip code of 01229 which is in Stockbridge Massachusetts, it reports the weather in Albany, New York which is about 45 minutes away by interstate highway. I need the weather to report the local weather from the exact town or close to it. Weather Underground could do that.

I was looking at Dark Sky API but I don’t know to integrate that with Xojo.

Jean’s link has other API, but I don’t know how to get them to work with Xojo either.

[quote=429240:@Jean-Yves Pochez]
Jean-Yves Pochez 1 hour ago Pre-Release Testers, Xojo Pro Europe (France, Besançon)
https://blog.rapidapi.com/access-global-weather-data-with-these-weather-apis/ [/quote]

If I remember correctly, I only included the option to enter a city ID as by OWM’s lists: Index of /sample/

Per the API you could also change the code to use zips or geo locations.

EDIT: Stockbridge should be 4224681.

Now I am a little lost. I used this code to get the weather for a zip code.

Dim curweather  As OpenWeatherData = OpenWeather.GetCurrentWeather("zip="+txtZipCode.Text, API_ID, "units=imperial", "lang=en")

Is that the Geo Location for Stockbridge? Where did you find that? Can I change the line above from zip code to Geo Location?

Yes. If you change the locationID to id=4224681, it will show you Stockbridge.
About geo location: I did not try, but basically the id is just a string. If you look up the data format that the API uses, that should work too.
The list of cities was taken from the link I sent in my last post. The URL says sample but that’s not true – it links to a handful of files for download.

Or, complete:

Dim curweather  As OpenWeatherData = OpenWeather.GetCurrentWeather("id=4224681", API_ID, "units=imperial", "lang=en")

Hi Ulrich,

Thank you for your response. The Stockbridge that you listed was not for Stockbridge Massachusetts. There are many towns in the USA that have the same names in other states. I just think that OpenWeather has only a limited number of weather stations available around the world. They certainly have a lot listed, but they don’t state what states they are from, just the country. I think when they search the zip code they search for the closest station, which was Albany.

The WeatherUnderground had one for most any town, but unfortunately, they closed up.

Dark Sky has a reasonable priced API, but they have no wrapper available for Xojo.

there are something like 40+ Springfields in the USA :stuck_out_tongue:

I had no idea you guys like the Simpsons that much

most dont have a wrapper for Xojo. but they are mostly REST APIs/JSON output apis. so it is easy to integrate into a Xojo app.

Hi Scott,

I just don’t have the knowledge to do that… Unfortunately. My app is for Fire Departments so when they enter their Run forms, it can automatically record the weather conditions in their town at the time they are preparing the report. Temperature, Wind Speed and Wind Direction, etc…