Description of OpenWeather’s capabilities and the processes of obtaining an API Key
Today, there are many different services available for obtaining weather data. On a smartphone, a user receives weather information based on geolocation. The user’s location coordinates are taken from the GPS sensor or are calculated based on WiFi networks or the positions of mobile operator towers.
One of these services is OpenWeather. This service provides us with an API for getting the weather data for the current town. There is also the possibility of getting weather data based on GPS coordinates. The service offers data on the current air temperature, atmospheric pressure, country code, etc. Additionally, it allows users to download beautiful images that show the current weather conditions (cloudiness, rain, sunny, etc.).
In our article, we will explain how we can use the capabilities of the OpenWeather service in our Embarcadero Delphi FMX application to obtain weather data for a selected city.
To start working with the OpenWeather service, we will need to register and obtain an API key. To do this, we should go to the official OpenWeather website by following the link https://openweathermap.org/
To register we need to open the “Sign In” tab.
Now let’s click “Create an Account”.
To register, we will need to provide an email and a password. We also need to create and enter a name (for example, SoftacomOWMTest), select the necessary checkboxes, and click “Create Account”.
Next, we need to enter the company name, select a purpose (for example, Education/Science), and click “Save”.
A notification will be displayed as a confirmation of successful registration. Additionally, a relevant email will be sent to your inbox.
To complete the registration, click “Verify your email” in the email received from OpenWeather and confirm your account.
Next, let’s select “My API keys”.
Now, we should copy our API key and save it for further use in our Embarcadero Delphi FMX application (the Key field).
Capabilities of the OpenWeather API for retrieving weather data in a selected city
Let’s explore the capabilities of the OpenWeather API for obtaining weather data for a selected city. First, we will test the service using a browser.
Now, we need to go to the API tab.
Next, we should find “Current Weather Data” and click on “API doc”.
In this case, we will work with the URL link for retrieving weather data for a city.
Let’s take a closer look at the URL link. Here, we will need to input the city and our previously obtained API key.
To test the OpenWeather service, we should enter our city and API key.
As a result, we will receive a JSON response with weather data for the city of Berlin.
The service will provide us with an icon code representing the current weather conditions.
With the help of this code, we can download the corresponding weather icon. To obtain the URL link for downloading the image of the current weather condition, click “Weather condition codes”.
Now let’s click “Weather icons”.
As a result, we can see an image showing the current weather conditions.
Utilizing the OpenWeather API capabilities to receive information about the current weather conditions in a city within our app
Let’s consider the possibilities of using our Embarcadero Delphi FMX application for retrieving weather data from the OpenWeather service. To send GET requests to the OpenWeather service and receive weather information in JSON format for our chosen city, as well as to retrieve the icon showing the current weather condition, we will use the TNetHttpClient component (the Net tab).
To input the city for which we want to get weather information, we will use the TEdit component.
To display certain characteristics of the current weather in the city (such as air temperature, atmospheric pressure, humidity, etc.), we will use the TMemo component.
To display the icon representing the current weather in the city, we will use the TImage component.
All the logic for interacting with the OpenWeather service is implemented in the onClick event handler (the TButton class).
In the private section of the TForm1 class (of our form), we will declare the following objects:
The string field FOWMRequest represents the URL string of the OpenWeather service. This string also contains the selected city and our API key. The string field FIconName contains the name of the current weather icon, which is included in the JSON response from OpenWeather. We will also declare two objects, FTask1 and FTask2 (of the ITask type). FTask1 is the first task, which is executed asynchronously. Here, we need to perform a GET request to OpenWeather, followed by receiving a JSON response from it. Next, we should parse the JSON response to extract weather information (air temperature, atmospheric pressure, icon name, etc.) and then display this information to the user.
The second task, FTask2, will also be executed asynchronously. Here, we will make a GET request to load the icon for the current weather conditions.
The string field FOWMUrl will contain only the URL for accessing the OpenWeather service (https://api.openweathermap.org/data/2.5/weather?q=). The string field FOWMApiKey will contain our API key. In the OnCreate method of the main form, we will perform the initial setup. The decimal separator will be a dot (FormatSettings.DecimalSeparator). We should also assign the URL and the API key.
Let’s take a closer look at the handler for the “Get Weather” button. We will declare the following objects in the onClick handler.
The string variable City will contain the name of the city for which we need to get the weather information.
Variables of the type Double, such as Degrees and Kelvins, store the air temperature in Celsius and Kelvin degrees, respectively. The OpenWeather service provides the air temperature in Kelvin. Therefore, we will need to convert Kelvin to Celsius, which will make the interaction with our app more convenient for users,
The Resp object of the TMemoryStream class stores the current weather condition icon as a byte stream.
The Response object of the TStringStream class stores the JSON response from the OpenWeather service with weather information for the city.
The JSON object of the TJSONObject class contains all the structures and objects of the JSON response from OpenWeather. The JSONMain object of the TJSONObject class contains the data from the “main” structure of the JSON response from OpenWeather.
The JSONCountry object of the TJSONObject class contains the data from the “sys” structure of the JSON response from OpenWeather (it includes information about the current country).
We should enter the desired city using the TEdit component (Edit1.Text). The string field FOWMRequest will contain the URL of the OpenWeather service (FOWMUrl field), as well as our city (City variable) and API key (FOWMApiKey field).
Next, we will execute the Get method (specifically, a GET request to the OpenWeather service to obtain weather data for the city) of the NetHttpClient1 object. This method takes two input parameters. In our case, this will be FOWMRequest (we should send the request to the OpenWeather service as a string, for example, https://api.openweathermap.org/data/2.5/weather?q=Berlin&appid=Your_API_Key) and Response (it contains the JSON response from the OpenWeather server with the weather data for the city).
Then let’s convert the JSON response from the OpenWeather service received in the form of a string (Response.DataString) to the TJSONObject type (using the ParseJSONValue method for this).
Next, we should read the “main” structure and assign the result to the JSONMain object. We will read the “weather” array and assign the value to the JSONDetail object. We will also read the “sys” structure and assign the result to the JSONCountry object.
We will obtain the air temperature in Kelvin by default from the OpenWeather service. To convert it to Celsius, it is necessary to subtract 273.15.
Now, we will display such parameters as air temperature (in Celsius degrees), country code, cloudiness, humidity, and atmospheric pressure in the multi-line field Memo1 (the TMemo class). We should save the current weather condition icon code in the FIconName field.
We will start our first task FTask1 (let’s perform a GET request and receive the JSON response followed by its parsing ).
Our second task FTask2 will be to load the current weather condition icon for the city. The second task will start running only after the first task (FTask1) is completed. This will be ensured by the line of code Task.WaitForAny(FTask1), which means that it will wait for the first task to finish.
The NetHttpClient1.Get method will take a string with the URL of our image and the Resp object (TMemoryStream) as two input parameters. The result of the Get method (the loaded weather condition icon as a byte stream) will be saved in the Resp object. Next, we will need to display the icon from Resp (TMemoryStream) in the Image1 component (TImage). To do this, we will use the Image1.Bitmap.LoadFromStream(Resp) method. Finally, we will start our second task (if FTask2.Status = TTaskStatus.Created then FTask2.Start).
Let’s demonstrate the work of our Embarcadero Delphi FMX app.