# Save device data to InfluxDB 2.0 using the Data Integrations
InfluxDB (opens new window) is an open source database for storing and analyzing time-series data, with built-in HTTP API, support for SQL-like statements, and unstructured features that are very friendly for users. Its powerful data throughput and stable performance make it very suitable for the IoT field.
Through the EMQX Cloud Data Integrations, we can customize the template file, and then convert the MQTT message in JSON format to Measurement and write it to InfluxDB.
In this guide, we will complete the creation of an InfluxDB data integration to achieve the following functions:
- Record the temperature and humidity of Prague. When there is a temperature and humidity message sent to the emqx/test topic, the Data Integrations will be triggered to record this data in InfluxDB.
In order to achieve this function, we will complete the following 4 tasks:
- Login and initialize InfluxDB
- Create a resource
- Create a rule and response action
- Connect to MQTT X to send data
- View results in the InfluxDB console
TIP
Before using the Data Integrations, create a deployment first. For professional deployment users: please complete Peering Connection first, and ensure that the servers involved in the following are established in the VPC under the peering connection. All the IP mentioned below refer to the intranet IP of the resource For basic deployment users: There is no need to complete peering connection, and the IP mentioned below refers to the public IP of the resource
# 1. Login and initialize InfluxDB
# Log in to the InfluxDB account
# Create Bucket
After logging in to the InfluxDB's console, go to the Load Data
page and create a new bucket.
Name the bucket and click Create
.
# Generate Token
Go back to the Load Data
page and generate a new token. At this time, we will generate a token with full access.
Once the token is created, you could choose to activate/deactivate the token.
# 2. Create Webhook Resource
Go to EMQX Cloud Console (opens new window) and go to the Data Integrations
page
Click on the WebHook
card to create a new resource. Fill in the request URL as follows:
Url: https://us-east-1-1.aws.cloud2.influxdata.com
Click Test button when configuration is complete, then click New button to create a resource when it is available.
# 3. Create Rule
After the resource is successfully created, you can return to the data integration page and find the newly created resource, and click create rule.
Our goal is that as long as the emqx/test topic has monitoring information, the engine will be triggered. Certain SQL processing is required here:
- Only target the topic "emqx/test"
- Get the three data we need: location, temperature, humidity
According to the above principles, the SQL we finally get should be as follows:
SELECT
payload.location as location,
payload.temp as temp,
payload.hum as hum
FROM "emqx/test"
2
3
4
5
You can click SQL Test under the SQL input box to fill in the data:
- topic: emqx/test
- payload:
{
"location": "Prague",
"temp": 26,
"hum": 46.4
}
2
3
4
5
Click Test to view the obtained data results. If the settings are correct, the test output box should get the complete JSON data as follows:
{
"hum": 46.4,
"location": "Prague",
"temp": 26
}
2
3
4
5
Tip
If the test fails, please check whether the SQL is compliant and whether the topic in the test is consistent with the SQL filled in.
# 4. Create Action
After completing the rule configuration, click Next to configure and create an action. Then add the request headers as following:
Method: POST
Path: path <your_influxdb_path>, example: '/api/v2/write?org=tifidol259%40revutap.com&bucket=emqx&precision=ns'
Headers:
Content-Encoding: identity
Content-Type: text/plain
Accept: application/json
Authorization: Token <your_influxdb_token>
User-Agent: Telegraf
2
3
4
5
6
7
8
Enter the Body as follows:
temp_hum,location=${location} temp=${temp},hum=${hum}
where:
Measurement: temp_hum
Tags: location
Fields: temp, hum
2
3
# 5. Connect to MQTT X to send data
We recommend you to use MQTT X, an elegant cross-platform MQTT 5.0 desktop client to subscribe/publish messages.
If you are using EMQX Cloud for the first time, you can go to Deployment Connection Guide to view the MQTT client connection and test guide
We will be using the MQTT X desktop version in this tutorial.
In the MQTT X console, click on the add
button and fill in the deployment information to connect to the deployment
Enter the topic name and payload message to publish the message
# 6. View results in influxDB console
Go back to the InfluxDB console and go to the Data Explorer
page
Select the bucket and filter the measurement, fields, then InfluxDB will generate the graphs for you