This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC3220S-LAUNCHXL: STA mode automatic re-connect

Part Number: CC3220S-LAUNCHXL
Other Parts Discussed in Thread: CC3200

Dear Forum,

My device is operating in STA mode and is successfully connected to the network. All of a sudden the router turns off and then after some time the router turns back on. During the off period the cc3220 loses connection and when the router is back on the cc3220 doesn't reconnect automatically. What methods exist to make the cc3220 try reconnecting to router automatically?

I tried putting the following code in SimpleLinkWlanEventHandler function:

void SimpleLinkWlanEventHandler(SlWlanEvent_t *pArgs)
{
    _i16 status;

    switch (pArgs->Id) {
        case SL_WLAN_EVENT_CONNECT:
            deviceConnected = true;
            break;

        case SL_WLAN_EVENT_DISCONNECT:
            deviceConnected = false;
            UART_PRINT("[SimpleLinkWlanEventHandler] WLAN has disconnected!!!\n\r");

            // check if there are any stored profiles, if there are no stored profiles then switch to AP mode but if there are stored profiles then try re-connecting periodically
            status = checkProfiles();
            if (status < 0)
            {
                status = setAPmode();
                UART_PRINT("[SimpleLinkWlanEventHandler] Set AP mode status is %d\n\r", status);
            }
            else if (status >= 0)
                while (deviceConnected != true)
                {

                    // Set auto connect policy
                    status = sl_WlanPolicySet(SL_WLAN_POLICY_CONNECTION, SL_WLAN_CONNECTION_POLICY(1, 0, 0, 0), NULL, 0);
                    if (status < 0) {
                        UART_PRINT("[SimpleLinkWlanEventHandler] Error %d Failed to set connection policy to auto connect\n\r", status);
                        while (1);
                    }

                    // Enable DHCP client
                    status = sl_NetCfgSet(SL_NETCFG_IPV4_STA_ADDR_MODE, SL_NETCFG_ADDR_DHCP, 0, 0);
                    if (status < 0) {
                        UART_PRINT("[SimpleLinkWlanEventHandler] Error %d Could not enable DHCP client\n\r", status);
                        while (1);
                    }

                    // Restart the network processor
                    sl_Stop(200);

                    status = sl_Start(0, 0, 0);
                    UART_PRINT("[SimpleLinkWlanEventHandler] sl_Start code is %d\n\r", status);

                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                    usleep(500000);
                }
            break;

        default:
            break;
    }
}

When the router turns off the device periodically re-starts in STA mode and my assumption is that it should try connecting to the stored profile. But that doesn't seem to happen. Can you advise?

Thanks,

David

  • Hi David,

    How are you performing the initial connection to the AP? If you are just using sl_WlanConnect(), that only performs a one-time connection that will not save persistent connection data onto the CC3220.

    The correct API to use would be sl_WlanProfileAdd(). In addition to adding the Wlan profile persistently, you also set the wlan auto connect settings of the CC3220 with the sl_WlanPolicySet() API.

    There is this code for the CC3200 that outlines the flow that you could follow as a way to set that persistently:
    e2e.ti.com/.../1380220

    The APIs are slightly different between the CC3200 and the CC3220, but the flow is the same. If you set the profile and the wlan policy, the CC3220 should automatically reconnect once the AP is brought back online. There is no need for the code you have in the SimpleLinkWlanEventHandler. In fact, performing all those usleep() calls within the interrupt handler is not good practice, and you should also avoid calling any sl_*() API calls from within an interrupt context.

    Let me know if you still run into this issue after setting the profile and wlan policy, or if you have any further questions.

    Regards,
    Michael
  • Hi Michael,

    Initially I put the device in AP mode, then open the local page where I have a simple form of sending credentials. When user submits the form I do http post to /api/1/wlan/profile_add as described in Section 8.4.4 of Programmer's Guide. I also send the credentials by regular http post and record those in the device under "ap_username_in" and "ap_password_in" variables. Then I use sl_WlanProfileAdd(). Below is my usage:

    SecParams.Type = SL_WLAN_SEC_TYPE_WPA_WPA2;
    SecParams.Key = (signed char *)ap_password_in;
    SecParams.KeyLen = strlen((const char *)SecParams.Key);
    index = sl_WlanProfileAdd((signed char*)ap_username_in, strlen((const char*)ap_username_in), MacAddr, &SecParams, NULL, 7, 0);
    UART_PRINT("[Link Local Task] STA profile added with status %d \n\r", index);

    The profile is being added successfully and the device is able to connect when it is reset. The problem is only when the connection is suddenly interrupted on router side. In that case the device doesn't re-connect. I dp policy settings when device starts:

    // Set auto connect policy
    status = sl_WlanPolicySet(SL_WLAN_POLICY_CONNECTION, SL_WLAN_CONNECTION_POLICY(1, 0, 0, 0), NULL, 0);
    if (status < 0) {
    UART_PRINT("[setSTAmode] Error %d Failed to set connection policy to auto connect\n\r", status);
    while (1);
    }

    // Enable DHCP client
    status = sl_NetCfgSet(SL_NETCFG_IPV4_STA_ADDR_MODE, SL_NETCFG_ADDR_DHCP, 0, 0);
    if (status < 0) {
    UART_PRINT("[setSTAmode] Error %d Could not enable DHCP client\n\r", status);
    while (1);
    }

    I double check the existence of the profile in SimpleLinkWlanEventHandler as I posted. It truly is not the best practice what I have in the handler, thanks for advice.

    Can you help understand why the re-connection doesn't happen if the profile exists? Maybe there is some sort of timeout? Where are the settings for the re-connection timeout located, I wasn't able to find?

    Thanks,
    David
  • Hi David,

    Looking through your code, the main thing that jumps out is how you pass in MacAddr in the sl_WlanProfileAdd() call. The MacAddr is the BSSID of the AP, which allows you to specify a specific AP to connect to in case there are multiple APs broadcasting on the same SSID in the area and you wanted to connect to a specific one.

    What happens if you the same thing but pass in 0 instead of MacAddr?

    Regards,
    Michael
  • Hi Michael,

    I used the following for MacAddr:
    _u8 MacAddr[] = {0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};

    In my testing the router SSID name is unique.

    Do you think the above could cause the problem with re-connection?

    Thanks,
    David
  • Hi David,

    If the MAC address provided does not match the BSSID of your AP, your profile shouldn't be able to connect. I'm not sure how your device managed to connect in the first place if you had a wrong BSSID. Please change MacAddr to 0 in your sl_WlanProfileAdd() call and see if that helps.

    Regards,
    Michael
  • Hi Michael,

    Thank you for your help. I tried your suggestion and WLAN connection works. But it looks like my problem has been completely different. And here it goes..

    FYI, I am using shadow_sample application as base. On the top of the example I basically added link local features to be able to pass the SSID credentials from a mobile phone to the CC3220 device. Again, the re-connection worked perfectly when I unplugged the CC3220 device and then plugged it back in again. CC3220 was able to load the profile and connect to my network. The problem occurred when I suddenly re-started the router. Here's a log depicting the process - green section is for the period when the device was connected, red section is for the period when the router was re-started:


    ==========================================================

    On Device: window state false
    Update Shadow: {"state":{"reported":{"temperature":25.500000,"windowOpen":false}}, "clientToken":"mydevice_unique-0"}

    ==========================================================

    Update Accepted !!
    Delta - Window state changed to 1

    ==========================================================

    On Device: window state true
    Update Shadow: {"state":{"reported":{"temperature":26.000000,"windowOpen":true}}, "clientToken":"mydevice_unique-1"}

    ==========================================================

    Update Accepted !!

    ==========================================================

    On Device: window state true
    Update Shadow: {"state":{"reported":{"temperature":26.500000,"windowOpen":true}}, "clientToken":"mydevice_unique-2"}

    ==========================================================

    Update Accepted !!

    ==========================================================

    On Device: window state true
    Update Shadow: {"state":{"reported":{"temperature":27.000000,"windowOpen":true}}, "clientToken":"mydevice_unique-3"}

    ==========================================================

    Update Accepted !!

    ==========================================================

    On Device: window state true
    Update Shadow: {"state":{"reported":{"temperature":27.500000,"windowOpen":true}}, "clientToken":"mydevice_unique-4"}

    ==========================================================

    Update Accepted !!

    ==========================================================

    On Device: window state true
    Update Shadow: {"state":{"reported":{"temperature":28.000000,"windowOpen":true}}, "clientToken":"mydevice_unique-5"}

    ==========================================================


    ==========================================================

    On Device: window state true
    Update Shadow: {"state":{"reported":{"temperature":28.500000,"windowOpen":true}}, "clientToken":"mydevice_unique-6"}

    ==========================================================


    ==========================================================

    On Device: window state true
    Update Shadow: {"state":{"reported":{"temperature":29.000000,"windowOpen":true}}, "clientToken":"mydevice_unique-7"}

    ==========================================================

    Update Timeout--

    ==========================================================

    On Device: window state true
    Update Shadow: {"state":{"reported":{"temperature":29.500000,"windowOpen":true}}, "clientToken":"mydevice_unique-8"}

    ==========================================================

    [SimpleLinkWlanEventHandler] WLAN has disconnected!!!
    Update Timeout--

    ==========================================================

    On Device: window state true
    Update Shadow: {"state":{"reported":{"temperature":30.000000,"windowOpen":true}}, "clientToken":"mydevice_unique-9"}

    ==========================================================

    Update Timeout--

    ==========================================================

    On Device: window state true
    Update Shadow: {"state":{"reported":{"temperature":30.500000,"windowOpen":true}}, "clientToken":"mydevice_unique-10"}

    ==========================================================

    Update Timeout--
    Update Timeout--
    Update Timeout--
    [SimpleLinkWlanEventHandler] WLAN is now connected!!!

    ==========================================================

    On Device: window state true
    Update Shadow: {"state":{"reported":{"temperature":31.000000,"windowOpen":true}}, "clientToken":"mydevice_unique-11"}

    ==========================================================

    ERROR: runAWSClient L#212 An error occurred in the loop -26
    Disconnecting
    ERROR: runAWSClient L#219 Disconnect error -13

    The above message is the last one, nothing happens after this.

    HOWEVER, there is one small green line in the red text that I was missing. It says: [SimpleLinkWlanEventHandler] WLAN is now connected!!!

    So WLAN actually connects when the router turns back on after the restart but the runAWSClient is not able to reconnect. How should I solve this? Restarting the runAWSClient from SimpleLinkWlanEventHandler function is not advised, correct? What would be the recommended method?

    Thanks a lot for your advice!

    David

    ===========================================

  • Hi David,

    I'm not the most familiar with the shadow_sample example, but it looks to me like the AWS connection is not being restarted correctly.

    I have my own project that uses the the AWS plugin + device shadows, and when I detect a disconnect, I perform a aws_iot_shadow_disconnect(&client) and then a aws_iot_shadow_free(&client), before restarting the AWS shadow connection through the usual aws_iot_shadow_init() and then aws_iot_shadow_connect() steps performed in the example.

    Try performing the disconnect and free before attempting to reconnect and see if that helps.

    Regards,
    Michael
  • Hi Michael,

    Thanks, it worked.

    Thanks,
    David