Last Updated on : 2024-06-05 03:15:10download
The data point (DP) is the abstraction of a smart device function, describing product functions and parameters.
Identifier: the identification of a data point. An identifier is used for DP data transmission between the device and the cloud. Identifiers can contain letters, numbers, and underscores (_), and must start with a letter.
DP name: the name of a function.
Data type:
Type | Parameter name | Description | Example |
---|---|---|---|
Boolean | bool | Represent a binary variable value that is either true or false. | On/off control. Turn a device on or off. |
Value | value | Apply to the data that can be linearly adjusted. | Adjust the temperature, ranging from 20°C to 40°C. |
Enum | enum | A custom finite set of values. | Change the working level in terms of high, medium, and low. |
Fault | fault | Used to report DP failure. The device can only report the data when multiple faults occur. | Faults occur in the temperature sensor, motor, and more. |
String | string | Apply to DP data transmitted in the string format. | - |
Raw | raw | Transmit the data in the raw binary format. Raw data needs to be converted into data in the hexadecimal format. | - |
Data transfer type
Take a lighting product as an example. The following table lists respective DPs.
code | Name | Data transfer type | Type | Property |
---|---|---|---|---|
switch_led | Switch | rw | bool | {“type”:“bool”} |
work_mode | Mode | rw | enum | {“range”:[“white”, “colour”, “scene”, “music”, “scene_1”, “scene_2”, “scene_3”, “scene_4”]} |
bright_value | Brightness | rw | value | {“min”:25,“scale”:0, “unit”:“”, “max”:255, “step”:1} |
temp_value | Color temperature | rw | value | {“min”:0, “scale”:0, “unit”:“”, “max”:255,“step”:1} |
These DPs show that the light supports the adjustment of On/Off, brightness, and color temperature, and provides different modules, such as the white light mode and music mode.
You can query device DPs by calling cloud functions. If you have not deployed cloud functions, follow the instructions in Cloud Functions to deploy them.
The following example shows how to query device DPs:
import { request } from '../../utils/request';
const params = {
data: {
action: "device.specifications",
params: {
"device_id": "xxx" // The device ID.
}
}
};
request(params).then(res =>{
console.log('res', res);
}).catch(err => console.log('err', err))
The following example shows the response:
{
"category":"dj", // The category of the device.
"functions":[ // The array of DPs. Only one DP is displayed in this example.
{
"code":"switch_led", // The identifier of the DP. It is used to send data.
"type":"Boolean", // The data type.
"values":"{}" // The properties of the DP.
},
]
}
Based on the communication protocols supported by smart devices, WeChat mini programs support the following methods to send DP commands:
device.control
For devices that can be directly connected to the internet, you can call cloud functions to send DP commands. If you have not deployed cloud functions, follow the instructions in Cloud Functions to deploy them.
If the response indicates a permission denial, check whether this error also occurs on other interfaces. If this error only occurs in the response for querying device DPs, this shows that the device is not standardized. In this case, you can submit a ticket to request sending raw data.
The following example shows how to send a DP command:
const params = {
data: {
action: "device.control",
// params: the parameters.
params: {
"device_id": "xxx", // The device ID.
"commands": [{ "code": "switch_led", "value": true }] // The DP command.
}
}
};
request(params).then(res =>{
console.log('res', res); // A value of true indicates that the command is sent.
}).catch(err => console.log('err', err))
To send a DP command to control a Bluetooth device that cannot be directly connected to the internet, you must establish a Bluetooth connection between the device and the mobile phone. Then, call WeChat’s native Bluetooth APIs to send the DP command.
To help you quickly implement Bluetooth features for WeChat mini programs, Tuya provides the Bluetooth SDK. For more information, see WeChat Mini Program Bluetooth SDK.
WeChat mini programs support device status query in any of the following methods:
device.status
.device.status
const params = {
data: {
action: "device.status",
// params: the parameters.
params: {
"device_id": "xxx", // The device ID.
"commands": [{ "code": "switch_led", "value": true }] // The DP command.
}
}
};
request(params).then(res =>{
console.log('res', res);
}).catch(err => console.log('err', err))
Response:
"result": [{ // The returned array contains all DP statuses.
"code": "switch_led",
"value": true
}]
onShow
lifecycle method of the WeChat mini program.Create an MQTT connection in app.js
file of the WeChat mini program.
// Import the MQTT file.
import wxMqtt from './utils/mqtt/wxMqtt'
// Start the connection and listen for MQTT messages with the onLaunch lifecycle method.
onLaunch: function () {
wxMqtt.connectMqtt()
wxMqtt.on('close', (errorMsg) => {
wxMqtt.connectMqtt()
console.log('errorMsg: mqttClose', errorMsg);
})
wxMqtt.on('error', (errorMsg) => {
wxMqtt.connectMqtt()
console.log('errorMsg: mqttError', errorMsg);
})
}
Develop a listener for the target page.
wxMqtt.on('message', (topic, newVal) => {
console.log('message') // The listener data.
})
For Bluetooth devices that cannot be directly connected to the internet, to query the device status, perform the following steps:
To help you quickly implement Bluetooth features for WeChat mini programs, Tuya provides the Bluetooth SDK. For more information, see WeChat Mini Program Bluetooth SDK.
device.status
called to query the device status?When a page is initially opened or the device status has changes, call device.status
to query the device status. You can also call it as needed.
Please check whether the same response occurs in other API requests. If this response has occurred only in the query of device features, it indicates that the device has not been standardized. You can also submit a ticket to request sending raw data.
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback