ESP32 HTTP GET – get weather condition from Weather Underground

Beside of local measured data, a weather station should as well display the current situation from a “professional” weather broadcaster. There are many API’s available from different online weather channels. I chose Weather Underground, because they offer a free account which is not as limited as e.g. Accuweather, which limits the number of accesses per day.

The API of Weather Underground is well documented at their API website, they offer to get current conditions, forecasts and even satellite images. To keep it simple I decided to just get the current conditions from a weather station close to my house.

To use the Weather Underground API you need to register on their website and get an API key.

To get the data, a HTTP GET request is send to their API address. The data is then sent back as a JSON object. This JSON object is then parsed by the ESP32 to extract the information.

To send the request, the HTTPClient class is needed and some information needed to access the API is created

To get information from Weather Underground the URL must follow a specified format. The URL formats are explained in the WU API documentation. To get the current weather of San Francisco the URL looks like

http://api.wunderground.com/api/YOUR_API_KEY/conditions/q/CA/San_Francisco.json

Well in my case I want the weather from Paranaque in the Philippines. The correct URL then is

http://api.wunderground.com/api/YOUR_API_KEY/conditions/q/PH/Paranaque_City.json

Here is how the URL is created

Next is to get the response from the WU API with a HTTP GET. First we setup a timeout, initiate the connection and then start the GET. If there is an error, a negative value will be returned

Check the result. If there is an error, a negative value will be returned. This might happen if the internet connection is interrupted or the WU API is not available. If the GET was successful, we still need to check if the returned value is a 200 (site could be loaded). In any other case (e.g. 404 -> site not found) we received no usable data. If everything is ok, we store the received data in the String payload.

The returned data is in JSON format, but it is with additional spaces at the beginning and end. We just get rid of them.

And there is really a lot of information in it, so we need to parse it to get the parts that we need.

I extract the time, temperature, humidity, weather description and wind information from the JSON. The result looks like

And here is the code to display the data on the TFT

Then the icon is drawn on the TFT.

As explained in the ESP32 SPI TFT display post, the bitmap for the different icons are stored in the code. To get the right bitmap, the “icon” entry in the JSON data must be parsed. Unfortunately the “icon” entry is a string and not an index. To make it easier to find the right icon, all possible “icon” strings are stored in an array

The pointer to the bitmap data is stored in an array as well

To get the right icon bitmap, the string of the “icon” entry in the JSON data is compared with the strings in ugIconName[]. Once we found a match, we have an index which is used get the matching bitmap array pointer from ugIconArray[] and draw the icon.

 

Loading...
Facebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published. Required fields are marked *

Free Link Directory