This tutorial will show how to get weather data using REST API.
Let’s assume your need to know the current weather in the asset location. You may use the weather info for certain data processing logic or just to track history and enable visualization of this info on the dashboard.
In this tutorial we will configure ThingsBoard Rule Engine to automatically get weather information using REST API. You can use this tutorial as a basis for more complex tasks.
We assume you have completed the following guides and reviewed the articles listed below:
Add Asset entity in ThingsBoard. Its name is Building A and its type is building.
Note:If you have Professional Edition, you will need to add asset to customer using Customer Hierarchy the following way:
In order to get weather data you should register on a website which will provide it. In this case OpenWeatherMap will be used.
After signing up there go to this page to get your api key.
To perform REST API call we need the following URL parameters: API key, longitude, latitude, and units of measurement.
We suggest adding an API key parameter to the customer server-side attribute and other parameters to the asset server-side attributes.
Customer attribute should look like this:
Add the attribute as following:
Field | Data Type | Input Data |
APPID | String | (an API key you got from OpenWeatherMap) |
Asset attributes should look like this:
Field | Data Type | Input Data |
latitude | Double | latitude of an asset |
longitude | Double | longitude of an asset |
units | String | "metric" for meters per second wind speed and Celsius temperature, "imperial" for miles per hour wind speed and Fahrenheit temperature, empty for meters per second wind speed and Kelvin temperature |
In this example the coordinates of New York City and metric units will be used.
In this section, we explain the purpose of each node in this tutorial. There will be one rule chain involved:
The following screenshot show how the above Rule Chain should look like:
Download and import attached json file with a rule chain for this tutorial. Be aware that you need to set the asset you created in the beginning as an originator in the leftmost generator node.
The following section shows you how to create this rule chain from scratch.
Go to Rule Chains -> Add new Rule Chain
Configuration:
New Rule Chain is created. Press Edit button and configure Chain.
In this rule chain, you will create 5 nodes as it will be explained in the following sections:
Field | Value |
Name | Generate requests |
Message count | 0 |
Period in seconds | 15 |
Originator type | Asset |
Asset | Building A |
Generate function | return { msg: {}, metadata: {}, msgType: "POST_TELEMETRY_REQUEST" }; |
Field | Value |
Name | Get customer API key |
Latest telemetry | False |
Source attribute | APPID |
Target attribute | APPID |
Field | Value |
Name | Latitude/Longitude |
Server attributes | latitude, longitude, units |
Field | Value |
Name | Get Weather Data |
Endpoint URL pattern | http://api.openweathermap.org/data/2.5/weather?lat=${ss_latitude}&lon=${ss_longitude}&units=${ss_units}&APPID=${APPID} |
Request method | GET |
Use simple client HTTP factory | False |
var newMsg = {
"outsideTemp": msg.main.temp,
"outsideMaxTemp": msg.main.temp_max,
"outsideMinTemp": msg.main.temp_min,
"outsideHumidity": msg.main.humidity,
};
return {msg: newMsg, metadata: metadata, msgType: msgType};
Download and import attached json file with a dashboard for this tutorial.
The dashboard should look like this:
Getting started guides - These guides provide quick overview of main ThingsBoard features. Designed to be completed in 15-30 minutes.
Installation guides - Learn how to setup ThingsBoard on various available operating systems.
Connect your device - Learn how to connect devices based on your connectivity technology or solution.
Data visualization - These guides contain instructions how to configure complex ThingsBoard dashboards.
IoT Data analytics - Learn how to use rule engine to perform basic analytics tasks.
Hardware samples - Learn how to connect various hardware platforms to ThingsBoard.
Advanced features - Learn about advanced ThingsBoard features.
Contribution and Development - Learn about contribution and development in ThingsBoard.