Build a device monitoring dashboard

Updated at:

This topic describes how to integrate IoT Platform with the Alibaba Cloud big data platform to implement real-time analysis, statistics collection, computation, and visualization of device data.

Prerequisites

Background information

  • Goal: Display real-time data from the devices of a specific product on a DataV dashboard.

  • Workflow:

    1. IoT Platform collects data from devices.

    2. The rules engine in IoT Platform forwards device data of a specific product to the streaming data processing service DataHub.

    3. Based on your configuration, DataHub sends device data to Realtime Compute for Apache Flink for processing. Realtime Compute then writes the processed data to an ApsaraDB RDS for MySQL database. If your data does not require processing, you can use DataConnector to sync data directly from DataHub to the ApsaraDB RDS for MySQL database.

    4. DataV uses the MySQL database table as a data source to display device data in real time.

数据交互

Procedure

  1. Create an ApsaraDB RDS for MySQL database to store device data.

    To learn more about ApsaraDB RDS, see the ApsaraDB RDS documentation.

    1. Log on to the ApsaraDB RDS console.

    2. On the Instances page, click Create Instance to create a MySQL database instance.

      Note

      The ApsaraDB RDS for MySQL instance must be in the same region as your IoT Platform devices and DataHub project.

    3. In your list of database instances, click Manage next to the target instance.

    4. In the left-side navigation pane, click Accounts to create a database account.

    5. In the left-side navigation pane, click Databases to create a database.

    6. In the left-side navigation pane, click Data Security and add a database whitelist. For more information, see Set an IP whitelist.

    7. In the left-side navigation pane, click Basic Information to view the information about the database.

      You will need this information in later steps to configure data synchronization in DataHub, DataV, and Realtime Compute for Apache Flink.

    8. At the top of the Basic Information page, click Log On to Database and enter your credentials to log on to the database.

    9. Create a database table. For example, create a table named mytable that contains two fields:

      Table 1. mytable

      Parameter

      Type

      Description

      d_data

      varchar(32)

      Timestamp of the data point.

      device_num

      int

      The number of active devices.

  2. Create a DataHub project and a topic.

    To learn more about the DataHub streaming data processing platform, see the DataHub documentation.

    1. Log on to the DataHub console.

    2. On the Project Management page, click New Project to create a project.

    3. In the project list, click View for the newly created project.

    4. On the project information page, click New Topic to create a topic.

      A topic is a storage unit in DataHub, similar to a table in a database.

    5. After the topic is created, find it in the topic list and click View to see the topic details.

    6. (Optional) Configure a DataConnector to synchronize data from the topic to a database table. On the topic information page, click the + Sync button in the upper-right corner and select RDS & Mysql. Then, in the New Connector panel, enter your MySQL database information and select the connection mode and network type.

      Note

      If you need to process data before writing it to the database, use Realtime Compute for Apache Flink as described in Step 3.

      You can find the database information on the Basic Information page for the database in the ApsaraDB RDS console.

      You also need to configure the Write Mode (for example, select IGNORE), Import Fields (for example, d_data, temp, and humidity), Start Time, and Timestamp Unit (for example, select MICROSECOND). After the configuration is complete, click Create.

      The following table describes the database parameters.

      Parameter

      Description

      Host

      The internal endpoint of the database.

      Port

      The internal port of the database, which is typically 3306.

      Database

      The name of the database.

      Table

      The name of the database table.

      User

      The database username.

      Password

      The logon password for the database.

  3. (Optional) Use Realtime Compute for Apache Flink to process streaming data.

    For example, you can calculate a metric, such as the cumulative number of unique devices that have sent or received messages, in real time and write it to the database.

    To learn more about the development platform, see What is Realtime Compute for Apache Flink?.

    1. Log on to the Realtime Compute for Apache Flink console.

    2. Click New Project to create a project.

      Note

      You must purchase Realtime Compute resources before you can create a project.

    3. After the project is created, click the project name to go to the project details page. Then, choose Develop > New Job to create a job.

    4. Choose Data Storage > DataHub Data Storage > + Register & Network. Enter your DataHub endpoint address (for endpoint addresses in different regions, see DataHub endpoints), set Project to your project name in DataHub, and then click Register.

      Through the UI, you can only register storage resources belonging to the current account. To use resources from other accounts, you must declare and reference them directly in your code.

    5. Choose Data Storage > RDS Data Storage > + Register & Network, enter your database information, and then click Register.

    6. Under Data Storage, double-click DataHub Data Storage to display the registered DataHub project name. Double-click the project name, then double-click the topic name, and click Reference as Input Table in the middle of the page. Realtime Compute automatically parses the topic's schema and generates the corresponding DDL statement.

    7. Under Data Storage, find and double-click the database table name, then click Reference as Result Table. Realtime Compute automatically parses the database table and generates the corresponding DDL statement.

    8. In the job editor, write the SQL for your business logic. The following is an example SQL statement for calculating the number of active devices:

      
      REPLACE INTO activity_device 
      SELECT 
      from_unixtime(FLOOR(dm.msgtime/1000), 'yyyy-MM-dd') as d_data,
      count(DISTINCT dm.devicename) as device_num
      FROM device_message dm
      group by from_unixtime(FLOOR(dm.msgtime/1000), 'yyyy-MM-dd');
    9. Click Debug and upload a data file to debug the SQL. If the result matches your expectations, the SQL is correct.

    10. After developing and debugging the job, click Go Live to begin the deployment process.

      Note

      The "Go Live" action only submits your job to the operations and maintenance (O&M) system. It does not start the job. To start the job, you must do so from the O&M interface.

  4. In the IoT Platform console, create a product and a device, and configure the rules engine.

    1. Log on to the IoT Platform console.

    2. On the Overview page, click All environment. On the All environment tab, find the instance that you want to manage and click the instance ID or instance name.

    3. In the left-side navigation pane, choose Devices > Products, and then create a product. For more information, see Create a product.

    4. After the product is created, go to the Product Details page. Based on your business needs, create custom topic categories or define product features (a TSL model) for the product.

    5. In the left-side navigation pane, choose Devices > Devices, and then register a device.

    6. In the left-side navigation pane, choose Message Forwarding > Data Forwarding to create a rule.

      Important

      If the new version of the data forwarding page is displayed, click Back to Old Version in the upper-right corner. Then, find the destination rule and click View.

    7. In the list of rules, click View for the rule.

    8. On the Data Forwarding Rule page, click Write SQL in the Data Processing section to write and debug a data processing SQL statement for the rule.

      In the Write SQL dialog box, in the Topic configuration area, select TSL Model Data Submitted, Home Thermostat, Device1, and Default Module in sequence. Set the SELECT fields of the SQL statement to items.temperature.value as temperature, items.humidity.value as humidity, deviceName() as deviceName, and timestamp() as time, and leave the WHERE condition empty. After you complete the configuration, click OK.

    9. Click Add Operation in the Data Forwarding section and configure a rule action to forward device data to the DataHub topic. For more information, see Set a data forwarding rule.

    10. After the rule is configured, go to the Data Forwarding page. In the list of rules, click Start for the rule to enable it.

    After the rule is started, use a simulated device to send messages and verify that the messages are successfully forwarded to DataHub. You can view the device logs on the Device Log page of the device. In the DataHub console, you can monitor the change in data volume in the shards of the corresponding topic and use the data sampling feature to view the specific message content.

  5. Develop the device-side client and connect the device to IoT Platform.

    This example uses the Java Link SDK for device development: Download Java SDK Demo. For development instructions, see Environment requirements and configuration.

    After developing the device-side application, flash it onto the physical device. When the device is powered on and connected to the network, it establishes a connection with IoT Platform and can exchange data. The data is forwarded to DataHub through the rules engine, processed by Realtime Compute for Apache Flink, and then written to the database.

  6. In the DataV console, configure DataV.

    1. Log on to the DataV console.

    2. Configure a DataV data source. Choose My Data > Add Data. Enter your ApsaraDB RDS for MySQL database information and click OK.

    3. Choose My Visualization > New Visualization.

    4. Select a data visualization template on the left side of the page, move your mouse to the Select Template area in the middle of the page, and click OK.

      DataV provides several dashboard templates. You can also choose a blank template and customize the display components and styles to fit your needs.

    5. Configure the display components based on your requirements.

      Introduction to the DataV configuration page:

      • The row of icons at the top allows you to select and add components.

      • The central area is the canvas, where you can see your dashboard layout. Click any component to configure it.

      • The left side lists your selected components. Click a component to configure it on the right. Right-click a component to adjust its position, rename it, or delete it.

      • Configure component information on the right side.

      For configuration instructions, see the DataV documentation.

    6. Configure component data.

      In the component configuration panel on the right, click the {/} data configuration button. Set Data Source Type to Database, select your database from the list, and then write your SQL query.

      In the data configuration panel, map the fields: Map x to data, y to count, and s to the series field.

  7. To test your dashboard, have multiple devices send messages. The number of active devices on the dashboard will update in real time.