The company OpenWeather provides weather data and forecasts on its website OpenWeatherMap https://openweathermap.org for retrieval. The data can be integrated into your own applications. The retrieval is possible via the offered API via REST interface, whereby the integration can be automated. In addition to detailed long-term forecasts, which are subject to a charge, current weather data and short-term forecasts can be called up free of charge.

REST Open Weather with OPC Router

REST Open Weather with OPC Router

Using the example of OpenWeatherMap, we show the use of OPC Router REST Plug-in to call REST API interfaces and show how to

  • retrieve weather data from OpenWeatherMap
  • JSON weather data are processed
  • Data can be stored in an SQL database using the SQL Server plug-in
  • Weather data can be sent by telegram with the Telegram Plug-in

Weather API, the REST interface to weather data

A REST interface is a data interface based on web technologies. Data can be retrieved from the REST API via the https protocol. Technically, a web page is called with a URL, just like in a browser. Parameters are passed to the Web page call, which are appended to the URL as parameter fields. The meaning of the parameters is defined in the respective REST API documentation. For OpenWeather’s Weather API, the documentation is available online at https://openweathermap.org/api. For each form of data retrieval (current weather, forecast daily / 5 days / 16 days / 30 days, etc.) there is a separate REST API endpoint (call URL).
An example of retrieving the current weather in London looks like this: Result data in JSON format (call returns test data only)

The parameter “q” contains the city and the country code. The REST API endpoint is “weather”.  The parameters are appended to the endpoint via “?” and chained with “&”. Each parameter is declared with name and value, separated by a “=”.
The parameter “appid” is used by the Weather API as authentication to identify the retrieving client. For your own test setup, you must request your own App-ID to make data retrievals. The App-ID or the API KEY can be obtained by registering on the following page https://home.openweathermap.org/users/sign_up. The API KEY can then be viewed in the user area under “API keys”.
The description for retrieving the current weather of any location is documented here: https://openweathermap.org/current

Connection of the REST interface in the OPC Router

In order to call the REST endpoint with the OPC Router, the first step is to introduce the REST Plug-in to the HOST. The following entry is created for this:

REST Plug-in
REST to Endpoint

Now that the host is known for the call, a REST transfer object can be used in an OPC Router connection that uses the created host. The respective endpoint is then selected in the transfer object and the parameters are determined. For the retrieval of the current weather the “weather” endpoint is used and in this case the location is determined by the location ID (here Schenefeld with ID=2839763).

To find the ID of your location, simply search for your city on the homepage (https://openweathermap.org). In the prediction page for your city, the ID can be found in the URL: https://openweathermap.org/city/2839763
GET is used here as the REST method and the parameters are passed as “QueryString”. The documentation for retrieving the current weather by ID can be found here: https://openweathermap.org/current#cityid

Retrieval of weather data by REST as JSON document

The REST API call returns a JSON document with the weather data. This JSON document can be transferred to a JSON transfer object in the OPC Router connection:

REST JSON read
REST OpenWeather JSON

Once the connection has been executed, the JSON structure information is available. Therefore, a 30 second trigger is already placed in the connection and the connection has been published so that it can be executed.
After the connection has been executed, the “Suggestions” function can be used in the configuration of the JSON transfer object to determine the available fields from the JSON document and make them available for selection. The arrow buttons can be used to select fields for use in the connection.

For this example, some elementary information about the current weather is selected from the JSON document (temperature, air pressure, humidity, min/max daily temperature, wind speed and direction). After selecting the data fields, the transfer object is displayed accordingly.

REST JSON read OK

Storage of weather data in a SQL database

SQL Server Client Configuration

The retrieved weather data can now be further processed as desired. In the example we will store the data in a database and send it to a messenger service.
The SQL Server Plug-in of the OPC Router can be used to store the data in an MS SQL database. In the plug-in configuration the usual connection parameters of the SQL database are specified and then tables and stored procedures are available in the selection of the transfer object. The example shows a database “Weather” with a table “TBL_WEATHER_CURRENT” as storage location for the current weather data. The table is selected and the columns to be used are marked.

As a result, the SQL Server transfer object is ready and connected to the fields of the JSON document. If the connection trigger is now triggered, the connection saves the weather data in the SQL Server table. The trigger is now set to a morning transfer at 6:15 am.
From the table the data is now available to other applications.

REST JSON to SQL

Sending weather information by telegram

Telegram Bot

The Telegram Plug-in can be used to distribute weather data not only via SQL, but also as an example directly via push message to a mobile device (e.g. mobile phone). With the Telegram Plug-in so-called Telegram Bots are connected. On the one hand the bot can be used to send active commands from the mobile user to the OPC Router, on the other OPC Router can also send active messages to registered users.
For our example we have generated a telegram bot, which is used for the distribution of weather information. A bot is generated via the Telegram app:

To send a message with the data to the logged in user, a telegram transfer object is now drawn into the connection. The transfer object sends the text passed to it to all users. To make the text easy to read for the users, a “Text Replace” transfer object is inserted by inserting the values from the JSON document into a text. For conversion from Kelvin to degrees Celsius a calculator transfer object is used.

The complete OPC Router connection

End result: From OpenWeather to the SQL Server and to Telegram

Database table with entries

As a result, a data set is written to the SQL database at the specified trigger time (also cyclically or at several times possible) and a telegram message is sent.

In addition to the SQL Server Plug-in and the Telegram Plug-in, many other plug-ins are available with which the weather data can be transferred to various systems in order to provide information or optimize processes depending on the weather.

Telegram client with messages from OPC Router and formatted weather data as text message

OPC Router Test

Try it yourself and download your trial version of the OPC Router here today:

Test now!