Tutorial to use Tuya Cloud Development Platform for your first IoT Development Practice

In the tutorial, we will introduce how to configure a Tuya Cloud Project, add devices, create a simulation with virtual devices, debug devices, and use Tuya-connector-Python SDK to develop a program to control ‘Powered by Tuya’ devices.

Configure Cloud Project

After registering an account at https://iot.tuya.com/, log in and go to Cloud>Development, choose to Create Cloud Project.

Fill the information in Project Name, Description, Development Method, Industry, Availability Zone.

To understand about development method, see Development Method.

Click Create and subscribe to the API products On the Authorize API Products page -> Click Authorize

Next, create the original asset and original account

Add Virtual device

Add devices with IoT Device Management app

In order to add your physical devices, you need to install the IoT Device Management app. After installing the IoT Device Management app, click Add Device -> Add Devices with IoT Device Management App, and the Scan the QR code with the IoT Device Management app to authorize the project.

You can log in to the IoT Device Management app using the initial account configured when the project was created. You also manage your account and the Tab Users.

After login, you can create your Assets and Pair your Smart Device.

A device can be added in two methods, Add Manually and Auto Scan. They are similar to those used to add devices on all-in-one apps. For more information, see Add a device.

Make a simulation with Virtual devices

You can create a simulation by adding Virtual devices to your cloud project. Choose Cloud->Development->Devices->All Devices->Add Device->Add Virtual Device

You can choose different types of devices for your application such and home security, household appliance, Health & Personal Care.

You can click “Buy” the physical devices at a good price. Tuya also provides OEM solutions for your product, when you can customize Logo, packing, User material, and user apps.

 

Debug Devices

You can make your simulation and use debugging feature to send instructions to control your devices.

Choose Cloud -> Development -> select your project -> Devices -> Debug Devices

Modify your  Value in Instruction Set -> Click Send Instruction

You also can modify the Instruction Set by choosing JSON Editing

You can click Update Device Status to get the Lastest status of the device.

Click Device Logs, you can query logs by DP ID, device event, and time.

Develop a program to control ‘Powered by Tuya’ devices

Next step, we can use Tuya-connector-Python SDK to control your devices through your Cloud development projects.

Firstly, download the SDK at Tuya-connector-Python.

You need to install a tuya-connector-python on your computer for requirements.

pip3 install tuya-connector-python

You need to modify ACCESS_ID, ACCESS_KEY, API_ENDPOINT, MQ_ENDPOINT to connect to your Cloud Project.

Go to Overview Tab to get that parameters

ACCESS_ID and ACCESS_KEY: Enter the values of Access ID and Access Key in the Authorization Key section on the Cloud Development Platform. For more information, see Query project information.

API_ENDPOINT: the data center address of API requests. See the tuya-connector-python/tuya_connector/tuya_enums.py file.

MQ_ENDPOINT: the request address of message subscription.

Location API_ENDPOINT MQ_ENDPOINT
China https://openapi.tuyacn.com wss://mqe.tuyacn.com:8285/
America https://openapi.tuyaus.com wss://mqe.tuyaus.com:8285/
Europe https://openapi.tuyaeu.com wss://mqe.tuyaeu.com:8285/
India https://openapi.tuyain.com wss://mqe.tuyain.com:8285/

How to Control Devices with Python

Use the sample code of querying devices and issuing instructions: device_control.py.

import logging
from tuya_connector import TuyaOpenAPI, TUYA_LOGGER

ACCESS_ID = "yhyrrpky*****gc0xmj9"
ACCESS_KEY = "ad976bf8*****da383ec37311e65f554"
API_ENDPOINT = "https://openapi.tuyacn.com"

# Enable debug log
TUYA_LOGGER.setLevel(logging.DEBUG)

# Init OpenAPI and connect
openapi = TuyaOpenAPI(API_ENDPOINT, ACCESS_ID, ACCESS_KEY)
openapi.connect()

# Set up device_id
DEVICE_ID ="vdevo164024617876854"

# Call APIs from Tuya
# Get the device information
response = openapi.get("/v1.0/iot-03/devices/{}".format(DEVICE_ID))

# Get the instruction set of the device
response = openapi.get("/v1.0/iot-03/devices/{}/functions".format(DEVICE_ID))


# Send commands
commands = {'commands': [{'code': 'countdown_1', 'value': 200}]}
openapi.post('/v1.0/iot-03/devices/{}/commands'.format(DEVICE_ID), commands)

# Get the status of a single device
response = openapi.get("/v1.0/iot-03/devices/{}/status".format(DEVICE_ID))

Modify DEVICE_ID with the ID of the device you want to control. You can get the Device ID at Development->Devices->All Devices

In the above code, sent commands are modified by, you should modify the code and the value depend on your devices.

commands = {'commands': [{'code': 'switch_led', 'value': true}]} openapi.post('/v1.0/iot-03/devices/{}/commands'.format(DEVICE_ID), commands)

The return should like below


[tuya-openapi] Request: method = GET, url = https://openapi.tuyacn.com/v1.0/token, params = {'grant_type': 1}, body = None, t = 1640339456098
[2021-12-24 17:50:57,518] [tuya-openapi] Response: {
"result": {
"access_token": "***",
"expire_time": 7200,
"refresh_token": "***",
"uid": "***"
},
"success": true,
"t": 1640339457334
}
[2021-12-24 17:50:57,519] [tuya-openapi] Request: method = GET, url = https://openapi.tuyacn.com/v1.0/iot-03/devices/vdevo164024617876854, params = None, body = None, t = 1640339457519
[2021-12-24 17:50:57,964] [tuya-openapi] Response: {
"result": {
"active_time": 1640246178,
"asset_id": "1473886148148346880",
"category": "cz",
"category_name": "Socket",
"create_time": 1640246178,
"gateway_id": "",
"icon": "smart/icon/1498529014_0.png",
"id": "vdevo164024617876854",
"ip": "***",
"lat": "***",
"local_key": "***",
"lon": "***",
"model": "HYS-X5-NEW",
"name": "Mini Smart Socket-vdevo",
"online": true,
"product_id": "qoacsyyfbrbe3lm8",
"product_name": "Mini Smart Socket",
"sub": false,
"time_zone": "+08:00",
"update_time": 1640246178,
"uuid": "vdevo164024617876854"
},
"success": true,
"t": 1640339457780
}
[2021-12-24 17:50:57,965] [tuya-openapi] Request: method = GET, url = https://openapi.tuyacn.com/v1.0/iot-03/devices/vdevo164024617876854/functions, params = None, body = None, t = 1640339457965
[2021-12-24 17:50:58,382] [tuya-openapi] Response: {
"result": {
"category": "cz",
"functions": [
{
"code": "switch_1",
"desc": "switch 1",
"name": "switch 1",
"type": "Boolean",
"values": "{}"
},
{
"code": "countdown_1",
"desc": "countdown 1",
"name": "countdown 1",
"type": "Integer",
"values": "{\"unit\":\"s\",\"min\":0,\"max\":86400,\"scale\":0,\"step\":1}"
}
]
},
"success": true,
"t": 1640339458198
}
[2021-12-24 17:50:58,382] [tuya-openapi] Request: method = POST, url = https://openapi.tuyacn.com/v1.0/iot-03/devices/vdevo164024617876854/commands, params = None, body = {'commands': [{'code': 'countdown_1', 'value': 200}]}, t = 1640339458382
[2021-12-24 17:50:58,853] [tuya-openapi] Response: {
"result": true,
"success": true,
"t": 1640339458669
}
[2021-12-24 17:50:58,853] [tuya-openapi] Request: method = GET, url = https://openapi.tuyacn.com/v1.0/iot-03/devices/vdevo164024617876854/status, params = None, body = None, t = 1640339458853
[2021-12-24 17:50:59,284] [tuya-openapi] Response: {
"result": [
{
"code": "switch_1",
"value": false
},
{
"code": "countdown_1",
"value": 200
}
],
"success": true,
"t": 1640339459099
}

In the tutorial, we introduce how to create a Tuya Cloud Project, add devices and create Virtual devices to make a simulation for your different scenarios. Tuya provides APIs and SDK in Python to help you control your smart devices through Tuya Cloud Development Platform. You can apply and extend this tutorial for your experiment with Tuya Platform to build your IoT applications and services fastly.

In the next tutorial, we want to show you “how to design a Smart Security app for home and small business users”. In order to implement DIY home security, the Tuya platform can support Video surveillance, voice chat, and smart control. You can add smart devices and set up a smart home scenario. You can also view live video and cloud-stored video, and communicate timely by voice.