Smart air purifier control

更新时间:
复制 MD 格式

In this tutorial, you will create a business service to automatically control an air purifier. This service turns the air purifier off when the reported PM2.5 concentration is less than 100 and turns it on when the concentration is 100 or greater.

Prerequisites

You have created products and devices for air detection (Air Detector: airDetector) and purification (Air Purifier: airPurifier) in the Project_test project. For details, see Create products for air detection and purification in a project.

Create an air purifier control service

  1. Create a business service in your project, for example, Air Purification. For details, see Create a business service.
  2. On the business logic development page, click the Node button on the left. Drag a Device trigger node onto the canvas. Configure the node to use the PM2.5 Concentration property from the airDetector device as input.
  3. Drag a Conditional node onto the canvas, connect it to the device trigger node, and configure it.
    Set Condition 1: The PM2.5 value from the device trigger is greater than or equal to 100.
  4. Drag two Air Purifier product nodes onto the canvas. Connect them to the success and failure branches of the Conditional node, respectively, and configure them.
    ParameterDescription
    Node nameEnter a name for the node.
    Select device to controlSelect the airPurifier device.
    Select operation typeSelect the operation type. Options: Execute Device Action, Query Device Property, and Query Other Device Information.

    To send a command to control the power switch, select Execute Device Action.

    PayloadSet the PowerSwitch property in the payload. For the node on the success branch (condition met), set the value to true. This turns on the air purifier when the PM2.5 value is 100 or greater. For the node on the failure branch, set the value to false to turn off the air purifier.
  5. In the upper-right corner of the page, click Save保存 to save the service.

Debug and publish

  1. On the business service editing page, click Deploy & Debug部署 in the upper-right corner to deploy and start the service.
  2. After the service is deployed successfully, click Deploy & Debug部署 in the upper-right corner to debug the service.
    1. Click Debug > Go to to open the product's device simulator page.
    2. Click Start virtual device.
    3. Select Uplink command debugging > Property report.
    4. In the default module, send a debug command.

      In this example, a simulated PM2.5 value of 101 is sent. Because this value is greater than 100, the air purifier receives a command to turn on.

      In the device simulator's uplink command debugging panel, select the Property report tab. Enter a value for the PM25 field and click Send Command. A PROPERTY_REPORT record will appear in the device logs panel on the right, confirming a successful property report.
  3. On the project's Devices page, check the running status of the air purifier. The PowerSwitch status is 1 (On).

    You can return to the service development page to view the related logs.

    The workflow for the Control Air Purifier automation rule consists of Device trigger, Conditional, and Air Purifier nodes. In the log viewing area, you can check the Global Log to see the execution details for each node. For example, the log for the device trigger node shows that the reported PM25 property value was 101.
  4. On the service development page, click the Publish button发布 in the upper-right corner.
    You must publish the business service to the cloud before it can be invoked.