DP Channel Priority

Last Updated on : 2024-06-24 10:16:31download

The TuyaOS framework automatically chooses an appropriate channel for data point (DP) transmission based on the device’s current connection status.

Channel types

Channel Description
LAN The mobile app and the device are connected to the same router, and the three form a LAN.
WAN (MQTT) Both the mobile app and the device can connect to the Tuya IoT cloud, and the device is online through MQTT.
Bluetooth The Bluetooth connection between the mobile app and the device is successful.

Channel priority

Default priority

The priority is LAN > WAN (MQTT) > Bluetooth, and the mobile app will choose the high-priority channel to interact with the device.

  • LAN channel: When the mobile app and the device are on the same LAN, the LAN channel takes precedence.
  • WAN channel: When the mobile app and the device are not on the same LAN, but the device is connected to the Tuya IoT cloud (the device is online through MQTT), the WAN channel takes precedence.
  • Bluetooth channel: The mobile app and the device are not on the same LAN, and the device cannot connect to the Tuya IoT cloud, the Bluetooth channel takes precedence.

Custom priority

Currently, Tuya provides two ways for you to set up the priority of DP channels.

  • Make API calls

    The framework provides APIs for setting up channel priorities. The settings apply to all DPs. That is, once set, the channel of all DP interactions will run in the specified priority.

  • Set up on the Tuya Developer Platform

    You can set up the priority for a single DP to apply it exclusively to this DP, while other DPs still follow the default priority.

How to choose reporting channels

  • When reporting data, the device first determines which channels are properly connected, and only reports data through the properly connected channels.

  • Bluetooth is mutually exclusive with the LAN and WAN channels.

    • If the device reports data on LAN or WAN, it will not report data through the Bluetooth channel.
    • If the device reports data on Bluetooth, it will not report data on the LAN and WAN channels.
  • If both LAN and WAN channels are connected, both channels will be used to report the data. That is to say, the mobile app might receive data reported through multiple channels.

Development guide

Reference the header

  • tuya_iot_com_api.h
  • smart_frame.h

How to

  • Adopt default priority

    After device functionality callbacks are registered during device initialization, call the appropriate reporting API to report DP data. The reporting channel will be automatically processed in the API.

  • Set up channel priority through API calls

    1. Open tuya_iot_config.h and check if the following macro is defined.

      #define ENABLE_COMMUNICATE_PRIORITY 1
      
    2. Before device functionality callbacks are registered during device initialization, call the API to set up the channel priority. After that, DP data reporting and sending will work in the priority you set.

  • Set up on the Tuya Developer Platform

    Create a product on the Tuya Developer Platform and then enter the product development process. On the page of Function Definition, find Standard Functions, click Edit, and set up the DP Routing in Special Configuration.

    If you cannot find DP Routing in Special Configuration, this is because the product solution does not have this function bound. To request binding it with your product, contact your account manager or submit a service ticket.

    Description of DP routing

    Routing type Meaning and scenario
    Not set The DP is reported based on the default channel priority.
    Bluetooth priority The DP is reported from the Bluetooth channel first. When there is no Bluetooth connection, the DP can also be reported through other channels.
    Force Bluetooth The DP is only reported from the Bluetooth channel. It is not reported when a Bluetooth connection is unavailable.

API description

Set up channel priority

You can modify the priority of the channels used to report and send DPs. After setting is finished, the channel priority applies to all DPs. Through API calls, you can modify the default channel priority. The priority setting is stored in the volatile memory and needs to be set every time the device is restarted.

This API shall be called before the device functionality callback is registered during the device initialization process.

/**
 * @brief Set the reporting channel for DP
 *
 * @param[in] chan: array of channel for DP report, refer to TY_DP_REPT_CHAN_TP_T
 * @param[in] cnt: count of array member
 * @param[in] only: report DP only on the first channel
 *
 * @note This API is used for setting the reporting channel for DP. This API should be called before or during device initialization.
 *
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET ty_set_dp_rept_chan(IN UINT8_T chan[], UINT8_T cnt, BOOL_T only);

Example

Set up the channel priority to: Bluetooth LE > LAN > WAN (MQTT).

// Set the DP reporting channel priority as BLE > LAN > WAN (MQTT)
OPERATE_RET example_set_dp_report_chan(VOID)
{
    OPERATE_RET rt = OPRT_OK;
    UCHAR_T chan_para[] = {TY_DP_REPT_CHAN_BLE,TY_DP_REPT_CHAN_LAN,TY_DP_REPT_CHAN_MQTT};
    TUYA_CALL_ERR_RETURN(ty_set_dp_rept_chan(chan_para, CNTSOF(chan_para), TRUE));
    // Device init
    //...
    return rt;
}