H5 Application or WeChat Applet SDK User Guide

Last Updated on : 2025-05-30 01:56:03download

Procedure

  • Import a module.
import { OpenSaasSDK } from ‘xxx/openSaasSdk’
  • Initialize developer information, including the clientId, secret, access token, and refresh token of Tuya Cloud.
OpenSaasSDK.set({client_id, secret, access_token, refresh_token})
  • Invoke the request method. An example is as follows:
OpenSaasSDK.request({ data: { action: "***.***", vsersion: "1.0", params: {"***":{}} } });

Method List

action version description
mqtt.getMqttConfig 1.0 Obtain MQTT connection configuration.
device.listDevices 1.0 Obtain the device list.
device.getDeviceStatus 1.0 Obtain the latest device status.
device.batchQuerySwitchStatus 1.0 Query device switch status in batches.
device.getDeviceFunctions 1.0 Obtain the function set supported by a device.
device.getDeviceSpecifications 1.0 Obtain device specifications.
device.controlDevice 1.0 Issue commands to a device.
device.controlDeviceSwitch 1.0 Control a device switch.
device.controlInfraredDevice 1.0 Control an infrared device.
device.getInfraredStatus 1.0 Obtain the latest status of an infrared device.
scene.listScenes 1.0 Obtain the scene list.
scene.triggerScene 1.0 Trigger a scene.

Method for Obtaining MQTT Connection Configuration

Method Description

You can use this method to obtain MQTT connection configuration.

Method Parameters

action version
mqtt.getMqttConfig 1.0

Request Parameters

Parameter Type Description
connect_id String Unique ID of a device. (Optional. We recommend that you set this parameter.)

Response Parameters

Parameter Type Description
code Integer Response code.Null indicates success.
success Boolean Request result.
true: success
false: failure
msg String Exception message.It is null when the request result is success.
result Object MQTT connection configuration.

Parameters in result

Parameter Type Description
client_id String ID of the client connected to MQTT. The value a unique and constant mapping generated based on the user account and unique_id. One client ID can be used for both publishing and subscription.
expire_time Integer Expiration time of the current configuration.When the current configuration is invalid, all connections are broken.
username String User name in an MQTT connection. The value is a unique and constant mapping generated based on the user account.
password String Password in an MQTT connection.This parameter remains unchanged before the expiration time.
sink_topic Map MQTT topic corresponding to device control messages delivered by a user. The format is cloud/token/out/{dev_id}. In the format, {dev_id} indicates the ID of the controlled IoT device.
source_topic Map MQTT topic corresponding to messages pushed from a device to the account. Multiple client IDs can be used to obtain connections.
url String MQTT connection URL, which contains the protocol, IP address, and port number.

Request Example

OpenSaasSDK.request({ data: { action: "mqtt.getMqttConfig", vsersion: "1.0", params: { "connect_id": "******" }, } });

Success Response Example

{ "result": { "client_id": "******", "expire_time": 7200, "username": "******", "password": "******", "sink_topic":{ "device":"cloud/token/out/{device_id}" }, "source_topic":{ "device":"cloud/token/in/22e37df6e53bc8******" }, "url": "wss://m1.tuyacn.com:443/mqtt" }, "success": true }

Failure Response Example

{ "code":500, "msg":"system error,please contact the admin", "success":false }

Method for Obtaining the Device List

Method Description

You can use this method to obtain the device list.

Method Parameters

action version
device.listDevices 1.0

Response Parameters

Parameter Type Description
code Integer Response code.Null indicates success.
success Boolean Request result.
true: success
false: failure
msg String Exception message.It is null when the request result is success.
result Array Device list.

Parameters in result

Parameter Type Description
device_id String Unique ID of a device.
product_id String Unique ID of a product.
device_name String Device name.
category_name String Category name.
category_code String Category code.
sub Boolean Sub device.
online Boolean Indicates whether a deivce is online.
position String Location of a device.
dp_count Integer Number of data points.

Request Example

OpenSaasSDK.request({ data: { action: "device.listDevices", vsersion: "1.0" } });

Success Response Example

{ "result": [ { "category_code": "ckqdkg", "category_name": "", "device_id": "6c423606c35******", "device_name": "Zigbee low-frequency card power switch", "dp_count": 0, "online": true, "position": "Living room", "product_id": "iqsefpek", "sub": false } ], "success": true }

Failure Response Example

{ "code":500, "msg":"system error,please contact the admin", "success":false }

Method for Obtaining the Latest Device Status

Method Description

You can use this method to obtain the latest device status.

Method Parameters

action version
device.getDeviceStatus 1.0

Request Parameters

Parameter Type Description
device_id String Unique ID of a device.

Response Parameters

Parameter Type Description
code Integer Response code.Null indicates success.
success Boolean Request result.
true: success
false: failure
msg String Exception message.It is null when the request result is success.
result Array Device status list.

Parameters in result

Parameter Type Description
code String Status code.
value String Status value.

Request Example

OpenSaasSDK.request({ data: { action: "device.getDeviceStatus", vsersion: "1.0", params: { "device_id": "6c760b4d9668******_switch_2" } } });

Success Response Example

{ "result": [ { "code": "switch", "value": "true" } ], "success": true }

Failure Response Example

{ "code":500, "msg":"system error,please contact the admin", "success":false }

Method for Querying Device Switch Status in Batches

Method Description

You can use this method to query device switch status in batches.

Method Parameters

action version
device.batchQuerySwitchStatus 1.0

Request Parameters

Parameter Type Description
device_ids List List of unique IDs of devices.

Response Parameters

Parameter Type Description
code Integer Response code.Null indicates success.
success Boolean Request result.
true: success
false: failure
msg String Exception message.It is null when the request result is success.
result Array List of device switch statuses.

Parameters in result

Parameter Type Description
device_id String Unique ID of a device.
status Boolean Switch status.

Request Example

OpenSaasSDK.request({ data: { action: "device.batchQuerySwitchStatus", vsersion: "1.0", params: { "device_id": "6c760b4d9668******_switch_2" } } });

Success Response Example

{ "result": [ { "device_id": "6c760b4d9668******_switch_2", "status": true } ], "success": true }

Failure Response Example

{ "code":500, "msg":"system error,please contact the admin", "success":false }

Method for Obtaining the Function Set Supported by a Device

Method Description

You can use this method to obtain the function set supported by a device.

Method Parameters

action version
device.getDeviceFunctions 1.0

Request Parameters

Parameter Type Description
device_id String Unique ID of a device.

Response Parameters

Parameter Type Description
code Integer Response code.Null indicates success.
success Boolean Request result.
true: success
false: failure
msg String Exception message.It is null when the request result is success.
result Object Result object.

Parameters in result

Parameter Type Description
category_code String Category code.
functions Array List of function sets.

Parameters in result.functions

Parameter Type Description
code String Function code.
type String Function type.
values String Parameter value range.
name String Function name.
desc String Function description.

Request Example

OpenSaasSDK.request({ data: { action: "device.getDeviceFunctions", vsersion: "1.0", params: { "device_id": "6c760b4d96688******_switch_2" } } });

Success Response Example

{ "result": { "category": "kg", "functions": [ { "name": "Switch", "desc": "Main switch. true: all on. false: all off.", "code": "switch", "type": "Boolean", "values": "{}" } ] }, "success": true }

Failure Response Example

{ "code":500, "msg":"system error,please contact the admin", "success":false }

Method for Obtaining Device Specifications

Method Description

You can use this method to obtain device specifications.

Method Parameters

action version
device.getDeviceSpecifications 1.0

Request Parameters

Parameter Type Description
device_id String Unique ID of a device.

Response Parameters

Parameter Type Description
code Integer Response code.Null indicates success.
success Boolean Request result.
true: success
false: failure
msg String Exception message.It is null when the request result is success.
result Object Result object.

Parameters in result

Parameter Type Description
category_code String Category code.
functions Array List of function sets.
status Array List of status sets.

Parameters in result.functions

Parameter Type Description
code String Function code.
type String Function type.
values String Parameter value range.
name String Function name.
desc String Function description.

Parameters in result.status

Parameter Type Description
code String Status code.
type String Status type.
values String Parameter value range.

Request Example

OpenSaasSDK.request({ data: { action: "device.getDeviceSpecifications", vsersion: "1.0", params: { "device_id": "6c760b4d96688******_switch_2" } } });

Success Response Example

{ "result": { "category": "kg", "functions": [ { "name": "Switch", "desc": "Main switch. true: all on. false: all off.", "code": "switch", "type": "Boolean", "values": "{}" } ], "status":[ { "code":"switch_1", "type":"Boolean", "values":"{}" } ] }, "success": true }

Failure Response Example

{ "code":500, "msg":"system error,please contact the admin", "success":false }

Method for Controlling Devices

Method Description

You can use this method to issue commands to a device based on the unique ID of the device.

Method Parameters

action version
device.controlDevice 1.0

Request Parameters

Parameter Type Description
device_id String Unique ID of a device.
commands Array Instruction set.

Parameters in commands

Parameter Type Description
code String Command code.
value Object Command value.

Response Parameters

Parameter Type Description
code Integer Response code.Null indicates success.
success Boolean Request result.
true: success
false: failure
msg String Exception message.It is null when the request result is success.
result Boolean Operation object.

Request Example

OpenSaasSDK.request({ data: { action: "device.getDeviceSpecifications", vsersion: "1.0", params: { "device_id": "6c760b4d96688******_switch_2", "commands": [ { "code": "switch_1", "value": false } ] } } });

Success Response Example

{ "result": true, "success": true }

Failure Response Example

{ "code":500, "msg":"system error,please contact the admin", "success":false }

Method for Controlling Device Switches

Method Description

You can use this method to control a device switch based on the unique ID of the device.

Method Parameters

action version
device.controlDeviceSwitch 1.0

Request Parameters

Parameter Type Description
device_id String Unique ID of a device.
value Boolean Device switch. The values are as follows:
true: on
false: off

Response Parameters

Parameter Type Description
code Integer Response code.Null indicates success.
success Boolean Request result.
true: success
false: failure
msg String Exception message.It is null when the request result is success.
result Boolean Operation object.

Request Example

OpenSaasSDK.request({ data: { action: "device.controlDeviceSwitch", vsersion: "1.0", params: { "device_id": "6c760b4d96688******_switch_2", "value": true } } });

Success Response Example

{ "result": true, "success": true }

Failure Response Example

{ "code":500, "msg":"system error,please contact the admin", "success":false }

Method for Controlling Infrared Devices

Method Description

You can use this method to control an infrared device based on the unique ID of the device.

Method Parameters

action version
device.controlInfraredDevice 1.0

Request Parameters

Parameter Type Description
infrared_id String Unique ID of an infrared device.
standard_key String Standard key of an infrared device.

Response Parameters

Parameter Type Description
code Integer Response code.Null indicates success.
success Boolean Request result.
true: success
false: failure
msg String Exception message.It is null when the request result is success.
result Boolean Operation object.

Request Example

OpenSaasSDK.request({ data: { action: "device.controlInfraredDevice", vsersion: "1.0", params: { "infrared_id": "6c760b4d966882******", "standard_key": "PowerOn" } } });

Success Response Example

{ "result": true, "success": true }

Failure Response Example

{ "code":500, "msg":"system error,please contact the admin", "success":false }

Method for Obtaining the Latest Status of an Infrared Device

Method Description

You can use this method to obtain the latest status of an infrared device based on the unique ID of the device.

Method Parameters

action version
device.getInfraredStatus 1.0

Request Parameters

Parameter Type Description
infrared_id String Unique ID of an infrared device.

Response Parameters

Parameter Type Description
code Integer Response code.Null indicates success.
success Boolean Request result.
true: success
false: failure
msg String Exception message.It is null when the request result is success.
result Object Result Object

Request Example

OpenSaasSDK.request({ data: { action: "device.getInfraredStatus", vsersion: "1.0", params: { "infrared_id": "6c760b4d966882******" } } });

Success Response Example

{ "result": { "mode": "4", "temp": "22", "power": "1", "wind": "2" }, "success": true }

Failure Response Example

{ "code":500, "msg":"system error,please contact the admin", "success":false }

Method for Obtaining the Scene List

Method Description

You can use this method to obtain the scene list.

Method Parameters

action version
scene.listScenes 1.0

Response Parameters

Parameter Type Description
code Integer Response code.Null indicates success.
success Boolean Request result.
true: success
false: failure
msg String Exception message.It is null when the request result is success.
result Array Scene list.

Parameters in result

Parameter Type Description
scene_id String Unique ID of a scene.
name String Scene name.

Request Example

OpenSaasSDK.request({ data: { action: "scene.listScenes", vsersion: "1.0" } });

Success Response Example

{ "result": [ { "scene_id": "*******", "name": "Turn on all lights" } ], "success": true }

Failure Response Example

{ "code":500, "msg":"system error,please contact the admin", "success":false }

Method for Triggering Scenes

Method Description

You can use this method to trigger a scene based on the unique ID of the scene.

Method Parameters

action version
scene.triggerScene 1.0

Request Parameters

Parameter Type Description
scene_id String Unique ID of a scene.

Response Parameters

Parameter Type Description
code Integer Response code.Null indicates success.
success Boolean Request result.
true: success
false: failure
msg String Exception message.It is null when the request result is success.
result Boolean Operation result.

Request Example

OpenSaasSDK.request({ data: { action: "scene.triggerScene", vsersion: "1.0", params: { "scene_id": "6c760******f" } } });

Success Response Example

{ "result": true, "success": true }

Failure Response Example

{ "code":500, "msg":"system error,please contact the admin", "success":false }

MQTT Connection and Message Listening

Importing a Module

import { SaasMqtt } from 'xxx/openSaasSdk'

Method for Establishing an MQTT Connection

Method description: You can use this method to establish an MQTT connection.
Method: SaasMqtt.connectMqtt(config)

Parameters Description

Parameter Type Description
config Object MQTT connection configuration.

Parameters in config

Parameter Type Description
client_id String ID of the client connected to MQTT. The value a unique and constant mapping generated based on the user account and unique_id. One client ID can be used for both publishing and subscription.
expire_time String Expiration time of the current configuration.When the current configuration is invalid, all connections are broken.
username String User name in an MQTT connection. The value is a unique and constant mapping generated based on the user account.
password String Password in an MQTT connection.This parameter remains unchanged before the expiration time.
sink_topic Map MQTT topic corresponding to device control messages delivered by a user. The format is cloud/token/out/{dev_id}. In the format, {dev_id} indicates the ID of the controlled IoT device.
source_topic Map MQTT topic corresponding to messages pushed from a device to the account. Multiple client IDs can be used to obtain connections.
url String MQTT connection URL, which contains the protocol, IP address, and port number.

Example

async function ({ const  { result } = await OpenSaasSDK.getgetMqttConfig() SaasMqtt.connectMqtt(result) } 

Method for Listening to Messages

Method description: You can use this method to listen to MQTT instances.
Method: SaasMqtt.on(event, fn)

Parameters Description

Parameter Type Description
event Object Name of a listening event.
fn Function Function for handling a listening event.

Parameters in event

Parameter Type Description
close String Connection closure.
disconnect String Disconnection.
offline String Going offline.
error String Error.
message String Message receiving.

Special Case
When the listening event is message, three callback parameters are available for the fn callback function.

Parameters in event

Parameter Type Description
topic String Message queue topic.
message String Message body.
packet String Packet.

Example

SaasMqtt.on('message'(topic, message) => { console.log(message) });

We recommend that you assign the MQTT object to a global variable and configure the message body to process the message push events of the corresponding device.

Precautions

MQTT disconnection often occurs due to the WebSocket mechanism of WeChat. Therefore, you need to add the following code to the onShow lifecycle listening function in the app.js file:

// Obtain MQTT connection configuration (-config). SassMqtt.connectMqtt(config) SassMqtt.on(‘close’, (errMsg) => { // Obtain the MQTT connection configuration. SassMqtt.connectMqtt(config) console.log(‘errorMsg: mqttClose’, errMsg) }) SassMqtt.on(‘error, (errMsg) => { // Obtain the MQTT connection configuration. SassMqtt.connectMqtt(config) console.log(‘errorMsg: mqttError, errMsg) })