This topic describes how to filter data that a device reports to the cloud. In this example, a LightSensor device is used.

Prerequisites

Background information

Sensors constantly report collected data. For example, a thermometer reports temperature values and a light sensor reports illuminance values. No significant difference exists in data that a device reports. Therefore, you need only to focus on the data that exceeds the limits. Link IoT Edge provides Function Compute-based edge applications to filter data that is reported to the cloud. This helps reduce costs of using Alibaba Cloud resources.

Before you use Function Compute-based edge applications to process data, you can view device data in the IoT Platform console. In the left-side navigation pane, choose Devices > Devices. Find the LightSensor device and click View in the Actions column. On the Device Details page, click the TSL Data tab and then the Status tab to view the status of the device. View device data

To view device data, click View Data. You can see that values regularly change from 100 to 600 with a difference ±100. Data is reported every two seconds. Reporting data with a high frequency for a long period may incur unnecessary costs. You can use Function Compute-based edge applications to filter data that the device reports to the cloud.

1. Create a data filtering function

  1. Click deviceDataFilter.zip to download the package of the data filtering function. You can use the function to filter illuminance values that the LightSensor device reports. For example, only the illuminance values that are greater than 500 or less than 200 can be reported to the cloud.
  2. Log on to the Function Compute console.
    If you have not activated Function Compute, read the terms, select I have read and agree, and then click Activate Now.
  3. Optional. In the left-side navigation pane, click Services and Functions. On the Services and Functions page, click Create Service. On the Create Service page, set the parameters as required and click Submit.
    The Service Name parameter is required. In this example, the Service Name parameter is set to EdgeFC. You can set other parameters based on your needs.
    Note If the EdgeFC service has been created for other scenarios or applications, you do not need to recreate the service.
  4. After you create the service, you must create a function in the service. On the Services and Functions page, click Create Function. On the Create Function page, click Configure and Deploy in the Event Function section.
  5. Set the parameters as required to create a data filtering function.
    Parameter Description
    Service Name The service where the function resides. Select EdgeFC.
    Function Name The name of the function. Enter lightSensorDataFilter.
    Runtime The runtime environment of the function. Select Node.JS 8.x.
    Function Handler The handler of the function. Use the default value index.handler.
    Memory The size of memory that is required to execute the function. Select 512MB.
    Timeout The timeout period of the function. Enter 10. Unit: seconds.
    Single Instance Concurrency The number of concurrent requests that can be processed by an instance. Use the default value.

    You can set other parameters based on your needs or leave them unspecified. For more information, see What is Function Compute?

    Confirm the function parameters and click Create.

  6. After the function is created, you are navigated to the details page of the function. On the Code tab, select Upload Zip File and click Select File to upload the deviceDataFilter.zip package that you downloaded in Step 1. Then, click Save.
    After the package is uploaded, you can select In-line Edit to view the source code.
    Note You can also click the function name on the Services and Functions page to go to the function details page, and click the Code tab to view the source code.
    In-line Edit option
    Note The lightSensorDataFilter sample code includes the following three operations:
    1. Parse illuminance values from the data that the device reports. The event parameter is used to send data.
      var illuminance = iotData.getThingPropertyByEvent(event, "MeasuredIlluminance");
    2. Check whether the data meets the filter conditions.
      
      if (illuminance > 500 || illuminance < 200)
    3. Report the data that meets the filter conditions.
      iotData.publish(message, (err, data)=> {callback(err);});
  7. Optional. Modify the filter conditions of data.
    After you create the function, you can edit the code to modify the filter conditions of data.
    The following sample code provides the default filter conditions of data. After you assign the function to the edge instance and deploy the instance, only illuminance values that are greater than 500 or less than 200 are reported. To view reported data, you can go to the Device Details page of the device, click the TSL Data tab, and then click the Status tab.
    if (illuminance > 500 || illuminance < 200)
    You can modify the filter conditions of data. For example, if you specify the following condition, only illuminance values that are greater than 450 are reported after you assign the function to the edge instance and deploy the instance. To view reported data, you can go to the Device Details page of the device, click the TSL Data tab, and then click the Status tab.
    if (illuminance > 450)

2. Assign the function to the edge instance

  1. Log on to the Link IoT Edge console.
  2. In the left-side navigation pane, click Applications.
  3. Create a Function Compute-based edge application by using the function that is created in 1. Create a data filtering function. For more information, see Use Function Compute to create edge applications.

    The following table describes the application parameters.

    Parameter Description
    Application Name The name of the application. Example: lightSensorDataFilter.
    Application Type The method that is used to create the application. Select Function Compute.
    Region The region where the service resides.
    Service The service of the function that is used to create the application. Select EdgeFC.
    Function The function that is used to create the application. Select lightSensorDataFilter.
    Authorization The RAM role that Link IoT Edge assumes to connect to Function Compute. Select AliyunIOTAccessingFCRole.
    Application Version The version number of the application. You cannot specify two identical version numbers for an application.
    The following table describes the function parameters.
    Parameter Description
    Running Mode The execution mode of the function. Valid values: On-demand and Continuous. In this example, select Continuous, which indicates that the function is executed immediately after it is deployed.
    Memory Limit (MB) The maximum size of memory that is available for executing the function. Unit: MB. Enter 512. If the size of memory that is used by the function exceeds the limit, the edge application is forced to restart.
    Timeout(Seconds) The maximum processing period after the function receives an event. In this example, use the default value 5. If the function does not return the result within the specified period, the edge application is forced to restart.
    Scheduled Execution Specifies whether to enable scheduled execution. By default, scheduled execution is disabled. In this example, use the default setting.

    You need only to set the parameters that are described in the preceding tables.

  4. In the left-side navigation pane, click Edge Instances.
  5. Find the created edge instance and click View in the Actions column.
  6. On the Instance Details page, click the Edge Applications tab. On the Edge Applications tab, click Assign Application.
  7. Assign the lightSensorDataFilter function to the edge instance, and click Close.

3. Configure message routing

For more information about how to add a message route and set parameters, see Configure message routing.

  1. On the Instance Details page, click the Message Routing tab. On the Message Routing tab, remove the default message route from the device to IoT Hub.
    Note You must remove the default message route that is configured when you created and assigned the LightSensor device to the edge instance. For information about how to create and assign devices to edge instances, see Sample drivers.
  2. Click Assign Route and add a message route from the LightSenor device to Function Compute.
    Set the parameters that are described in the following table and click OK to add the first message route.
    Parameter Description
    Route Name The name of the message route.
    Message Source The source of messages. Select Device. Then, select LightSensor and LightSensor in sequence.
    Message Topic Filter The filter condition of messages. Select All.
    Message Destination The destination of messages. Select Edge Applications. Then, select EdgeFC/lightSensorDataFilter.
  3. On the Instance Details page, click the Devices & Drivers tab. Find the LightSensor device and click View in the Actions column to go to the Device Details page.
  4. Click the Topic List tab and then the Topics for TSL Communications tab. Then, copy the first topic that is used to report device properties.
    Topics for TSL Communications tab
  5. Go back to the Instance Details page, click Assign Route on the Message Routing tab, and then add a message route from Function Compute to IoT Hub.
    Set the parameters that are described in the following table and click OK to add the second message route.
    Parameter Description
    Route Name The name of the message route.
    Message Source The source of messages. Select Edge Applications. Then, select EdgeFC/lightSensorDataFilter.
    Message Topic Filter The filter condition of messages. Enter the topic that you copied in the previous step.
    Message Destination The destination of messages. Select IoT Hub.
    Service Level The level of the service. Select 0.

4. Deploy the edge instance

  1. On the Instance Details page, click Deploy in the upper right corner. In the message that appears, click OK to assign resources such as sub-devices and Function Compute-based edge applications to the edge instance.
  2. On the Instance Details page, click the Devices & Drivers tab. On the Devices & Drivers tab, find the LightSensor device and click View in the Actions column. Then, you are navigated to the Device Details page of the IoT Platform console.
    On the Device Details page, click the TSL Data tab and then the Status tab to view the status and data of the LightSensor device. View device data

    This is the whole procedure of using a Function Compute-based edge application to filter data that a device reports to the cloud.