Demo Introduction

Last Updated on : 2024-02-27 02:59:49download

This topic describes the demo in the SDK in terms of device pairing, control, and removal as well as how to configure them to your needs.

Pins on the module

tuyaos_demo_thread_light

Pins on the module Purpose Description
pin1 KEY0 (active low) Press to report the contact sensor status. Press and hold to reset the device.
pin2 KEY1 (active low) Press to report the contact sensor status. Press and hold to reset the device.
pin6 Serial port RX for logging The baud rate is 921600.
pin7 Serial port TX for logging The baud rate is 921600.
pin15 Serial port TX for testing The baud rate is 115200.
pin16 Serial port RX for testing The baud rate is 115200.
pin20 LED1 (active high) Device control indicator.
pin21 LED0 (active high) Network status indicator.

tuyaos_demo_thread_door_sensor

Pins on the module Purpose Description (door_sensor_demo)
pin1 KEY0 (active low) Press to report the contact sensor status. Press and hold to reset the device.
pin2 KEY1 (active low) Press to report the contact sensor status. Press and hold to reset the device.
pin6 Serial port RX for logging The baud rate is 921600.
pin7 Serial port TX for logging The baud rate is 921600.
pin15 Serial port TX for testing The baud rate is 115200.
pin16 Serial port RX for testing The baud rate is 115200.
pin20 LED1 (active high) Not used
pin21 LED0 (active high) Network status indicator.

tuyaos_demo_thread_switch

Pins on the module Purpose Description (switch_demo)
pin1 KEY0 (active low) Press to report the contact sensor status. Press and hold to reset the device.
pin2 KEY1 (active low) Press to report the contact sensor status. Press and hold to reset the device.
pin6 Serial port RX for logging The baud rate is 921600.
pin7 Serial port TX for logging The baud rate is 921600.
pin15 Serial port TX for testing The baud rate is 115200.
pin16 Serial port RX for testing The baud rate is 115200.
pin19 LED2 (active high) Device control indicator.
pin20 LED1 (active high) Device control indicator.
pin21 LED0 (active high) Network status indicator.

How to use the demo

  1. Build the project. Navigate to tuyaos_demo_thread_light, tuyaos_demo_thread_door_sensor, or tuyaos_demo_thread_switch. Right-click the project and choose Build Project. See Quick Start for details.

  2. Perform firmware flashing and authorization. Create a product, upload the firmware, generate a token, and then perform firmware flashing and authorization. You can also use Silicon Labs’ flashing tool for firmware flashing without authorization, but it is limited to debugging purposes.

  3. Power on the device. If the firmware comes with logging, logs should be generated when the device starts up after flashing. Connect the device to your computer based on the above pin definitions. Locate the QR code for pairing in the initialization log, then copy and open the link in a browser.

  4. Pair the device. The demo defaults to power-on pairing, with 3-minute Bluetooth advertising initiated. The network indicator blinks slowly. Using the Tuya Smart app as an example, open the gateway panel, tap Add Thread Device, scan the QR code shown in the previous step, and finish pairing.

    To pair a Thread device, you need a gateway. Prepare a Tuya-enabled gateway to pair the device with the Tuya Smart app. If you are using the Apple Home app or Google Home, have their respective gateways ready.

  5. Control the device.

    • tuyaos_demo_thread_light: Press KEY0 to turn on/off LED1.

    • tuyaos_demo_thread_door_sensor: Press the buttons to simulate closing and opening a contact sensor. The mobile app will present the sensor status accordingly.

    • tuyaos_demo_thread_switch: This demo features a 2-gang switch. Press KEY0 and KEY1 to control switch 1 and switch 2 respectively, with the status indicated by LED1 and LED2.

  6. Remove a device. After removal, you need to pair the device again before using it.

    • Remove the device from the mobile app. Since the contact sensor is low power, tap Remove on the app and then press the button to wake up the device and sync the removal command. The device will be removed from the mobile app and enter pairing mode.
    • Press and hold either button for five seconds to remove the device locally. The device will enter pairing mode and display as offline on the mobile app.

General configurations

See app_callback.cpp in the demo for configuration.

Key

// Initialize the keys to configure parameters such as button number, pin, active level, press and hold timeout, and callbacks.

ty_key_init(KEY1_INDEX,KEY1_GPIO_PIN_INDEX,KEY_LOW_LEVEL,KEY_LONG_PRESS_TIMEOUT,key_event_callback);
ty_key_init(KEY2_INDEX,KEY2_GPIO_PIN_INDEX,KEY_LOW_LEVEL,KEY_LONG_PRESS_TIMEOUT,key_event_callback);

LED

// Initialize the light to configure parameters such as the light number, pin, active level, and initial level.

ty_led_init(NETWORK_LED_INDEX,NETWORK_GPIO_PIN_INDEX,TUYA_GPIO_LEVEL_LOW,LED_HIGH_LEVEL);
ty_led_init(LIGHT1_LED_INDEX,LIGHT1_GPIO_PIN_INDEX,TUYA_GPIO_LEVEL_LOW,LED_HIGH_LEVEL);

Pairing

// Specify whether to enable power-on pairing and set the pairing window time.

network_data.power_on_start_pairing = TRUE;
network_data.network_pairing_timeout = 3 * 60;

Node type

// Initialize network node types, used with the corresponding library. It is recommended not to make any changes.

network_data.node_type = ROUTER;
network_data.node_type = SLEEP_END_DEVICE;

Data model

// Configure device types and application data models.
// It is recommended not to make changes. To configure other device types, select the correct cluster and add it to the list by following the Matter cluster specifications.

uint16_t ep_device_type[]={EXTENDED_COLOR_LIGHT_DEVICE_TYPE};

MATTER_CLUSTER_S ep_server_cluster_list[] = {
    CLUSTER_IDENTIFY_INFO,
    CLUSTER_GROUPS_INFO,
    CLUSTER_SCENES_INFO,
    CLUSTER_ON_OFF_INFO,
    CLUSTER_LEVEL_CONTROL_INFO,
    CLUSTER_DESCRIPTOR_INFO,
    CLUSTER_COLOR_CONTROL_INFO };

MATTER_ENDPOINT_S app_ep_info[] = {
    { LIGHT1_ENDPOINT, { get_array_len(ep_device_type), &ep_device_type[0] }, ep_server_cluster_list, get_array_len(ep_server_cluster_list), NULL, 0 },
};

Network leaving

// Remove a device from the network.

tal_matter_reset_factory_new();

Low power

Poll interval

// The poll interval in active state.
   network_data.sleep_end_device.active_interval =200;

// The RF wake-up interval in sleep mode.
   network_data.sleep_end_device.idle_interval = 5000;

Heartbeat

// Heartbeat reporting time. The device reports the battery level to the gateway to indicate the connection works fine.

   tal_sw_timer_start(heartbeat_timer, 2*60*60000, TAL_TIMER_CYCLE);

Light demo

Light control

// Call this API to control the light, where "ep" indicates the target light, and "new_val" indicates the desired state.

VOID_T update_dev_attr_state(UINT8_T ep, UINT8_T new_val)
{
    OPERATE_RET ret = OPRT_OK;
    ret = tal_matter_attribute_write(ep, ZCL_ON_OFF_CLUSTER_ID, ZCL_ON_OFF_ATTRIBUTE_ID, (UINT8_T *) &new_val, ZCL_BOOLEAN_ATTRIBUTE_TYPE);
    if (ret != OPRT_OK)
    {
        TUYA_LOG("ERR: updating on/off %x", ret);
    }
}

Status callback

// This callback indicates the current state of the light. Change the light's state based on the status value.

VOID_T attribute_change_callback(UINT16_T endpoint,
                                 UINT32_T cluster_id,
                                 UINT32_T attr_id,
                                 UINT8_T attr_type,
                                 UINT16_T len,
                                 UINT8_T *raw_buffer)

Contact sensor demo

Sensor control

// Report the open and close status of the contact sensor to the mobile app.

   VOID_T dev_attr_state_report_boolean(BOOL_T new_val){
    UINT8_T value_t = new_val?0x01:0x00;
    tal_matter_attribute_write(1,ZCL_BOOLEAN_STATE_CLUSTER_ID,ZCL_STATE_VALUE_ATTRIBUTE_ID,
                                        (uint8_t *)&value_t,ZCL_BOOLEAN_ATTRIBUTE_TYPE);
}

Battery level reporting

// Report battery level to the mobile app.

   VOID_T dev_attr_state_report_power(UINT8_T new_val){
   UINT8_T value_t = new_val;

   tal_matter_attribute_write(0,ZCL_POWER_SOURCE_CLUSTER_ID,ZCL_POWER_SOURCE_BAT_PERCENT_REMAINING_ATTRIBUTE_ID,
                                     (UINT8_T *)&value_t,ZCL_INT8U_ATTRIBUTE_TYPE);
}

Switch demo

Switch control

// This indicates the action triggered when the switch is activated. In the demo, light on/off acts as the indicator. You can configure the indicator as needed.

VOID_T update_dev_attr_state(UINT8_T ep, UINT8_T new_val)
{
    OPERATE_RET ret = OPRT_OK;
    ret = tal_matter_attribute_write(ep, ZCL_ON_OFF_CLUSTER_ID, ZCL_ON_OFF_ATTRIBUTE_ID, (UINT8_T *) &new_val, ZCL_BOOLEAN_ATTRIBUTE_TYPE);
    if (ret != OPRT_OK)
    {
        TUYA_LOG("ERR: updating on/off %x", ret);
    }
}

Status callback

// Detect the switch attribute and change the light state based on the status value callback.

VOID_T attribute_change_callback(UINT16_T endpoint,
                                 UINT32_T cluster_id,
                                 UINT32_T attr_id,
                                 UINT8_T attr_type,
                                 UINT16_T len,
                                 UINT8_T *raw_buffer)

Support and help

If you have any problems with TuyaOS development, you can post your questions in the Tuya Developer Forum.