Last Updated on : 2022-10-17 07:54:08download
Tuya IoT Core SDK is written in C and supports the Tuya-defined device model, aiming to help developers to connect their proprietary hardware to the Tuya IoT Development Platform. This SDK contains the implementation of essential IoT capabilities including IoT device activation, bidirectional data exchange between the cloud and devices, and OTA updates. It is independent of platforms and operating systems (OS), and it can run on a single-tasking operating system. If your devices support the TCP/IP protocol stack, you can develop with this SDK to connect your devices to the platform.
Download Tuya IoT Core SDK from GitHub.
The following describes the structure of the SDK directory:
certs
: Private keys, device certificates, and server-side CA root certificates.docs
: Development documentation.libraries
: External dependencies including MQTT client, HTTP client, and Mbed TLS.interface
: The SDK function API, which is required to port to your platform.include
: SDK APIs.src
: Source code.platform
: Interface adaptation used for porting.utils
: Common tools.examples
: Routines.To get the platform authorization, you need to create a product on the Tuya IoT Development Platform. Add the obtained product and authorization information to your code to access cloud services.
Log in to the Tuya IoT Development Platform.
Click Create.
Select the required product category. Choose from the category list. For example, in the Standard Category tab, click Small Home Appliances > Heater.
Select TuyaLink. Complete the required information and click Create.
In Function Definition, click Add to add standard functions or create custom functions.
In Device Development, select and download the SDK and click Next.
Tuya offers free licenses for debugging purposes. Click Get 2 Free Licenses to request the licenses. Then, register your device using the obtained license and the device information will be shown.
Add the device information to the examples/subdevice_basic_demo/subdevice_basic_demo.c
file. Build and run the program. Then, your device can connect to cloud services. For more information about the build process, see the following Build and run the demo.
const char productId[] = "rwosj58aaqjk****";
const char deviceId[] = "6c95875d0f5ba69607****";
const char deviceSecret[] = "*******************";
This section uses Ubuntu as an example to show you how to build and run the program. The build processes also apply to Debian system.
Install make
and other dependencies.
sudo apt-get install make cmake
Navigate to the SDK directory. Create a build
folder and navigate to the build
directory. Run cmake ..
to generate a build system. Then run make
to compile the project. The generated binary files are stored in the build folder of your project.
mkdir build && cd build
cmake ..
make
Navigate to the bin
directory and run the demo. The SDK contains the basic sample code for communication, such as code for sub-device management.
./bin/subdevice_basic_demo
View log files.
If you see MQTT client connected, your device is successfully connected to the cloud.
Go back to the platform and refresh the Device Status. You will find it is Online.
Instantiate and initialize a device object tuya_iot_client_t
to assign initial values, such as product ID (PID) and authorization information, for this object.
/* instantiate the client */
tuya_mqtt_context_t* client = &client_instance;
/* initialize the client */
ret = tuya_mqtt_init(client, &(const tuya_mqtt_config_t) {
.host = "m2.tuyacn.com",
.port = 8883,
.cacert = tuya_cacert_pem,
.cacert_len = sizeof(tuya_cacert_pem),
.device_id = deviceId,
.device_secret = deviceSecret,
.keepalive = 60,
.timeout_ms = 2000,
.on_connected = on_connected,
.on_disconnect = on_disconnect,
.on_messages = on_messages
});
Define event callbacks in the application layer, used to receive the event notifications from the SDK, such as the data point (DP) data from the cloud or cloud connection status.
/* Tuya SDK event callback */
void on_messages(tuya_mqtt_context_t* context, void* user_data, const tuyalink_message_t* msg)
{
TY_LOGI("on message id:%s, type:%d, code:%d", msg->msgid, msg->type, msg->code);
switch (msg->type) {
case THING_TYPE_MODEL_RSP:
TY_LOGI("Model data:%s", msg->data_string);
break;
case THING_TYPE_PROPERTY_SET:
TY_LOGI("property set:%s", msg->data_string);
break;
case THING_TYPE_PROPERTY_REPORT_RSP:
break;
default:
break;
}
printf("\r\n");
}
Start TuyaOS SDK service.
ret = tuya_mqtt_connect(client);
// TuyaOS SDK service tasks such as handling data and keepalive mechanism.
Loop the following function to create threads for the underlying Link SDK client.
tuya_mqtt_loop(client);
Define the function for data reporting. Call the corresponding API to report data to the cloud.
The example below demonstrates that a product reports specified data to the cloud when it is connected to the cloud. You can implement your application for data reporting as needed.
void on_connected(tuya_mqtt_context_t* context, void* user_data)
{
TY_LOGI("on connected");
/* data model test code */
tuyalink_thing_data_model_get(context, NULL);
tuyalink_thing_desired_get(context, NULL, "[\"power\"]");
tuyalink_thing_property_report(context, NULL, "{\"power\": {\"value\":1234,\"time\":1631708204231}}");
tuyalink_thing_property_report_with_ack(context, NULL, "{\"power\":{\"value\":1234,\"time\":1631708204231}}");
tuyalink_thing_event_trigger(context, NULL, "{\"eventCode\":\"boom\",\"eventTime\":1626197189630,\"outputParams\":{\"param1\":100}}");
tuyalink_thing_batch_report(context, "{\"msgId\":\"45lkj3551234001\",\"time\":1626197189638,\"sys\":{\"ack\":1},\"data\":{\"properties\":{\"power\":{\"value\":11,\"time\":1626197189638}},\"events\":{\"boom\":{\"outputParams\": {\"param1\":\"10\"},\"eventTime\":1626197189001}}}}");
}
/* Report data to the cloud */
/* data model code */
tuyalink_thing_data_model_get(context, NULL);
tuyalink_thing_desired_get(context, NULL, "[\"power\"]");
tuyalink_thing_property_report(context, NULL, "{\"power\":{\"value\":1234,\"time\":1631708204231}}");
tuyalink_thing_property_report_with_ack(context, NULL, "{\"power\":{\"value\":1234,\"time\":1631708204231}}");
tuyalink_thing_event_trigger(context, NULL, "{\"eventCode\":\"boom\",\"eventTime\":1626197189630,\"outputParams\":{\"param1\":100}}");
tuyalink_thing_batch_report(context, "{\"msgId\":\"45lkj3551234001\",\"time\":1626197189638,\"sys\":{\"ack\":0},\"data\":{\"properties\":{\"power\":{\"value\":11,\"time\":1626197189638}},\"events\":{\"boom\":{\"outputParams\":{\"param1\":\"10\"},\"eventTime\":1626197189001}}}}");}
/* subdevice code */
tuyalink_subdevice_bind(context, "[{\"productId\":\"jtwe4q9jrs0bbc8q\",\"nodeId\":\"123456\",\"clientId\":\"123455asdf\"}]");
tuyalink_subdevice_bind_login(context, "[\"6c17c5ba952143f592b8g1\",\"6c41626e5cea758aees0ik\"]");
tuyalink_subdevice_bind_logout(context, "[\"6c17c5ba952143f592b8g1\"]");
tuyalink_subdevice_topo_add(context, "[{\"productId\":\"jtwe4q9jrs0bbc8q\",\"deviceId\":\"6c17c5ba952143f592b8g1\",\"sign\":\"366508ed895644e70a3006bdef2dbe77ef73e18a\",\"signMethod\":\"hmacSha1\",\"timestamp\":\"1636989480\"}]");
tuyalink_subdevice_topo_delete(context,"[\"6c41626e5cea758aees0ik\"]");
tuyalink_subdevice_topo_get(context);
When a device is connected to the MQTT server and goes online, you can start debugging code online.
Click the Online Debug tab and select a device. Enter the DeviceId of the online device to show the collection of the defined data points (DPs).
The right screen shows the device logs in real time.
You can click Get to request the current device data.
You can click Set to change the property value. The payload data will be displayed in the log on the right screen. You can also view the payload in the local log file.
API | Description |
---|---|
Function prototype | int tuya_mqtt_init(tuya_mqtt_context_t* context, const tuya_mqtt_config_t* config); |
Description | Device initialization |
Input parameter | The handle to the client context Initial configuration |
Output parameter | None |
Return value | See Error Codes (int). |
API | Description |
---|---|
Function prototype | int tuya_mqtt_connect(tuya_mqtt_context_t* context); |
Description | Start SDK services. |
Input parameter | The handle to the client context |
Output parameter | None |
Return value | See Error Codes (int). |
API | Description |
---|---|
Function prototype | int tuya_mqtt_disconnect(tuya_mqtt_context_t* context); |
Description | Stop SDK services. |
Input parameter | The handle to the client context |
Output parameter | None |
Return value | See Error Codes (int). |
API | Description |
---|---|
Function prototype | int tuya_mqtt_loop(tuya_mqtt_context_t* context); |
Description | The SDK services run in the background. |
Input parameter | The handle to the client context |
Output parameter | None |
Return value | See Error Codes (int). |
Notes | Call this function in the main loop. |
API | Description |
---|---|
Function prototype | int tuyalink_thing_data_model_get(tuya_mqtt_context_t* context, const char* device_id); |
Description | Get the things data model. |
Input parameter | The handle to the client context Device ID |
Output parameter | None |
Return value | See Error Codes (int). |
Notes | Call this function to get things data model of your device. |
API | Description |
---|---|
Function prototype | int tuyalink_thing_property_report(tuya_mqtt_context_t* context, const char* device_id, const char* data);| |
Description | Report device properties |
Input parameter | The handle to the client context Device ID The device properties reported |
Output parameter | None |
Return value | See Error Codes (int). |
Notes | Call this function to report device properties. |
API | Description |
---|---|
Function prototype | int tuyalink_thing_property_report_with_ack(tuya_mqtt_context_t* context, const char* device_id, const char* data);| |
Description | Report device properties |
Input parameter | The handle to the client context Device ID The device properties reported |
Output parameter | None |
Return value | See Error Codes (int). |
Notes | Call this function to report device properties and wait for an acknowledgment from the cloud. |
API | Description |
---|---|
Function prototype | int tuyalink_thing_event_trigger(tuya_mqtt_context_t* context, const char* device_id, const char* data);| |
Description | Respond to device events |
Input parameter | The handle to the client context Device ID The event data |
Output parameter | None |
Return value | See Error Codes (int). |
Notes | Call this function to process device events. |
API | Description |
---|---|
Function prototype | int tuyalink_thing_batch_report(tuya_mqtt_context_t* context, const char* data);| |
Description | The device reports status data to the cloud in bulk. |
Input parameter | The handle to the client context Data |
Output parameter | None |
Return value | See Error Codes (int). |
Notes | The device reports status data to the cloud in bulk. |
API | Description |
---|---|
Function prototype | int tuyalink_subdevice_bind(tuya_mqtt_context_t* context, const char* data);| |
Description | Bind sub-devices |
Input parameter | The handle to the client context Data |
Output parameter | None |
Return value | See Error Codes (int). |
Notes | Bind sub-devices |
API | Description |
---|---|
Function prototype | int tuyalink_subdevice_bind_login(tuya_mqtt_context_t* context, const char* data);| |
Description | Sub-device goes online |
Input parameter | The handle to the client context Data |
Output parameter | None |
Return value | See Error Codes (int). |
Notes | Sub-device goes online |
API | Description |
---|---|
Function prototype | int tuyalink_subdevice_bind_logout(tuya_mqtt_context_t* context, const char* data);| |
Description | Sub-device goes offline |
Input parameter | The handle to the client context Data |
Output parameter | None |
Return value | See Error Codes (int). |
Notes | Sub-device goes offline |
API | Description |
---|---|
Function prototype | int tuyalink_subdevice_topo_add(tuya_mqtt_context_t* context, const char* data);| |
Description | Add topology for sub-devices |
Input parameter | The handle to the client context Data |
Output parameter | None |
Return value | See Error Codes (int). |
Notes | Add topology for sub-devices |
API | Description |
---|---|
Function prototype | int tuyalink_subdevice_topo_delete(tuya_mqtt_context_t* context, const char* data);| |
Description | Delete topology for sub-devices |
Input parameter | The handle to the client context Data |
Output parameter | None |
Return value | See Error Codes (int). |
Notes | Delete topology for sub-devices |
API | Description |
---|---|
Function prototype | int tuyalink_subdevice_topo_get(tuya_mqtt_context_t* context);| |
Description | Get topology for sub-devices |
Input parameter | The handle to the client context |
Output parameter | None |
Return value | See Error Codes (int). |
Notes | Get topology for sub-devices |
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
#include "tuya_cacert.h"
#include "tuya_log.h"
#include "tuya_error_code.h"
#include "system_interface.h"
#include "mqtt_client_interface.h"
#include "tuyalink_core.h"
const char productId[] = "3jbcpefnn1jxxxxx";
const char deviceId[] = "6ced2aa564727c01xxxxx";
const char deviceSecret[] = "ac5d367db39xxxxx";
tuya_mqtt_context_t client_instance;
void on_connected(tuya_mqtt_context_t* context, void* user_data)
{
TY_LOGI("on connected");
/* data model test code */
tuyalink_thing_data_model_get(context, NULL);
tuyalink_thing_desired_get(context, NULL, "[\"power\"]");
tuyalink_thing_property_report(context, NULL, "{\"power\":{\"value\":1234,\"time\":1631708204231}}");
tuyalink_thing_property_report_with_ack(context, NULL, "{\"power\":{\"value\":1234,\"time\":1631708204231}}");
tuyalink_thing_event_trigger(context, NULL, "{\"eventCode\":\"boom\",\"eventTime\":1626197189630,\"outputParams\":{\"param1\":100}}");
tuyalink_thing_batch_report(context, "{\"msgId\":\"45lkj3551234001\",\"time\":1626197189638,\"sys\":{\"ack\":0},\"data\":{\"properties\":{\"power\":{\"value\":11,\"time\":1626197189638}},\"events\":{\"boom\":{\"outputParams\":{\"param1\":\"10\"},\"eventTime\":1626197189001}}}}");
}
void on_disconnect(tuya_mqtt_context_t* context, void* user_data)
{
TY_LOGI("on disconnect");
}
void on_messages(tuya_mqtt_context_t* context, void* user_data, const tuyalink_message_t* msg)
{
TY_LOGI("on message id:%s, type:%d, code:%d", msg->msgid, msg->type, msg->code);
switch (msg->type) {
case THING_TYPE_MODEL_RSP:
TY_LOGI("Model data:%s", msg->data_string);
break;
case THING_TYPE_PROPERTY_SET:
TY_LOGI("property set:%s", msg->data_string);
break;
case THING_TYPE_PROPERTY_REPORT_RSP:
break;
default:
break;
}
printf("\r\n");
}
int main(int argc, char** argv)
{
int ret = OPRT_OK;
tuya_mqtt_context_t* client = &client_instance;
ret = tuya_mqtt_init(client, &(const tuya_mqtt_config_t) {
.host = "m2.tuyacn.com",
.port = 8883,
.cacert = tuya_cacert_pem,
.cacert_len = sizeof(tuya_cacert_pem),
.device_id = deviceId,
.device_secret = deviceSecret,
.keepalive = 60,
.timeout_ms = 2000,
.on_connected = on_connected,
.on_disconnect = on_disconnect,
.on_messages = on_messages
});
assert(ret == OPRT_OK);
ret = tuya_mqtt_connect(client);
assert(ret == OPRT_OK);
for (;;) {
/* Loop to receive packets, and handles client keepalive */
tuya_mqtt_loop(client);
}
return ret;
}
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback