Control a humidifier with weather data
This tutorial shows you how to create a service that turns a humidifier on or off based on humidity data from a weather forecast. The service turns on the humidifier if the humidity is less than or equal to 30%, and turns it off otherwise.
Prerequisites
You have purchased a weather query API from the Alibaba Cloud Marketplace.
You have created a project. For more information, see Create a project.
Step 1: Create a product and a device
Click a project card to open the project Homepage. In the left-side navigation pane, click Products.
On the Products page, click Create Product in the upper-left corner.
On the Create Product page, set the parameters and click Confirm.
NoteIf you already created the product and device in the IoT Platform console, click Associate IoT Platform Product to link the product to this project.
Set the following parameters: Set Product Name to
Smart Humidifier, select Custom Category for Category, select Directly-connected device for Node Type, select Wi-Fi for Networking Method, and select ICA standard data format (Alink JSON) for Data Format.After the product is created, return to the Products page and click View for your product in the list.
On the Product Details page, click the Feature Definition tab, click Edit Draft, then click Add Custom Feature to add a custom boolean switch property. Click Confirm.
In the dialog box, set Feature Name to Power Switch, Identifier to
PowerSwitch, and Data Type to bool (Boolean). A value of 0 indicates Off and 1 indicates On. Set Read/Write Type to Read/Write.After adding the thing model, click Publish in the lower-left corner. This opens the Publish TSL Model dialog box.
Select the I have compared the current version with the online version. check box.
Click OK to publish the thing model.
On the project details page, click Devices in the left-side navigation pane.
Click Add Device to add the smart humidifier device.
In the dialog box, set DeviceName and Nickname. If you leave DeviceName empty, Alibaba Cloud assigns a unique identifier as the DeviceName. Click Confirm to finish adding the device.
Step 2: Create a business service
On the project Homepage, select the Business Logic tab. Click New > New to create a business service.
On the business logic development page, click the Nodes tab on the left. Drag an HTTP Request node onto the canvas and configure it as follows.
Parameter
Description
Node name
Enter a name for the node.
Action
The Action parameter for the API call. In this example, set the value to QueryWeather.
Add parameter
The input parameters for the API call. In this example, add a required input parameter cityId of the String type.
Drag a Cloud Marketplace API node onto the canvas, connect it to the HTTP Request node, then configure it.
In this example, the weather query API purchased from the Cloud Marketplace is Moji Weather - Weather Service (cityId) - Free Edition.
Select UTF-8 for Encoding. In the Set Request Header field, enter
{"Content-Type": "application/json"}.Parameter
Description
Node name
Enter a name for the node.
Request method
Check the request method on the API's purchase page in the Cloud Marketplace. In this example, the request method of the API is POST.
Request URL
On the API's purchase page in the Cloud Marketplace, find the request URL of the API.
AppCode
In the Cloud Marketplace console, find the AppCode of your purchased API on the Purchased Services page.
Encoding
Select an encoding method.
Set request header
Set the request header as required by the API.
Parameters
Enter the request parameters as described on the API's purchase page in the Cloud Marketplace. In this example, two parameters are required:
cityId: the city ID. You can enter one of the following:
Static parameter: A fixed value, such as
"cityId":"20". For a list of values, see the "cityid mapping table" on the Moji Weather purchase page.Dynamic parameter: A parameter that takes its value from the API request, such as
"cityId":"{{query.cityId}}". This structure passes an API input parameter as a sub-object.
token: In the Alibaba Cloud API Gateway console, choose , select China (Beijing) as the region, and view the token in the details of the purchased API. For detailed instructions, see "How to find the token" on the Moji Weather purchase page.
Add a temporary HTTP Response node to debug and inspect the weather API's response data structure.
To find the humidity value in the API's response, add an HTTP Response node, then deploy and debug the service. You can then inspect the response data structure in the debug logs.
Configure an HTTP Response node and set its Output to Previous Node (payload).
Click the Deploy and Debug button
to deploy the service.After the service is deployed, click the Deploy and Debug button
again. Enter a cityId value, such as 20, to start debugging.You can view the returned data in the API Call Results section or the Node Logs section of the HTTP Response node.
In the workflow, the HTTP Request, Cloud Marketplace API, and HTTP Response nodes show successful executions with green check marks. The API Call Results section displays the returned data in a JSON tree structure. In this example, the value of
codeis0, and thedataobject contains weather information such ascondition(Sunny),temp(11),windDir(Westerly), andhumidity(11).The response data structure shows that humidity is nested under data and condition. Therefore, in the path configuration, you must use
data.condition.humidityto access the humidity value from the weather API's response.After identifying the data path, delete the HTTP Response node.
Configure a Switch node and connect it to the Cloud Marketplace API node.
Parameter
Description
Node name
Enter a name for the node.
Data source
Select the Cloud Marketplace API node as the data source.
For the input variable, enter the path to the humidity value that you found during debugging: data.condition.humidity.
Add path
This example requires only two paths, so delete the third path.
Configure the paths:
Path 1: ≤ 30.
Path 2: > 30.
From the Devices tab, drag a Smart Humidifier device node onto the canvas and connect it to Path 1. Configure the node as follows:
Parameter
Description
Node name
Enter a name for the node.
Select a device to control
Select a device of this product.
Select an operation type
Select Device Action Execution.
Data to send
Select Property and set the Power Switch to true to turn on the humidifier.
Configure another Smart Humidifier node and connect it to Path 2.
Configure this node for the same device, but set the Power Switch to false to turn off the humidifier.
For the operation type, select Device Action Execution. For the data to send, select Property. For the Power Switch value source, select Fixed Value. For the data type, select Boolean.
Configure an HTTP Response node for each of the two device nodes. Set the output for both to Previous Node (payload).
In the upper-right corner of the page, click the Save button
to save the settings.
Step 3: Debug and publish
On the business service editing page, click the Deploy and Debug button
in the upper-right corner to deploy the service.After the deployment is complete, click the Deploy and Debug button
again, enter a cityId value, then click Start Debugging.The Global Logs show that the current humidity is 11. Therefore, the service executed Path 1 and turned on the humidifier.
Next to Deploy and Debug, click the Publish button
to publish the service.You must publish the service to the cloud before it can be called.
Results
You can use the API created by this service for web-based visualization development.
To view details on how to call the service, right-click the service name and select Service Invocation Help.
to publish the service.