Last Updated on : 2023-09-18 06:38:33download
The TuyaOS Link SDK for C is implemented in C programming and applies to the independent development of device logic services to connect to the Tuya Cloud. It packages basic service interfaces such as device activation, DP upstream and downstream transmission, and over-the-air programming (OTA). This SDK is independent of device-specified platforms and OS environments and can run on a single-task environment. You can quickly implement integration through a supported TCP/IP protocol stack and system-dependent interfaces.
For more information, see TuyaOS Link SDK Migration Guide.
Log in to the Tuya IoT Platform and create a product to get the PID. For more information, see Create Products.
After a product is created, you define the data point (DP) in the Tuya IoT Platform in terms of product functions. DP is the abstraction of functions, and each function can be defined by different function types. The Tuya IoT Platform supports six data types, including Boolean, value, enum, fault, string, and raw. For more information, see Function Definition.
A license is composed of UUID and AUTHKEY, which serves as the credential to connect to the Tuya IoT Platform. You can get the license in the step of Hardware Development on the Tuya IoT Platform.
Download in GitHub.
|–certs
(Device private key, device certificate, and server’s CA root certificate)
|–docs
(Development documentation)
|–external_libs
(External dependent libraries - cJSON, mbedTLS)
|–include
(Necessary migration interfaces for the platform, and SDK function interfaces)
|–src
(Source code to implement the SDK)
|–platform
(Interface adaptation for platform migration)
|–examples
(Routine)
Write the Product ID and authorization information to the TUYA_PRODUCT_KEY, TUYA_DEVICE_UUID, TUYA_DEVICE_AUTHKEY
defined in SDK examples/linux/switch_demo/tuya_config.h
to complete the basic information configuration.
make
./examples/linux/switch_demo/switch_demo
You can use the Smart Life app to scan the QR code generated by the routine program on Linux to bind a device.
Generate a QR code for the following URL with the QR code generation tool.
https://smartapp.tuya.com/s/p?p=<PRODUCT_KEY>&uuid=<UUID>&v=2.0
<PRODUCT_KEY> The Product ID generated in the IoT Platform.
https://smartapp.tuya.com/s/p?p=U0fxNCEnZptKnQZy&uuid=f2ef8b136911f4b0&v=2.0
Instantiate a device object tuya_iot_client_t client
and initialize it. Write the Product ID and authorization information to tuya_iot_config_t
.
/* instantiate the client */
tuya_iot_client_t client;
/* instantiate the config */
tuya_iot_config_t config = {
.productkey = <Product ID>,
.uuid = <UUID>,
.authkey = <AUTHKEY>,
.event_handler = user_event_handler_cb
};
/* initialize the client */
tuya_iot_init(&client, &config);
Define event callbacks on the application layer. The callback functions are used by the application layer to receive SDK event notifications, such as DP sending and notifications of cloud connection status:
/* Tuya SDK event callback */
void user_event_handler_on(tuya_iot_client_t* client, tuya_event_msg_t* event)
{
switch(event->id){
case TUYA_EVENT_DP_RECEIVE:
TY_LOGI("DP recv:%s", (const char*)event->data);
/* After receiving a DP command, the status DP data is reported after the command is executed.
Synchronize the app panel status */
break;
case TUYA_EVENT_MQTT_CONNECTED:
TY_LOGI("Device MQTT Connected!");
break;
...
default:
break;
}
}
Enable Tuya IoT SDK services:
tuya_iot_start(&client);
Tuya IoT SDK service tasks include data receiving and processing, device online keep-alive, and more.
tuya_iot_yield(&client);
Actively report DP:
/* Report Boolean data */
const char bool_value[] = {"{\"101\":true}"};
tuya_iot_dp_report_json(&client, bool_value);
/* Report numeric data */
const char int_value[] = {"{\"102\":123}"};
tuya_iot_dp_report_json(&client, int_value);
/* Report string data */
const char string_value[] = {"{\"103\":\"helloworld\"}"};
tuya_iot_dp_report_json(&client, string_value);
/* Report enumeration data */
const char enum_value[] = {"{\"104\":\"auto\"}"};
tuya_iot_dp_report_json(&client, enum_value);
/* Report RAW data */
const char raw_value[] = {"{\"105\":\"aGVsZA==\"}"};
tuya_iot_dp_report_json(&client, raw_value);
/* Report data of multiple DP types */
const char multiple_value[] = {"{\"101\":true,\"102\":123,\"103\":\"hellowrold\",\"104\":\"auto\",\"105\":\"aGVsZA==\"}"};
tuya_iot_dp_report_json(&client, multiple_value);
Function prototype | int tuya_iot_init(tuya_iot_client_t* client, tuya_iot_config_t* config) |
---|---|
Features | Device initialization |
Input parameters | client: device handle config: device initialization |
Output parameters | None |
Return value | int. See general error codes. |
Remark | None |
Function prototype | int tuya_iot_start(tuya_iot_client_t *client) |
---|---|
Features | Enable device SDK services. |
Input parameters | client: device handle |
Output parameters | None |
Return value | int. See general error codes. |
Remark |
Function prototype | int tuya_iot_stop(tuya_iot_client_t *client) |
---|---|
Features | Disable device SDK services. |
Input parameters | client: device handle |
Output parameters | None |
Return value | int. See general error codes. |
Remark |
Function prototype | int tuya_iot_yield(tuya_iot_client_t* client) |
---|---|
Features | SDK running in the background |
Input parameters | client: device handle |
Output parameters | None |
Return value | int. See general error codes. |
Remark | Call this service function in the main loop of the program. |
Function prototype | int tuya_iot_dp_report_json(tuya_iot_client_t* client, char* dps) |
---|---|
Features | Report the DP data to the cloud. |
Input parameters | client: device handle DPS: DP data in JSON format |
Output parameters | None |
Return value | int. See general error codes. |
Remark | Call this service function in the main loop of the program. |
{
"101":true, # Boolean
"102":18, # Numeric
"103":"hello wrold", # String
"104":"auto", # Enumeration
"105":"aGVsZA==", # RAW (base64)
"106":2 # Fault (32bit)
}
For more information about DP description, see Overview
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "tuya_log.h"
#include "tuya_config.h"
#include "tuya_iot.h"
#include "cJSON.h"
/* for APP QRCode scan test */
extern void example_qrcode_print(char* productkey, char* uuid);
/* Tuya device handle */
tuya_iot_client_t client;
#define SWITCH_DP_ID_KEY "1"
/* Hardware switch control function */
void hardware_switch_set(bool value)
{
if (value == true) {
TY_LOGI("Switch ON");
} else {
TY_LOGI("Switch OFF");
}
}
/* DP data reception processing function */
void tuya_iot_dp_download(tuya_iot_client_t* client, const char* json_dps)
{
TY_LOGD("Data point download value:%s", json_dps);
/* Parsing json string to cJSON object */
cJSON* dps = cJSON_Parse(json_dps);
if (dps == NULL) {
TY_LOGE("JSON parsing error, exit!");
return;
}
/* Process dp data */
cJSON* switch_obj = cJSON_GetObjectItem(dps, SWITCH_DP_ID_KEY);
if (cJSON_IsTrue(switch_obj)) {
hardware_switch_set(true);
} else if (cJSON_IsFalse(switch_obj)) {
hardware_switch_set(false);
}
/* relese cJSON DPS object */
cJSON_Delete(dps);
/* Report the received data to synchronize the switch status. */
tuya_iot_dp_report_json(client, json_dps);
}
/* Tuya SDK event callback */
static void user_event_handler_on(tuya_iot_client_t* client, tuya_event_msg_t* event)
{
switch(event->id){
case TUYA_EVENT_DP_RECEIVE:
tuya_iot_dp_download(client, (const char*)event->data);
break;
case TUYA_EVENT_WAIT_ACTIVATE:
/* Print the QRCode for Tuya APP bind */
example_qrcode_print(client->productkey, client->uuid);
break;
case TUYA_EVENT_MQTT_CONNECTED:
TY_LOGI("Device MQTT Connected!");
break;
default:
break;
}
}
int main(int argc, char **argv)
{
int ret = OPRT_OK;
/* Initialize Tuya device configuration */
ret = tuya_iot_init(&client, &(const tuya_iot_config_t){
.productkey = TUYA_PRODUCT_KEY,
.uuid = TUYA_DEVICE_UUID,
.authkey = TUYA_DEVICE_AUTHKEY,
.event_handler = user_event_handler_on
});
assert(ret == OPRT_OK);
/* Start tuya iot task */
tuya_iot_start(&client);
for(;;) {
/* Loop to receive packets, and handles client keepalive */
tuya_iot_yield(&client);
}
return ret;
}
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback