Is this page helpful?
YesNoLast Updated on : 2021-11-17 06:41:38download
This topic describes how to develop a program to control smart devices based on the open capabilities and SDK of the Tuya IoT Development Platform.
Tuya-iot-python-sdk
This step is based on Tuya-iot-python-sdk. Tuya-iot-python-sdk
helps you efficiently create cloud development projects regarding the OpenAPI or message subscription capabilities. You can put all the focus on business logic without taking care of server-side programming nor relational databases. Perform the following steps:
Get the SDK address at Tuya-iot-python-sdk.
Go to Projects > Get from VCS > URL, enter the SDK address, and then click Clone.
Before development, you need to configure environment variables in the env.py
file. The parameters are described as follows:
ACCESS_ID
and ACCESS_KEY
: the authorization key obtained in the project overview.USERNAME
and PASSWORD
: the original account and password configured when the project was created.ENDPOINT
: The access address of the cloud development project. For more information, see Endpoints.ASSET_ID
: The asset ID. You can view it on the Assets page. For more information, see Manage Assets.DEVICE_ID
: the ID of the linked device. For more information, see View Devices.ACCESS_ID = "fsxg5c9jasdrolc****"
ACCESS_KEY = "d064654cd2714286a3823f16a223****"
USERNAME = "*********"
PASSWORD = "*********"
ASSET_ID = "141998818184296****"
DEVICE_ID = "vdevo16274430851****"
ENDPOINT = "https://openapi.tuyacn.com"
After the environment is ready, you can start your coding journey. Device control is a must-have feature. The following example shows how to switch the strip lights after you press Enter on the keyboard. It is just for your reference.``
The strip lights are controlled in this example. The standard instruction for turning lights on/off is the switch_led
. If you want to control other devices, query the standard instruction set and modify the code.
from env import ENDPOINT, ACCESS_ID, ACCESS_KEY, USERNAME, PASSWORD, DEVICE_ID
from tuya_iot import TuyaOpenAPI, tuya_logger
# Initialization of Tuya OpenAPI
openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY)
openapi.login(USERNAME, PASSWORD)
# # Uncomment the following lines to see logs.
import logging
tuya_logger.setLevel(logging.DEBUG)
flag = True
while True:
input('Hit Enter to toggle light switch.')
flag = not flag
commands = {'commands': [{'code': 'switch_led', 'value': flag}]}
openapi.post('/v1.0/iot-03/devices/{}/commands'.format(DEVICE_ID), commands)
Sample response
[2021-07-28 16:52:51,784] [tuya-openapi] Request: method = POST, url = https://openapi.tuyacn.com/v1.0/iot-03/devices/vdevo16274430851****/commands, params = None, body = {'commands': [{'code': 'switch_led', 'value': True}]}, headers = {'client_id': 'fsxg5c9jasdrolcz****', 'sign': '95ECEB80EC21C990A7D84935722C99CBDD80EECBE07418A3E72D9E01F2C7****', 'sign_method': 'HMAC-SHA256', 'access_token': '1bd0e0bbf6a73a09c4094ece5b9****', 't': '1627462371784', 'lang': 'en'}
[2021-07-28 16:52:51,900] [tuya-openapi] Response: {
"result": true,
"success": true,
"t": 1627462372040
}
Hit Enter to toggle light switch.
The following sample code shows how to implement listening for device events for a user. For example, devices get offline or offline, and device status is reported. Device status notifications are sent as MQTT messages to actively notify the terminal application of real-time data, such as changes in device status and device alert messages. This way, the terminal application does not need to frequently query the device status.
The Device status notification cloud service is required to enable this feature. For more information, see Subscribe to API services.
from env import ENDPOINT, ACCESS_ID, ACCESS_KEY, USERNAME, PASSWORD
from tuya_iot import TuyaOpenAPI, TuyaOpenMQ, tuya_logger
# Uncomment the following lines to see logs.
import logging
tuya_logger.setLevel(logging.DEBUG)
# Initialization of Tuya OpenAPI
openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY)
openapi.login(USERNAME, PASSWORD)
# Receive device messages
def on_message(msg):
print("on_message: %s" % msg)
openmq = TuyaOpenMQ(openapi)
openmq.start()
openmq.add_message_listener(on_message)
Sample response
[2021-07-27 10:37:28,087] [tuya-openmq] payload-> b'{"data":"AAAADN2q/ueLzZE78ubHsQ9Nk+iUBd7QOfPyB78P2Fspm2rMMHYymWYVnZL1m*****/kpPaGVDXsAIlrIIaM6k6wWWAezW7SJLF6gER*****/GQ24QYahT3UvadDx4v45sWuyssg33sX1uC0PFP9kvghc8o9Q1QELGFgmc7JPb0FQ4HDxW98DJPCeIgYTWmmz+BHoaDbH6iOV0SEcsi6vlFGtYQ*****/t/O/g0bTT9RdnmLbGxllJjedwBA2c6XPKNv7HHrouF7LfTDmXwFHml5q6*****","protocol":4,"pv":"2.0","sign":"65f7dcb596587db3f7d9374e93484e7eb44d938f287fae4dcb3eaf721*****","t":1627353446615}'
[2021-07-27 10:37:28,091] [tuya-openmq] on_message: {'data': {'dataId': '356c93f2-3929-4a8f-ae28-ae5607efc7ad', 'devId': '***1626937310799', 'productKey': 'yju2******ujr5zx', 'status': [{'code': 'switch_led', 't': '1627353446615', 'value': False, '20': False}]}, 'protocol': 4, 'pv': '2.0', 'sign': '65f7dcb59************74e93484e7************fae4dcb3e******fbee66', 't': 1627353446615}
This topic describes how to use the Python SDK based on the Tuya IoT Development Platform. You can call device APIs to control smart devices and listen for device status. Thanks to the standard ecosystem of devices, you can extend this control method to all ‘Connected by Tuya’ devices, and accelerate your SaaS development based on smart devices without regard to device differences.
Is this page helpful?
YesNoIs this page helpful?
YesNo