AP Pairing

Last Updated on : 2023-12-19 08:36:57download

Pairing is a process where a device capable of network provisioning, such as a mobile phone, connects an unpaired device to the network.

Feature description

How it works:

  1. An unpaired device acts as an access point (AP) that allows nearby devices to connect to it.
  2. A device capable of network provisioning connects to this AP and then sends the SSID and password of the router and the token to the unpaired device through Tuya’s AP pairing protocol.
  3. The unpaired device connects to the specified wireless network with the received information and then connects to the cloud for activation.

Pairing process

AppDeviceRouterListen for device UDP advertising.UDP advertisingConnect to the device's access point.EstablishTLS or UDP connection.Send pairing data.Decrypt the data.Connect to the router's Wi-Fi.Connect to the cloud for activation and binding.AppDeviceRouter

Development guide

Reference the header

  • tuya_iot_wifi_api.h

How to

To use AP pairing, you need to enable the pairing mode that supports AP mode on device initialization.

Pairing mode Description
WF_START_AP_ONLY Only AP mode is supported.
WF_START_SMART_ONLY Only EZ (Wi-Fi Easy Connect) mode is supported.
WF_START_AP_FIRST AP mode or EZ mode is supported, defaulting to AP mode. After the device is reset, it changes to EZ mode.
WF_START_SMART_FIRST AP mode or EZ mode is supported, defaulting to EZ mode. After the device is reset, it changes to AP mode.
WF_START_SMART_AP_CONCURRENT AP mode and EZ mode coexist. This is a universal pairing mode and allows you to enable support for Bluetooth pairing, Pegasus pairing, and FFS pairing as needed.

API description

Set AP information

The name of the AP defaults to SmartLife-xxxx where xxxx represents the last four digits of the MAC address. You can call this interface to customize the name of the AP.

This interface must be called before device initialization.

/**
 * @brief tuya_iot_set_user_def_ap_if
 *
 * @param[in] ssid: user defined ssid
 * @param[in] passwd: user defined passwd
 *
 * @return OPERATE_RET
 *
 * @note need call before tuya_iot_wf_xxx_init
 */
OPERATE_RET tuya_iot_set_user_def_ap_if(IN CONST CHAR_T *ssid, IN CONST CHAR_T *passwd);

Get AP information

Request the SSID and password of the AP.

/**
 * @brief tuya_iot_get_dev_ap_if
 *
 * @param[in] ssid: the ssid of ap
 * @param[in] passwd: the passwd of ap
 *
 * @return OPERATE_RET
 *
 */
OPERATE_RET tuya_iot_get_dev_ap_if(OUT CHAR_T *ssid, OUT CHAR_T *passwd);

Example

int main(int argc, char *argv[])
{
    //Set the name of the access point (AP). If a custom name is not needed, calling this API is not required.
    TUYA_CALL_ERR_RETURN(tuya_iot_set_user_def_ap_if("test_ap", "test_passwd"));

    TY_IOT_CBS_S wf_cbs = {
        .gw_status_cb = status_changed_cb,
        .gw_ug_cb = (GW_UG_INFORM_CB)gw_ug_inform_cb,
        .gw_reset_cb = vResetCB,
        .dev_obj_dp_cb = dev_obj_dp_cb,
        .dev_raw_dp_cb = dev_raw_dp_cb,
        .dev_dp_query_cb = dev_dp_query_cb,
        .dev_ug_cb = (DEV_UG_INFORM_CB)dev_ug_inform_cb,
    };
    //You can specify the start_mode as any of the pairing modes that enable startup in AP mode, such as WF_START_SMART_AP_CONCURRENT
    TUYA_CALL_ERR_RETURN(tuya_iot_wf_mcu_dev_init(GWCM_OLD, WF_START_SMART_AP_CONCURRENT, &wf_cbs, tuya_get_user_firmware_key(), "product_key", tuya_get_user_sw_ver(), "1.1.0"));
}

FAQs

Why does the device fail to decrypt the received pairing data?

The network segments vary depending on encryption methods. There are two network segments: 192.168.175.1 and 192.168.176.1. Be sure the SDK sets the network segment for AP pairing in the development environment.

Why does the mobile app fail to connect to the AP?

  1. The development framework allows only one client to be connected to an AP. Check if the AP has been connected to a client.
  2. For some Android versions, there might be a compatibility issue where the AP is forced to be offline due to congestion when the mobile phone is connected to this AP.

What is the interval between broadcast packets? What is the broadcast address?

When the device enters the AP pairing mode, it regularly broadcasts UDP packets. The packets are broadcast to 255.255.255.255 at an interval of one second.

What is the stack size for AP pairing?

It is about 3.5 KB.

Can AP pairing coexist with other pairing modes?

Yes, it can coexist with the pairing modes you need. In universal pairing mode, all pairing modes can coexist, specifically, EZ pairing, AP pairing, Bluetooth pairing, FFS pairing, and Pegasus pairing.