docs/en/10-third-party/01-collection/09-emq-broker.md
MQTT is a popular IoT data transmission protocol, and EMQX is an open-source MQTT Broker software. Without any coding, you can directly write MQTT data into TDengine by simply configuring "rules" in the EMQX Dashboard. EMQX supports saving data to TDengine by sending it to a web service and also provides a native TDengine driver in the enterprise version for direct saving.
To enable EMQX to properly add a TDengine data source, the following preparations are needed:
Users can download the installation package from the EMQX official website according to their operating system and execute the installation. After installation, start the EMQX service using sudo emqx start or sudo systemctl start emqx.
Note: This article is based on EMQX v4.4.5. Other versions may differ in configuration interface, configuration methods, and features as the version upgrades.
Create the corresponding database and table structure in TDengine to receive MQTT data. Enter the TDengine CLI and copy and execute the following SQL statement:
CREATE DATABASE test;
USE test;
CREATE TABLE sensor_data (ts TIMESTAMP, temperature FLOAT, humidity FLOAT, volume FLOAT, pm10 FLOAT, pm25 FLOAT, so2 FLOAT, no2 FLOAT, co FLOAT, sensor_id NCHAR(255), area TINYINT, coll_time TIMESTAMP);
Since the configuration interface differs across EMQX versions, this section is only an example for v4.4.5. For other versions, please refer to the respective official documentation.
Open the URL http://IP:18083 in a browser and log in to the EMQX Dashboard. The initial username is admin and the password is: public.
Select "Rule Engine (Rule Engine)" on the left, then "Rule (Rule)" and click the "Create (Create)" button:
Copy the following content into the SQL edit box:
SELECT
payload
FROM
"sensor/data"
Where payload represents the entire message body, sensor/data is the message topic selected for this rule.
Select "Send Data to Web Service" and click the "Create Resource" button:
Select "WebHook" and fill in the "Request URL" with the address provided by taosAdapter for REST services. If taosadapter is started locally, the default address is http://127.0.0.1:6041/rest/sql.
Please keep other properties at their default values.
Edit the resource configuration, adding an Authorization key/value pair. The default username and password corresponding Authorization value is:
Basic cm9vdDp0YW9zZGF0YQ==
For related documentation, please refer to TDengine REST API Documentation.
Enter the rule engine replacement template in the message body:
INSERT INTO test.sensor_data VALUES(
now,
${payload.temperature},
${payload.humidity},
${payload.volume},
${payload.PM10},
${payload.pm25},
${payload.SO2},
${payload.NO2},
${payload.CO},
'${payload.id}',
${payload.area},
${payload.ts}
)
Finally, click the "Create" button at the bottom left to save the rule.
{{#include docs/examples/other/mock.js}}
Note: In the code, CLIENT_NUM can be set to a smaller value at the start of the test to avoid hardware performance not being able to fully handle a large number of concurrent clients.
npm install mqtt mockjs --save --registry=https://registry.npm.taobao.org
node mock.js
Refresh the EMQX Dashboard rule engine interface to see how many records were correctly received:
Use the TDengine CLI program to log in and query the relevant database and table to verify that the data has been correctly written to TDengine:
For detailed usage of EMQX, please refer to EMQX Official Documentation.