Free Code: Accessing OpenWeatherMap.org API

Now that Google Weather has died and the yahooweather demo didn’t want to work for me, I looked for a substitute and found one free to use at openweathermap.org.
I have built a quick & dirty class to load and examine the weather and forecast JSON objects returned from the API. It’s nothing fancy and built for my own needs, but maybe it gives you a starting point for own classes. You need to enter an API key you can get for free here: http://openweathermap.org/api
See the documentation on that page for defining unique identifiers. Bulk download is not supported.
If you should extend this project, please feel free to add your methods. If there is interest I can put the classes on a repository as well.

The demo features no visible output besides via the result in debugger. There is no documentation, but I think the properties are quite self-explanatory.

https://dl.dropboxusercontent.com/u/21200221/Xojo/OpenWeather.zip

Ha! I have read about using Google rather than the built-in search when you’re looking for something in the forum :wink:
Thanks, Jean-Paul! Well, I wanted a class that is easier to view than a JSON anyway …

In case someone should ever need this class: It has moved to https://github.com/UBogun/Xojo-OpenWeatherMapOrg-API

I put demo apps for Desktop, Web, console and Rasberry Pi with a display into it, all sharing the same code.

Maybe not relevant but the ONLY weather source that gives vaguely correct forecasts for my place is yr.no and they seem to allow some level of free access if Google translate is not spewing out garbage again:

http://om.yr.no/verdata/free-weather-data/

Hi Ulrich,

Do you still have the original project file available for the Open Weather example you posted at the top of this thread? The example project that you moved to https://github.com/UBogun/Xojo-OpenWeatherMapOrg-API only seems to display the temperature and not the current conditions.

The project you show in the above picture appears to include current weather conditions. Im’ trying to replace the Yahoo Weather API in my app and not having a very successful time doing so.

It looks like the Open Weather may be the solution. Any help would be greatly appreciated.

Hi James,
I am afraid I lost the old project, but it should be easy to get the properties you want. The demo app may only display the temperature, but the complete OpenWeatherData object is created. You can access each other property of it.
You might want to check the Raspi demo file (even if you don’t use any Raspi) – it updates a display with several weather conditions every second.

I noticed there is a bug in the current code: RainItem and SnowItem should be checked for Nil by the …last3hours properties accessing them, and the same could be valid for the wind properties although I had no crash so far.
I wish I would find a description of the weather condition strings. If you should do, please tell me. EDIT: Found it!

I am using the time I wait for the final changes on a job project to work a bit on my Raspi project where I use the display like the demo but will add a forecast too. So there’ll probably be a few changes to the repository soon, especially with a few forecast simplifications.

If you can’t figure out how to get a certain weather parameter, let me know.

Hi Ulrich,

Thanks for getting back to me. I’m sorry you lost the project. I’m just trying to get a replacement for Yahoo. Just need the current weather conditions displayed in a TextField that refreshes every once in a while. Nothing too fancy.

I’m trying to figure it out with your other project example. I will keep working on it. Thanks for your help. :slight_smile:

This sounds basically like the desktop demo which you could expand with simply more labels, textfields and stuff accessing the OpenWeatherData properties.
The old demo I posted initially did only dump both objects to the text array, so there isn’t much lost.

Hi Ulrich,

I was able to figure out how to add more fields and also how to change for metric to imperial, but I’m trying to figure out how to change for the Location ID to a zip code lookup of the weather. Since all of my customers will be in the USA, that would be the best approach. I was wondering if you knew how to switch it from the Location Code to Zipcode or if you can use WOEID?

Hi James,

yes, you would only have to replace “id=xxxx” by “zip=zip code” (and an optional country code) in the getCurrentWeather method call.
You can even use the city name. See here:
http://openweathermap.org/current

EDIT: Adding convenience properties for metrics and language, as well as more location methods, is a good idea. Thx for the inspiration!

Hi Ulrich,

Thanks for all your help. I was able to change the location by changing the LocationID constant to the default value of zip=06798, but what if you want the user to enter their own zip code in a textbox

You can’t change the default value of the LocationID constant to “zip=txtMyZip.Text” Do you know how I can write it so that the user can change their zip code and still have their zip code in the LocationID default value?

Should be easy, James:

When the user has entered the ZIP code, simply call

CurrentData (As OpenWeatherData) = OpenWeather.GetCurrentWeather("zip="+Zipfield.text, API_ID, (optional parameters))

The properties locationID and API_ID are just convenience containers for your own projects. You don’t need to use them (and should blur the API-ID anyway if you put your ID into distributed code).
EDIT: You should be able to read to locationID then from the ID property of the WeatherData object if you want to store it.

Sorry to keep bothering you. I don’t know how to do that. I’m just not all that good at this.

  1. I create a method called CurrentData?
  2. The Method Parameters I enter (As OpenWeatherData) = OpenWeather.GetCurrentWeather(“zip=”+txtZipCode.text, API_ID)
  3. I create a button that the user clicks after the zip code has been entered into txtZipCode that calls the method?

I’m not doing something right here, because that does not work.

Never mind, James, and sorry for being unclear:

I don’t know the structure of your program but, basically, if you address the current weather data you need to create an OpenWeatherdata object. That’s what I meant above. And instead of creating it with only a call to OpenWeather.GetCurrentWeather(), you forward the properties above to the shared GetCurrentWeather method of the OpenWeather module.

The button was only the idea that you GUI would have some kind of “show me the weather” button, or at least a “test zip” button that looks if the ZIP resolves.

I just figured it out Ulrich.

The first line in the RefreshTime Method should be:

dim curweather As OpenWeatherData = OpenWeather.GetCurrentWeather(“zip=”+txtZipCode.text, API_ID, “units=imperial”, “lang=en”)

That does the trick. Thanks so very much for all your help. I’m working on an app for firefighters and having the current weather conditions at the time of the call is an important factor in what I’m trying to do. When the Yahoo weather went down, I didn’t know what else to use. I really appreciate you coming up with this alternative. I think its really going to help.

Thanks so much again. I appreciate you taking the time to help!!

You’re very welcome, James, and it’s good to know my code serves a good purpose somewhere. Thanks a lot!

Wuderground Class for Xojo:

Includes:
Alerts
Almanac
Animated Radar
Animated Radar / Animated Satellite
Astronomy
Conditions
Current Hurricane
Forecast
Forecast: 10-Day
GeoLookup
History
Hourly: 10-Day
Planner
Radar
Radar / Satellite
Raw Tide
Tide
Webcams
Yesterday

Can output data in JSON, GIF, PNG, SWF . Free to use in your software, just credit me, or Xojo Developer’s Spot somewhere in your about.

@James Redway - Hope the Maps class is helping with your endeavor still :slight_smile:

I was trying to use Matthew Combatti’s Weather Underground Class for getting the local weather conditions for my app. He posted the project file at:

link text

When you run the project and request the “Conditions,” you get the conditions all jammed together is a TextField like what is shown below.

[quote]?{? “response”: {? “version”:“0.1”,? “termsofService”:“http://www.wunderground.com/weather/api/d/terms.html”,? “features”: {? “conditions”: 1? }??}? ,?“current_observation”: {???“image”: {???“url”:“http://icons.wxug.com/graphics/wu2/logo_130x80.png",???“title”:"Weather Underground”,???“link”:“http://www.wunderground.com”???},???“display_location”: {???“full”:“Spartanburg, SC”,???“city”:“Spartanburg”,???“state”:“SC”,???“state_name”:“South Carolina”,???“country”:“US”,???“country_iso3166”:“US”,???“zip”:“29301”,???“magic”:“1”,???“wmo”:“99999”,???“latitude”:“34.93458176”,???“longitude”:"-81.96445465",???“elevation”:“251.00000000”???},???“observation_location”: {???“full”:“I-26 at Reidville Road, Spartanburg, South Carolina”,???“city”:“I-26 at Reidville Road, Spartanburg”,???“state”:“South Carolina”,???“country”:“US”,???“country_iso3166”:“US”,???“latitude”:“34.910179”,???“longitude”:"-81.998116",???“elevation”:“779 ft”???},???“estimated”: {???},???“station_id”:“KSCSPART1”,???“observation_time”:“Last Updated on September 20, 9:53 AM EDT”,???“observation_time_rfc822”:“Tue, 20 Sep 2016 09:53:02 -0400”,???“observation_epoch”:“1474379582”,???“local_time_rfc822”:“Tue, 20 Sep 2016 09:53:50 -0400”,???“local_epoch”:“1474379630”,???“local_tz_short”:“EDT”,???“local_tz_long”:“America/New_York”,???“local_tz_offset”:"-0400",???“weather”:“Clear”,???“temperature_string”:“75.0 F (23.9 C)”,???“temp_f”:75.0,???“temp_c”:23.9,???“relative_humidity”:“82%”,???“wind_string”:“Calm”,???“wind_dir”:“North”,???“wind_degrees”:351,???“wind_mph”:0.0,???“wind_gust_mph”:0,???“wind_kph”:0,???“wind_gust_kph”:0,???“pressure_mb”:“1020”,???“pressure_in”:“30.11”,???“pressure_trend”:"+",???“dewpoint_string”:“69 F (21 C)”,???“dewpoint_f”:69,???“dewpoint_c”:21,???“heat_index_string”:“NA”,???“heat_index_f”:“NA”,???“heat_index_c”:“NA”,???“windchill_string”:“NA”,???“windchill_f”:“NA”,???“windchill_c”:“NA”,???“feelslike_string”:“75.0 F (23.9 C)”,???“feelslike_f”:“75.0”,???“feelslike_c”:“23.9”,???“visibility_mi”:“10.0”,???“visibility_km”:“16.1”,???“solarradiation”:"–",???“UV”:“3”,“precip_1hr_string”:“0.00 in ( 0 mm)”,???“precip_1hr_in”:“0.00”,???“precip_1hr_metric”:" 0",???“precip_today_string”:“0.00 in (0 mm)”,???“precip_today_in”:“0.00”,???“precip_today_metric”:“0”,???“icon”:“clear”,???“icon_url”:“http://icons.wxug.com/i/c/k/clear.gif",???“forecast_url”:“http://www.wunderground.com/US/SC/Spartanburg.html”,???“history_url”:“http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=KSCSPART1”,???“ob_url”:“http://www.wunderground.com/cgi-bin/findweather/getForecast?query=34.910179,-81.998116”,???“nowcast”:"”??}?}?
[/quote]

I was wondering if anyone knew how to extract this text into a more readable form, so certain weather conditions can be extracted and redisplayed in a neat format such as:

Temperature: -------
Wind Speed: ------
Wind Direction: -----
ect…

Any suggestions would be greatly appreciated.

It looks like if you get the encoding right it would be able to use the JSON functions in Xojo.

There lies the problem. I don’t see how to use a JSON function that would allow me to extract certain elements from the Textfield, and format them neatly.