Introduction

ThingsBoard is an open source IoT platform for data collection, processing, visualization and device management. It supports device connectivity via protocols such as MQTT , CoAP , and HTTP, and supports cloud and private deployments. Use rich server-side APIs to securely provision, monitor, and control your IoT entities, defining relationships between your devices, assets, customers, or any other entity. Collect and store telemetry data in a scalable and fault-tolerant manner, visualize your data with built-in or custom widgets and flexible dashboards, and share the Dashboard interface with your customers.

This article will use ThingsBoard Cloud combined with EMQ's fully managed MQTT cloud service - EMQX Cloud , to introduce how to integrate a third-party MQTT Broker in ThingsBoard and customize the Dashboard UI to access MQTT data.

Prepare

Since we are using ThingsBoard Cloud, we do not need to download and install, just visit https://thingsboard.cloud/signup to register and log in to get related services. In addition to using the ThingsBoard cloud service, users can also choose private deployment for download and installation .

Note: Only the professional version has the platform integration function, so you need to use ThingsBoard Cloud or download and deploy the professional version.

This article uses the fully managed MQTT message cloud service - EMQX Cloud to create a third-party broker. Register and log in to the EMQX Cloud console, create a new deployment, and a deployment is a Broker. New users have both a 14-day Basic Edition and a 14-day Professional Edition free trial.

EMQX Cloud provides functions such as VPC peering connection and REST API, and has powerful and flexible data integration capabilities to facilitate users to connect with their existing cloud service resources. Providing one-stop operation and maintenance management can save a lot of time and labor costs, allowing enterprises to focus on delivering more valuable business systems.

integrated

Using EMQX Cloud

  1. Get the connection address and port. Wait for the deployment status to be running, go to the deployment overview page, and find the connection address and the connection port corresponding to the mqtt protocol. We need to use them when adding integrations in ThingsBoard later.

    MQTT Cloud 部署信息

  2. Add authentication information. Go to [Authentication] -> [Authentication] and add a set of username and password for subsequent integration.

    MQTT Cloud 添加认证信息

Configure ThingsBoard

  1. Add a data converter of type Uplink in [Data converters]. The role of this upstream data converter is to parse the incoming message payload and convert it to the format used by ThingsBoard.

    1. Fill in the name, select Uplink for the type, enable Debug mode and copy and paste the following parsing script into the parsing method.

       // Decode an uplink message from a buffer
      // payload - array of bytes
      // metadata - key/value object
      
      // decode payload to json
      var payloadJson = decodeToJson(payload);
      var result = {
         deviceName: payloadJson.deviceName,
         attributes: {
             model: 'Model A',
             serialNumber: 'SN111',
             integrationName: metadata['integrationName']
         },
         telemetry: {
             temperature: payloadJson.temperature,
             humidity: payloadJson.humidity,
         }
      };
      
      // Helper functions
      function decodeToString(payload) {
         return String.fromCharCode.apply(String, payload);
      }
      function decodeToJson(payload) {
         // covert payload to string.
         var str = decodeToString(payload);
      
         // parse string to JSON
         var data = JSON.parse(str);
         return data;
      }
      
      return result;
    2. Click the test button to enter the test page and test the parsing script just now. Enter the payload content in JSON format for testing, and you can see that the test output data contains the input device name, temperature and humidity data. Then click the Save button to return to the configuration page just now.

      ThingsBoard Data converters

    3. Click the Add button and successfully add a data converter of type Uplink .

      ThingsBoard Uplink

  2. Go to [Integrations] to add EMQX Cloud deployment integration.

    1. Click Add Integration, enter a name and select the upstream data converter MQTT-Uplink that was successfully added in step 1 above. Then copy and paste the connection address on the EMQX Cloud deployment overview page and the port number corresponding to the mqtt protocol.

      ThingsBoard Integrations

    2. Add authentication information. Since authentication is enabled by default in EMQX Cloud deployments, we can choose the basic type of authentication, and then fill in the username and password added in the EMQX Cloud authentication page. Click Test Connection, and you can see the message prompt that the connection has been successfully established pops up in the lower right corner, indicating that it has been successfully deployed and integrated with EMQX Cloud. Finally, enter a filter topic /test/integration/emqxcloud (we need to use this topic to publish a message during subsequent simulation tests), and finally click the Add button to successfully add the integration with EMQX Cloud deployment.

      ThingsBoard 添加认证

Integration Testing

After completing the above integration configuration, we use the MQTT 5.0 client tool - MQTT X to simulate a device to test and verify the effectiveness of the function.

  1. Use MQTT X as a device to connect to the EMQX Cloud deployment.

    MQTT 连接

  2. After the connection is successfully established, simulate sending a piece of temperature and humidity data reported by the device to the filter topic configured during the above integration /test/integration/emqxcloud .

    MQTT 消息发布

  3. Enter all the menus under the device group in ThingsBoard, you can see that the device name and temperature and humidity data we just simulated have been displayed here. It indicates that the EMQX Cloud deployment has been successfully integrated in ThingsBoard. You can also see the relevant information of the simulation data just now in [Events] and [Relations] in the Integration with EMQX Cloud integration details page.

    ThingsBoard MQTT

Customize Dashboard to access MQTT data

  1. Add a new dashboard.

    MQTT Dashboard

  2. Open the dashboard and click the orange edit icon in the lower right corner, and then add an alias (define the data that will use the entity) as shown in the figure. Filter type to select a single entity, type to select device and select the Device Test device simulated by MQTT X above. After adding all the configuration information, you need to click the application icon in the lower right corner, otherwise the previously completed configuration will not be applied or saved.

    MQTT Device

  3. Adds a time series table widget.

    1. Still on the page just now, click the orange edit icon in the lower right corner to enter the edit mode, and then click Add Widget.

      ThingsBoard 新增小组件

    2. Enter Cards to search for Timeseries table and click to configure.

      ThingsBoard Timeseries table

    3. Configure the table just selected, select the alias set above for the instance alias, then add the key value of the table, and finally click the Add button.

      ThingsBoard Timeseries table1

    4. Drag to resize the newly added table, and click the orange tick icon to apply the button.

      ThingsBoard Timeseries table2

    5. We now go back to MQTT X, change the temperature value to 25 and the humidity to 80, publish a message again, and you can see that there is corresponding data in the table just configured.

      ThingsBoard Timeseries table3

  4. Similar to step 3 above, we click Add widget, search for input charts, then select Timeseries Line Chart, configure and change the real-time time range to the last 5 hours. Use MQTT X to send a piece of data again, and you can see that the corresponding data is displayed in both components.

    ThingsBoard Timeseries Line Chart

Summarize

So far, we have completed the integration of EMQX Cloud deployment in ThingsBoard Cloud, verified the integration function using MQTT X test, and finally customized a simple Dashboard access to display MQTT data. In actual projects, we can perform more complex Dashboard configuration after in-depth study and understanding of ThingsBoard, which can more vividly and concretely monitor the relevant data of the device in real time, set the alarm threshold, receive the alarm information and deal with it in time.

Copyright statement: This article is original by EMQ, please indicate the source when reprinting.

Original link: https://www.emqx.com/zh/blog/how-to-use-thingsboard-to-access-mqtt-data


EMQX
336 声望436 粉丝

EMQ(杭州映云科技有限公司)是一家开源物联网数据基础设施软件供应商,交付全球领先的开源 MQTT 消息服务器和流处理数据库,提供基于云原生+边缘计算技术的一站式解决方案,实现企业云边端实时数据连接、移动、...