09-emq-broker.md 5.6 KB
Newer Older
1 2 3 4 5
---
sidebar_label: EMQX Broker
title: EMQX Broker writing
---

6
MQTT is a popular IoT data transfer protocol. [EMQX](https://github.com/emqx/emqx) is an open-source MQTT Broker software. You can write MQTT data directly to TDengine without any code. You only need to setup "rules" in EMQX Dashboard to create a simple configuration. EMQX supports saving data to TDengine by sending data to a web service and provides a native TDengine driver for direct saving in the Enterprise Edition. Please refer to the [EMQX official documentation](https://www.emqx.io/docs/en/v4.4/rule/rule-engine.html) for details on how to use it.).
7 8 9 10 11 12 13 14 15 16 17 18

## Prerequisites

The following preparations are required for EMQX to add TDengine data sources correctly.
- The TDengine cluster is deployed and working properly
- taosAdapter is installed and running properly. Please refer to the [taosAdapter manual](/reference/taosadapter) for details.
- If you use the emulated writers described later, you need to install the appropriate version of Node.js. V12 is recommended.

## Install and start EMQX

Depending on the current operating system, users can download the installation package from the [EMQX official website](https://www.emqx.io/downloads) and execute the installation. After installation, use `sudo emqx start` or `sudo systemctl start emqx` to start the EMQX service.

19
Note: this chapter is based on EMQX v4.4.5. Other version of EMQX probably change its user interface, configuration methods or functions.
20

D
dingbo 已提交
21
## Create Database and Table
22

D
dingbo 已提交
23
In this step we create the appropriate database and table schema in TDengine for receiving MQTT data. Open TDengine CLI and execute SQL bellow: 
24 25

```sql
D
dingbo 已提交
26 27 28
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);
29 30 31 32 33 34
```

Note: The table schema is based on the blog [(In Chinese) Data Transfer, Storage, Presentation, EMQX + TDengine Build MQTT IoT Data Visualization Platform](https://www.taosdata.com/blog/2020/08/04/1722.html) as an example. Subsequent operations are carried out with this blog scenario too. Please modify it according to your actual application scenario.

## Configuring EMQX Rules

35
Since the configuration interface of EMQX differs from version to version, here is v4.4.5 as an example. For other versions, please refer to the corresponding official documentation.
36 37 38 39 40

### Login EMQX Dashboard

Use your browser to open the URL `http://IP:18083` and log in to EMQX Dashboard. The initial installation username is `admin` and the password is: `public`.

D
dingbo 已提交
41
![TDengine Database EMQX login dashboard](./emqx/login-dashboard.webp)
42 43 44 45 46

### Creating Rule

Select "Rule" in the "Rule Engine" on the left and click the "Create" button: !

D
dingbo 已提交
47
![TDengine Database EMQX rule engine](./emqx/rule-engine.webp)
48 49 50

### Edit SQL fields

D
dingbo 已提交
51 52 53 54 55 56 57 58 59
Copy SQL bellow and paste it to the SQL edit area:

```sql
SELECT
  payload
FROM
  "sensor/data"
```

D
dingbo 已提交
60
![TDengine Database EMQX create rule](./emqx/create-rule.webp)
61 62 63

### Add "action handler"

D
dingbo 已提交
64
![TDengine Database EMQX add action handler](./emqx/add-action-handler.webp)
65 66 67

### Add "Resource"

D
dingbo 已提交
68
![TDengine Database EMQX create resource](./emqx/create-resource.webp)
69 70 71 72 73

Select "Data to Web Service" and click the "New Resource" button.

### Edit "Resource"

D
dingbo 已提交
74
Select "WebHook" and fill in the request URL as the address and port of the server running taosAdapter (default is 6041). Leave the other properties at their default values.
75

D
dingbo 已提交
76
![TDengine Database EMQX edit resource](./emqx/edit-resource.webp)
77 78 79

### Edit "action"

D
dingbo 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
Edit the resource configuration to add the key/value pairing for Authorization. If you use the default TDengine username and password then the value of key Authorization is:
```
Basic cm9vdDp0YW9zZGF0YQ==
```

Please refer to the [ TDengine REST API documentation ](/reference/rest-api/) for the authorization in details. 
 
Enter the rule engine replacement template in the message body:

```sql
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}
)
```
105

D
dingbo 已提交
106
![TDengine Database EMQX edit action](./emqx/edit-action.webp)
107

D
dingbo 已提交
108
Finally, click the "Create" button at bottom left corner saving the rule.
109 110 111
## Compose program to mock data

```javascript
D
dingbo 已提交
112
{{#include docs/examples/other/mock.js}}
113 114 115 116
```

Note: `CLIENT_NUM` in the code can be set to a smaller value at the beginning of the test to avoid hardware performance be not capable to handle a more significant number of concurrent clients.

D
dingbo 已提交
117
![TDengine Database EMQX client num](./emqx/client-num.webp)
118 119 120 121 122 123 124 125

## Execute tests to simulate sending MQTT data

```
npm install mqtt mockjs --save ---registry=https://registry.npm.taobao.org
node mock.js
```

D
dingbo 已提交
126
![TDengine Database EMQX run mock](./emqx/run-mock.webp)
127 128 129 130 131

## Verify that EMQX is receiving data

Refresh the EMQX Dashboard rules engine interface to see how many records were received correctly:

D
dingbo 已提交
132
![TDengine Database EMQX rule matched](./emqx/check-rule-matched.webp)
133 134 135 136 137

## Verify that data writing to TDengine

Use the TDengine CLI program to log in and query the appropriate databases and tables to verify that the data is being written to TDengine correctly:

D
dingbo 已提交
138
![TDengine Database EMQX result in taos](./emqx/check-result-in-taos.webp)
139 140 141

Please refer to the [TDengine official documentation](https://docs.taosdata.com/) for more details on how to use TDengine.
EMQX Please refer to the [EMQX official documentation](https://www.emqx.io/docs/en/v4.4/rule/rule-engine.html) for details on how to use EMQX.