In this demo, we will show you how to implement a TS02N-based touch sensor driver. Based on the Tuya IoT Platform, we use Tuya' Bluetooth LE module, SDK, and the Tuya Smart app to connect the touch sensor to the cloud. The interfaces provided in this demo allow you to set the input pin and the trigger function. Then, register the function to implement your custom setting. The LED is on when the sensor is touched and is off when the sensor is released. The key callback can synchronize the touch key event to the mobile app.
-
Install the integrated development environment (IDE) as per the requirements of the original chip SDK.
-
Find the download URL of the Tuya BLE SDK Demo Project from the following table. Refer to the
README.md
file under each branch to import the project.Chip platforms Model Download URL Nordic nrf52832 tuya_ble_sdk_Demo_Project_nrf52832.git Realtek RTL8762C tuya_ble_sdk_Demo_Project_rtl8762c.git Telink TLSR825x tuya_ble_sdk_Demo_Project_tlsr8253.git Silicon Labs BG21 Coming soon. Beken BK3431Q Tuya_ble_sdk_demo_project_bk3431q.git Beken BK3432 tuya_ble_sdk_Demo_Project_bk3432.git Cypress Psoc63 tuya_ble_sdk_Demo_Project_PSoC63.git
-
Edit code
-
In
tuya_ble_app_demo.h
, specify the PID of the product you have created on the Tuya IoT Platform.#define APP_PRODUCT_ID "xxxxxxxx"
Change
xxxxxxxx
to the PID. -
In
tuya_ble_app_demo.h
, specify theauthkey
andUUID
.static const char auth_key_test[] = "yyyyyyyy"; static const char device_id_test[] = "zzzzzzzz";
Change
yyyyyyyy
to theauthkey
andzzzzzzzz
to theUUID
.
-
-
Compile code
Compile the edited code, download the code to the hardware, and run it. You may need to download the stack and bootloader depending on your chip models. Check the logs and use the third-party Bluetooth debugging app (such as LightBlue for iOS) to verify the Bluetooth broadcast.
├── src /* Source code files*/
| ├── sdk
| | └── tuya_uart_common_handler.c /* Code for UART communication */
| ├── driver
| | ├── tuya_app_driver_ts02n_key.c /* Touch key driver */
| | └── tuya_app_driver_led.c /* LED driver */
| ├── tuya_ble_app_demo.c /* Entry file of application layer */
| └── tuya_app_ts02n_key.c /* Application code for TS02N touch keys */
|
└── include /* Header files */
├── sdk
| ├── custom_app_uart_common_handler.h /* Code for UART communication */
| ├── custom_app_product_test.h /* Implementation of custom production test items */
| └── custom_tuya_ble_config.h /* Application configuration file */
├── driver
| ├── tuya_app_driver_ts02n_key.h /* Touch key driver */
| └── tuya_app_driver_led.h /* LED driver */
├── tuya_ble_app_demo.h /* Entry file of application layer */
└── tuya_app_ts02n_key.h /* Application code for TS02N touch keys */
Entry file: /tuya_ble_app/tuya_ble_app_demo.c
void tuya_ble_app_init(void)
is executed to initialize Tuya IoTOS Embedded Bluetooth LE SDK. This function is executed only once.void app_exe()
is used to execute the application code. It is executed in a loop.
Function name | tuya_ble_dp_data_report |
---|---|
Function prototype | tuya_ble_status_t tuya_ble_dp_data_report(uint8_t *p_data,uint32_t len); |
Feature overview | Reports DP data. |
Parameter | p_data [in] : DP data. len[in] : data length. It cannot exceed TUYA_BLE_REPORT_MAX_DP_DATA_LEN . |
Return value | TUYA_BLE_SUCCESS : sent successfully. TUYA_BLE_ERR_INVALID_PARAM : invalid parameter. TUYA_BLE_ERR_INVALID_STATE : failed to send data due to the current Bluetooth connection, such as Bluetooth disconnected. TUYA_BLE_ERR_NO_MEM : failed to request memory resources. TUYA_BLE_ERR_INVALID_LENGTH : data length error. TUYA_BLE_ERR_NO_EVENT : other errors. |
Remarks | application calls this function to send DP data to the mobile app. |
Parameter description:
The Tuya IoT Platform manages data through DPs. The data generated by any device is abstracted into a DP. DP data consists of four parts, as described below.
-
Dp_id
: the DP ID of a data point defined on the Tuya IoT platform. It is one byte. -
Dp_type
: the data type. It is one byte.-
#define DT_RAW 0
: raw type. -
#define DT_BOOL 1
: Boolean type. -
#define DT_VALUE 2
: value type. The value range is specified when a DP of value type is created on the Tuya IoT Platform. -
#define DT_STRING 3
: string type. -
#define DT_ENUM 4
: enum type. -
#define DT_BITMAP 5
: bitmap type.
-
-
Dp_len
: It can be one byte or two bytes. Currently, Bluetooth only supports one byte, so the data of a single DP can be up to 255 bytes. -
Dp_data
: the DP data, withdp_len
byte(s).
The data that the parameter p_data
points to must be packaged in the following format for reporting.
DP 1 data | – | DP n data | ||||||
---|---|---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | – | n | n+1 | n+2 | n+3 |
Dp_id | Dp_type | Dp_len | Dp_data | – | Dp_id | Dp_type | Dp_len | Dp_data |
When this function is called, the maximum data length of is TUYA_BLE_REPORT_MAX_DP_DATA_LEN
, which is 255+3 currently.
key1_pin | key2_pin | led1_pin | led2_pin |
---|---|---|---|
P8 | P7 | P26 | P24 |
You can get support from Tuya with the following methods: