Tuya Wi-Fi SDK Reference
Change log
Overview
The Tuya device OS is a proprietary IoT operating system, which provides abundant components and you can make SDK customization according to your business requirements. It has the characteristic of lightweight, interconnection, secure transmission, fully functional components and rapid development, effectively lowering the development threshold and shortening the development cycle. It can be widely used in wearable devices, video monitors, compute gateways, smart speakers, smart locks and other smart home appliances.
The Tuya Wi-Fi SDK is developed based on the Tuya device OS technology and tailored according to the Wi-Fi application in the Internet of Things scenarios. It is applicable to any Wi-Fi chip that is configured according to the Tuya Smart platform interconnection rules.
Project address
GitHub repo of the Tuya Wi-Fi SDK for RTL8710BN
Directories
The following table describes the directories of the Tuya Wi-Fi SDK.
Initialization
Process
The initialization is the process of loading and running firmware automatically after the device is powered on for initializing.
Because the Tuya Wi-Fi SDK is platform independent, differences exist in its initialization process for different products. Taking the lighting products that have SoC designs and are connected via Wi-Fi as an example, the initialization process is shown in the following picture.
The initialization of Tuya Wi-Fi SDK is almost completed in the user_main()
API. It mainly includes the following stages:
pre_init_process
: makes preparation before initializing the Tuya IoT SDK. It is optional, therefore you can implement it depending on your needs. Thepre_init_process
is vastly used for lighting products, it is recommended to turn on them as soon as possible to create a better user experience. During thepre_init_process
stage, the following actions will be performed.pre_device_init
: prints the status information and sets the importance level of logs.tuya_cnt_rst_judge
: counts the hardware devices.tuya_iot_kv_flash_init
: initializes the storage space to store the generated critical data during thepre_init_process
operation.Your customized actions, such as subscribing to events.
tuya_iot_init
: this stage is to initialize necessary functions for Tuya Wi-Fi SDK. Allocate necessary compute and storage resources.app_init
: initializes the programming code of application, and set the working mode.device_init
: performs API initialization according to the working mode. The initialization API varies for different product categories.SoC series:
tuya_iot_wf_soc_dev_init
MCU series:
tuya_iot_wf_mcu_dev_init
Gateway series:
tuya_iot_wf_gw_dev_init
To initialize a product, you must set the value of
product_key
,firmware_key
andstart_mode
(network configuration mode).
Working mode
During the app_init
stage, you must select an appropriate working mode. The mode determines the initialization, network configuration, reset and other properties of your products.
GWCM_OLD
The GWCM_OLD
mode is mainly applicable for the firmware using serial port interaction type.
Production testing: only after the MCU initiates a production testing request, which can be initiated any time, the device can scan the router that are used for production testing.
Note: if a SoC firmware uses the
OLD
mode, it cannot enter into the production testing mode.
GWCM_LOW_POWER
The GWCM_LOW_POWER
mode is mainly applicable for the sockets with SoC designs.
Before configuring the network:
After powering on, the
GWCM_LOW_POWER
mode is indicated with a constant light status. Manual switching is required to enter the network configuration status.Within 10 seconds, if the device keeps disconnected, you can restart it to re-enter the network configuration status.
After 10 seconds, if the device keeps disconnected, you can restart it to re-enter the
GWCM_LOW_POWER
status.If the device keeps disconnected within 3 minutes, it enters into the
GWCM_LOW_POWER
status automatically.
Network configured and product added
Removal from App: the device restarts and enters into the network configuration mode automatically. By default, the network configuration is the EZ mode (smart configuration). The existing value of the
SSID
andPassWD
will be erased.Manual removal: the device restarts and enters into the network configuration mode automatically. The existing value of the
SSID
andPassWD
will be erased.Network not configured:
Within 10 seconds, if the device keeps disconnected, you can restart it to re-enter the network configuration status.
After 10 seconds, if the device keeps disconnected, you can restart it to re-enter the
GWCM_LOW_POWER
status.If the device keeps disconnected within 3 minutes, it enters into the
GWCM_LOW_POWER
status automatically.
Network configured: the device is activated and connected again.
Production testing: when a device is in the
GWCM_LOW_POWER
mode, you must restart it to scan the production testing router to enter the production testing mode.
GWCM_SPCL_MODE
The SPCL_MODE
mode is mainly applicable for the lighting products with SoC designs.
Before configuring the network:
After powering on, the
SPCL_MODE
mode is indicated with a constant light status. Manual switching is required to enter the network configuration status.Within 10 seconds, if the device keeps disconnected, you can restart it to re-enter the network configuration status.
After 10 seconds, if the device keeps disconnected, you can restart it to re-enter the
GWCM_SPCL_MODE
status.If the device keeps disconnected within 3 minutes, it enters into the
LOW_POWER
status automatically.
Network configured and product added
Removal from App: the device restarts and enters into the network configuration mode automatically. By default, the network configuration is the EZ mode (smart configuration). The existing value of the
SSID
andPassWD
will be erased.Manual removal: the device restarts and enters into the network configuration mode automatically. The existing value of the
SSID
andPassWD
will be erased.Network not configured:
Within 10 seconds, if the device keeps disconnected, you can restart it to re-enter the network configuration status.
After 10 seconds, if the device keeps disconnected, you can restart it to re-enter the
GWCM_SPCL_MODE
status.If the device keeps disconnected within 3 minutes, it enters into the
GWCM_SPCL_MODE
status automatically.
Network configured: the device is activated and connected again.
Production testing: when a device is in the
GWCM_SPCL_MODE
mode, you must restart it to scan the production testing router to enter the production testing mode.
GWCM_OLD_CPT
The GWCM_OLD_CPT
mode is mainly used for electrical and lighting products with SoC designs. After powering on, products enter the network configuration state and wait for configuration.
When the device waits for network configuration, you can restart the device to make it scanned by routers and enter into the production testing mode.
GWCM_LOW_POWER_AUTO_CFG
The GWCM_LOW_POWER_AUTO_CFG
mode is mainly used for electrical products with SoC designs. Usually, those products flash when they are powered on, and enter into the LOW_POWER
mode 3 minutes later.
Before configuring the network: after powering on, the products enter into the EZ (network smart configuration) mode, and sway between the EZ and AP mode.
Network not configured: if the product is restarted within 3 minutes, it keeps the last network configuration state. If the network of the product is not configured within 3 minutes, it enters into the
LOW_POWER
mode automatically.
Network configured and device added
Removal from App: the device restarts and enters into the network configuration mode automatically. By default, the network configuration is the EZ mode (smart configuration). The existing value of the
SSID
andPassWD
will be erased.Manual removal: the device restarts and enters into the network configuration mode automatically. The existing value of the
SSID
andPassWD
will be erased.If the device keeps disconnected within 3 minutes, it enters into the
GWCM_SPCL_MODE
status automatically.Network configured: the device is activated and connected again.
Production testing: when a device is in the
GWCM_LOW_POWER_AUTO_CFG
mode, you must restart it to scan the production testing router to enter the production testing mode.
GWCM_SPCL_AUTOCFG
The GWCM_SPCL_AUTOCFG
mode is mainly used for lighting products with SoC designs.
Before configuring the network:
After powering on, the
GWCM_SPCL_AUTOCFG
mode is indicated with a constant light status. Manual switching is required to enter the network configuration status.Within 10 seconds, if the device keeps disconnected, you can restart it to re-enter the network configuration status.
After 10 seconds, if the device keeps disconnected, you can restart it to re-enter the
GWCM_LOW_POWER
status.If the device keeps disconnected within 3 minutes, it enters into the
GWCM_LOW_POWER
status automatically.
Network configured and product added
Removal from App: the device restarts and enters into the network configuration mode automatically. By default, the network configuration is the EZ mode (smart configuration). The existing value of the
SSID
andPassWD
will be erased.Network not configured:
Within 10 seconds, if the device keeps disconnected, you can restart it to re-enter the network configuration status.
After 10 seconds, if the device keeps disconnected, you can restart it to re-enter the
GWCM_SPCL_AUTOCFG
status.If the device keeps disconnected within 3 minutes, it enters into the
GWCM_LOW_POWER
status automatically.
Manual removal: the device restarts and enters into the network configuration mode automatically. The existing value of the
SSID
andPassWD
will be erased.Network configured: the device is activated and connected again.
Production testing: when a device is in the
GWCM_SPCL_AUTOCFG
mode, you must restart it to scan the production testing router to enter the production testing mode.
API reference
TY_IOT_CBS_S
In the app_init
phase of the initial process, users need to provide some callbacks according to their needs. The SDK will notify the application of the corresponding state change through the callback at the corresponding stage.
Description
The SDK callback interface.
Member description
tuya_iot_init
Prototype
Description
Initializes the Tuya IoT system. And it must be called at first.
Parameter
Return value
tuya_iot_get_sdk_info
Prototype
Description
Obtains the Tuya IoT SDK version information.
Parameter
Return value
tuya_iot_sys_mag_hb_init
Prototype
Description
Enables heartbeat management capabilities of child devices.
Parameter
Return value
tuya_iot_set_wf_gw_prod_info
Prototype
Description
Sets the authorization information of the configuration Wi-Fi device. The authorization information needs to be obtained through Tuya, otherwise the device cannot be used normally.
Parameter
Return value
tuya_iot_wf_mcu_dev_init
Prototype
Description
The metworking module and MCU device initialization interface.
Parameter
Return value
tuya_iot_wf_soc_dev_init
Prototype
Description
The Wi-Fi SoC device initialization interface.
Parameter
Return value
tuya_iot_wf_gw_init
Prototype
Description
The Wi-Fi gateway initialization interface.
Parameter
Return value
tuya_iot_wf_gw_dev_init
Prototype
Description
The Wi-Fi gateway and device initialization interface. Compared with tuya_iot_wf_gw_init
, this function makes the gateway have the attributes of the device and can set function points.
Parameter
Refers to tuya_iot_wf_gw_init
interface description.
Return value
Refers to tuya_iot_wf_gw_init
interface description.
tuya_iot_reg_get_wf_nw_stat_cb
Prototype
Description
Gets Wi-Fi status interface.
Parameter
Return value
Network configuration
The process
After the device is initialized, the most important follow-up is to configure the network so that the device can connect to the cloud through the router. If the device has been connected, you do not need to configure the network after device restarting or initializing.
You need to determine the working mode supported by the Wi-Fi network card, and inform the Tuya Wi-Fi SDK the network configuration mode of the device when the tuya_iot_wf_soc_dev_init
or tuya_iot_wf_gw_dev_init
is initialized. Refer to the description of start_mode
parameters of the interface for more information. During the network configuration, you must use a mobile App to interact with the device, and send the ssid
, pswd
, token
, and other information through coding, broadcast packets and multicast packets until the network is configured.
EZ mode
EZ indicates fast configuration. When the Wi-Fi module works in the monitor
mode, the device can easily to receive and resolve the Wi-Fi beacon packets that are sent by the mobile App, and get configuration information from them. Later, the Wi-Fi module turns to the station
mode and wait for network configuration.
AP network configuration mode
The process of AP network configuration is relatively complicated. In this mode, devices must enable AP hotspot, and wait for network connection from mobile phone. Next, the configuration information is sent via LAN network to the device, and the Wi-Fi module turns to the station
mode and wait for network configuration.
Other network configuration mode
The Tuya Wi-Fi SDK supports other network configuration mode, such as QR code scanning or Bluetooth communication. In those configuration mode, your application obtains the ssid
, passwd
, and token
via the following methods and directly applies for device joining by calling APIs.
QR code: the devices scan the QR code that are generated by App to obtain the
ssid
,passwd
, andtoken
information.Easy routing: the devices obtain the
ssid
,passwd
, andtoken
information via routers.FFS: the devices obtain the
ssid
,passwd
, andtoken
information via third party devices, such as Amazon Echo.Sound wave: the devices obtain the encoded
ssid
,passwd
, andtoken
information via serial ports or sound waves.Bluetooth: the devices obtain the
ssid
,passwd
, andtoken
information via Bluetooth.
The network configuration mode
You can choose the network configuration mode by calling the device_init()
API, and the following values are supported.
AP only (
WF_START_AP_ONLY
): only works in the AP configuration mode.Smart only (
WF_START_SMART_ONLY
): works in thesmart config
mode.AP first (
WF_START_AP_FIRST
): works both in the AP configuration mode and thesmart config
mode, but the AP configuration mode is the default setting.Smart first (
WF_START_SMART_FIRST
): works both in the AP configuration mode and thesmart config
mode, but thesmart config
mode is the default setting.Both AP and Smart (
WF_START_SMART_AP_CONCURRENT
): works both in the AP configuration mode and thesmart config
mode.
API reference
tuya_hal_wifi_all_ap_scan
Prototype
Description
Scans all AP.
Parameter
Return value
tuya_hal_wifi_assign_ap_scan
Prototype
Description
Scans the specified AP.
Parameter
Return value
tuya_hal_wifi_release_ap
Prototype
Description
Release processing of resources.
Parameter
Return value
tuya_hal_wifi_set_cur_channel
Prototype
Description
Sets the Wi-Fi working channel.
Parameter
Return value
tuya_hal_wifi_get_cur_channel
Prototype
Description
Gets the current working channel.
Parameter
Return value
tuya_hal_wifi_sniffer_set
Prototype
Description
Wi-Fi device promiscuous mode setting.
Parameter
Return value
tuya_hal_wifi_get_ip
Prototype
Description
Gets Wi-Fi device ip information.
Parameter
Return value
tuya_hal_wifi_get_mac
Prototype
Description
Gets the MAC address of the Wi-Fi device.
Parameter
Return value
tuya_hal_wifi_set_mac
Prototype
Description
Sets the MAC address of the Wi-Fi device is optional.
Parameter sescription
Return value
tuya_hal_wifi_wk_mode_set
Prototype
Description
Sets the Wi-Fi working mode.
Parameter
Return value
tuya_hal_wifi_wk_mode_get
Prototype
Description
Gets Wi-Fi working mode.
Parameter
Return value
tuya_hal_wifi_station_connect
Prototype
Description
Sets up the Wi-Fi device to establish a connection with the AP/hotspot/Wi-Fi router.
Parameter
Return value
tuya_hal_wifi_station_disconnect
Prototype
Description
Disconnect the Wi-Fi device from the AP/hotspot/Wi-Fi router.
Parameter
VOID.
Return value
tuya_hal_wifi_station_get_conn_ap_rssi
Prototype
Description
Gets the signal strength of the Wi-Fi device in station mode connected to the AP.
Parameter
Return value
tuya_hal_wifi_station_stat_get
Prototype
Description
Gets the connection status of the Wi-Fi device in station mode.
Parameter
Return value
tuya_hal_wifi_set_country_code
Prototype
Function sescription
Sets the country code.
Parameter sescription
Return value
tuya_hal_wifi_ap_start
Prototype
Description
Starts the Wi-Fi AP hotspot.
Parameter
Return value
tuya_hal_wifi_ap_stop
Prototype
Description
Stop Wi-Fi AP hotspot.
Parameter
None.
Return value
tuya_iot_gw_wf_user_cfg
Prototype
Description
When the network is not configured in the AP or EZ mode, call this interface for processing.
Parameter
Return value
Device reset
APP reset
You can reset a device via App. The APP reset APIs send a reset message through MQTT to reset the device.
Local reset
Alternatively, the SDK retains the ability to reset the gateway locally, for example, you can reset the device locally by pressing buttons.
tuya_iot_gw_unactive
Description
Resets the device to unassociate the gateway with the App, so that the gateway is in an inactive state.
Note: This function is asynchronous, and it will trigger the gw_reset_cb callback after execution, you can restart the thread in the callback.
Parameter
None.
Return value
Firmware upgrade
The process
Configuration of firmware package
After the gateway or device is successfully configured, App obtains the virtual ID of the device, and add it to the white list for firmware upgrade.
Compile the firmware package that is to be upgraded, and the firmware version is newer than the firm that is running in the device.
Log on to the Tuya IoT console, find the relevant product, and upload the compiled firmware package.
Perform the follow-ups. For more information, see Upgrade Firmwares.
How the upgrade starts
After the firmware is uploaded to cloud, your devices will not receive the upgrade notification immediately. At present, the following upgrade notification is available.
App notification: when your users switch to the App panel of the devices, they receive an upgrade reminder box and can choose to upgrade or not.
Silent upgrade: after the devices are restarted, they will ask the cloud if there is a silent upgrade task, and complete the upgrade automatically. If your users happen to switch to the App panel of the devices during the upgrade, an progress tracker will be displayed, and the device cannot be operated for a while.
Forcible upgrade: when your users switch to the App panel of the devices for the first time, they receive the upgrade reminder pop-up and only confirmation is provided. The equipment cannot be operated for a while.
Upgrade detection: your users switch to the App panels of the devices and click the upper right corner to enter the information center to check the firmware version of the device, and proactively update the firmware.
The firmware upgrade process
API reference
tuya_iot_upgrade_gw_notify
Prototype
Description
The networking module firmware upgrade processing interface.
Parameter
Return value
tuya_iot_dev_upgd_progress_rept
Prototype
Description
Reports the upgrade progress.
Parameter
Return value
Data point of devices
The process
Tuya Smart provides a network application protocol based on MQTT to achieve device control and status reporting. MQTT is a lightweight message transmission protocol designed specifically for the Internet of things applications in low bandwidth and unstable network environment.
The Tuya Wi-Fi SDK encapsulates the APIs for MQTT protocol layer, which is presented in the form of data points (hereinafter referred to as DP points). It supports data type of integer, Boolean, enum, string, bitmap, and raw, which are the same as defining variables in C programming language.
You must create corresponding data points on the Tuya IoT console according to the device functions, and create a new DP Point description. For more information, see Overview of function definition.
Features
At present, up to 35 DPs for each product are supported. To develop complex functions, the raw data is recommended.
The objects such as Boolean, integer, string, enum, and bitmap, the Tuya Wi-Fi SDK filters the reported values, and the repeated data will be dropped.
API reference
dev_obj_dp_cb
Prototype
Description
The OBJ function point information command callback.
Parameter
Return value
dev_raw_dp_cb
Prototype
Description
The command callback for function points passthrough.
Parameter
Return value
dev_dp_query_cb
Prototype
Description
The device specific data query entry. It is an optional implementation.
Parameter
Return value
dev_report_dp_json_async
Prototype
Description
The report function point information in an asynchronous way.
Parameter
Return value
dev_report_dp_raw_sync
Prototype
Description
The device data reporting interface in the passthrough method, and the caller guarantees the reliability of data reporting.
Parameter
Return value
dev_report_dp_stat_sync
Prototype
Description
The structured device data reporting interface in the passthrough method, and the caller guarantees the reliability of data reporting. It is usually used for statistical data reporting.
Parameter
Return value
Log management
The Tuya Wi-Fi SDK can collect and redirect logs. By default, the log is printed to a standard terminals and can be redirected if needed, such as printing to a file or the cloud.
AddOutputTerm
Prototype
Description
Adds an output callback for the tuya_SDK log to write the log to a file.
Parameter
Return value
DelOutputTerm
Prototype
Description
Unregisters the log callback registered by the AddOutputTerm interface.
Parameter
Return value VOID
SetLogManageAttr
Prototype
Description
Sets the log level.
Parameter
Return value
Timer
Add a system timer
Prototype
Description
Adds a system timer.
Parameter
Return value
sys_stop_timer
Prototype
Description
Stops a timer.
Parameter
Return value
IsThisSysTimerRun
Prototype
Description
Determines whether a timer is running.
Parameter
Return value
sys_start_timer
Prototype
Description
Starts a timer.
Parameter
Return value
message queue
CreateMsgQueAndInit
Prototype
Description
Createa message queues.
Parameter
Return value
AddMsgNodeToQueue
Prototype
Description
Creates message queues.
Parameter
Return value
GetMsgNodeFromQueue
Prototype
Description
Gets the message node of the specified ID.
Parameter
Return value
GetFirstMsgFromQueue
Prototype
Description
Gets the message node that first enters the chain.
Parameter
Return value
GetMsgNodeNum
Prototype
Description
Gets the total number of message nodes in the linked list.
Parameter
Return value
DelAndFreeMsgNodeFromQueue
Prototype
Description
Removes the message node from the chain and free the message node memory.
Parameter
Return value
ReleaseMsgQue
Prototype
Description
Releases the memory occupied by the message queue.
Parameter
Return value
PostMessage
Prototype
Description
Delivers a message to the module (the message is executed first).
Parameter dDescription
Return value
PostInstancyMsg
Prototype
Description
Delivers urgent messages (messages are executed immediately).
Parameter
Return value
WaitMessage
Prototype
Description
Delivers urgent messages (messages are executed immediately). WaitMessage
needs to be called successfully, and DelAndFreeMsgNodeFromQueue needs to be called after the message is processed to release the message
Parameter
Return value
Task queue
CreateAndStart
Prototype
Description
Creates a task.
Parameter
return value
ThrdJoin
Prototype
Description
Creates a task.
Parameter
Return value
HAL interface
The Tuya Device OS is a platform-independent device-to-cloud operating system. To achieve platform independence, you need to implement the underlying abstract interface according to your systems. For specific implementation, refer to Tuya_GM_WIFI_SDK development environment adaptation Description.
SDK samples
The Tuya Wi-Fi SDK provides a dedicated Demo to guide you to use. For specific steps, please refer to Tuya_Wi-Fi_SDK Demo Description.
Error codes
Last updated