Filter device data uploaded to the cloud

Updated at:

This topic uses the LightSensor device from the official sample driver to demonstrate how to filter data that devices report to the cloud.

Prerequisites

Background information

Devices such as sensors continuously report collected data. For example, a thermometer reports temperature values, and a light sensor reports illuminance values. This data often varies only slightly. Typically, you only need data points that exceed a specific threshold. You can use the Function Compute feature of IoT Edge to filter device data before it is sent to the cloud. This practice reduces the amount of uploaded data and lowers your cloud computing costs.

Before you use Function Compute to process data, you can view the device status in the IoT Platform console. In the navigation pane on the left, choose Device Management > Devices. Find the LightSensor device and click View next to the device name. On the Device Details page, go to the TSL Data > Status tab to view the device status.查看设备数据

In the preceding figure, click View Data. The value continuously changes between 100 and 600 in increments of 100. Data is reported at a high frequency, once every 2 seconds. Over time, this high frequency of data reporting can lead to unnecessary costs. To avoid these costs, you can use Function Compute to process and filter the data that the device reports to the cloud.

Step 1: Create a data filtering function

  1. Download the data filtering function: deviceDataFilter.zip. This function filters the illuminance data reported by the LightSensor device. The function reports data to the cloud only when the illuminance value is greater than 500 or less than 200.
  2. Log on to the Function Compute console.
    If you have not activated this service, read the terms and select I have read and agree to the terms. Then, click Activate Now.
  3. (Optional) In the navigation pane on the left, click Services & Functions. On the Services & Functions page, in the Service List section, click Create Service to create a service.
    The Service Name parameter is required. Set this parameter to EdgeFC. You can configure the other parameters as needed or use their default settings.
    Note
    • If this is your first time creating a service in Function Compute, follow the configuration wizard.
    • If you have already created the EdgeFC service for other scenarios or miniprogram examples, you do not need to create it again.
  4. After the service is created, on the Services & Functions page, find the EdgeFC service and click Create Function.
  5. On the Create Function page, click Configure and Deploy in the Event Function section.
  6. Set the basic configuration parameters for the data filtering function.
    ParameterDescription
    Function TypeKeep the default option.
    ServiceSelect the EdgeFC service that you created.
    Function NameSet to lightSensorDataFilter.
    RuntimeSet the function runtime and choose how to upload the code. For this example, select Node.js 8.

    To the right of Upload Code, select Upload a ZIP file. Click Upload Code and upload the deviceDataFilter.zip package that you downloaded in Step 1.

    HandlerUse the default value: index.handler.

    You can configure the other parameters as needed or use their default settings. For more information, see Function Compute.

    After you confirm the function information, click Create.

  7. After the function is created, you are redirected to the function details page. On the Code tab, in the Code Execution Management section, select Online Editing to view the source code.
    在线编辑
    Note The lightSensorDataFilter sample code consists of the following three steps.
    1. Parse the illuminance value from the received device data (the event parameter).
      var illuminance = iotData.getThingPropertyByEvent(event, "MeasuredIlluminance");
    2. Check whether the data meets the filtering conditions for cloud upload.
      
      if (illuminance > 500 || illuminance < 200)
    3. Report the data that meets the conditions.
      iotData.publish(message, (err, data)=> {callback(err);});
  8. (Optional) Adjust the data filtering parameters.
    After the function is created, you can edit the code online to change the data filtering conditions.
    The default data filtering condition in the sample code is as follows. After you assign the function to an edge instance and deploy the instance, the function filters the collected illuminance data. Only data where the illuminance is greater than 500 or less than 200 is displayed on the TSL Data > Status tab of the Device Details
    if (illuminance > 500 || illuminance < 200)
    You can change the filtering condition. For example, to filter for data where the illuminance is greater than 450, change the data filtering parameter to the following:
    if (illuminance > 450)

Step 2: Assign the function to an edge instance

  1. Log on to the IoT Edge console.
  2. In the navigation pane on the left, click Application Management.
  3. Create an edge application of the Function Compute type using the function that you created in Step 1. For more information, see Function Compute applications.

    The application parameters are described as follows:

    ParameterDescription
    Application NameEnter a name for your application, such as lightSensorDataFilter.
    Application TypeSelect Function Compute.
    RegionSelect the region where your service was created.
    ServiceSelect the EdgeFC service.
    FunctionSelect the lightSensorDataFilter function.
    AuthorizationSelect AliyunIOTAccessingFCRole.
    Application VersionSet the application version. The version number must be unique for this application. You cannot have two identical version numbers for the same application.

    In the Function Configuration section, set Enable Default Configurations to Yes. The other parameters can be left at their default settings.

  4. In the navigation pane on the left, click Edge Instances.
  5. Find the edge instance that you created in the "Prerequisites" section and click View.
  6. On the Instance Details page, go to the Edge Applications tab and click Assign Application.
  7. In the Assign Application panel, find the lightSensorDataFilter function you created. Click Assign in the Actions column, and then click Close.

Step 3: Configure message routing

This topic explains how to add messages and describes each parameter. For more information, see Set message routing.

  1. On the Instance Details page, go to the Message Routing tab. Remove the message route from the device to IoT Hub.
    Note When you followed the instructions in the Sample driver prerequisite, you configured a message route for the edge instance. You must remove this route in this step.
    移除消息路由
  2. Click Assign Route to add a message route from the LightSensor device to Function Compute.
    Set the following parameters. After you finish, click OK to add the first route.
    ParameterDescription
    Route NameEnter a name for the message route.
    Message SourceSelect Device, and then select Light Sensor > LightSensor.
    Topic FilterSelect All.
    Message TargetSelect Edge Application and the EdgeFC/lightSensorDataFilter function.
  3. On the Instance Details page, go to the Devices & Drivers tab. Click View next to the LightSensor device. You are redirected to the Device Details page.
  4. Go to the Topic List > TSL Communication Topic tab. Copy the first topic for reporting device properties.
    获取设备topic列表
  5. Return to the Instance Details page. Click Assign Route to add a message route from Function Compute to IoT Hub (the cloud).
    Set the following parameters. After you finish, click OK to add the second route.
    ParameterDescription
    Route NameEnter a name for the message route.
    Message SourceSelect Function Compute and the EdgeFC/lightSensorDataFilter function.
    Topic FilterEnter the topic for reporting device properties that you copied in the previous step.
    Message TargetSelect IoT Hub.
    QoS LevelSelect 0.

Step 4: Deploy the edge instance

  1. On the Instance Details page, click Deploy in the upper-right corner. In the dialog box that appears, click OK to deploy resources, such as sub-devices and functions, to the edge.
  2. On the Instance Details page, go to the Devices & Drivers tab. Then, click View next to the LightSensor device. You are redirected to the Device Details page in the IoT Platform console
    On the Device Details page, go to the TSL Data > Status tab to view the status and data of the LightSensor device.查看设备数据

    You have now successfully used an edge function to filter device data before it is uploaded to the cloud.