BUG / Workaround : Mesh and smart config

leenowell
Posts: 92
Joined: Tue Jan 15, 2019 1:50 pm

BUG / Workaround : Mesh and smart config

Postby leenowell » Sat Feb 02, 2019 8:13 pm

Hi all

I am struggling to get smart config working with my mesh. I deliberately give it an incorrect said which it detects and sends event "no parent". When I receive this event I start the smart config process as per the example and get the ssid and password that I send it. Once received on SC_STATUS_LINK I call esp_mesh_set_router and it appears to hang (no output). I have tried stopping the smart config before it and it behaves the same. I have also tried switching off mesh self organising once I detect no parent and then back on before calling the set router and this seems to stop the smart config working.

Anyone been able to get this working?

Thanks in advance

Lee.
Last edited by leenowell on Mon Feb 18, 2019 7:53 am, edited 1 time in total.

ESP_yudong
Posts: 68
Joined: Wed May 16, 2018 12:31 pm

Re: Mesh and smart config

Postby ESP_yudong » Mon Feb 11, 2019 10:37 am

Hi leenowell,
We will have a discussion on this API.
Currently, you can try `esp_mesh_set_self_organized(true,true)` after set router, and allow channel switch when set config.

leenowell
Posts: 92
Joined: Tue Jan 15, 2019 1:50 pm

Re: Mesh and smart config

Postby leenowell » Mon Feb 11, 2019 11:37 am

Thanks I have tried this before and unfortunately it doesn't seem to work. In my test I turned self organising off first then did the smart config session and when the new details came back set the router info and then switched it back on.

leenowell
Posts: 92
Joined: Tue Jan 15, 2019 1:50 pm

Re: Mesh and smart config

Postby leenowell » Mon Feb 11, 2019 11:40 am

Thanks I have tried this before and unfortunately it doesn't seem to work. In my test I turned self organising off first then did the smart config session and when the new details came back set the router info and then switched it back on.

leenowell
Posts: 92
Joined: Tue Jan 15, 2019 1:50 pm

Re: BUG / Workaround : Mesh and smart config

Postby leenowell » Mon Feb 18, 2019 8:18 am

Hi,

After a lot of trial and error, I have a workaround but think there is an underlying bug. I am using the smart config example code as a template.

If I call the function to do the smart config in the mesh event handler (when no parent found) either as a function call or by creating a new task, the smart config appears to get the details (fires SC_STATUS_LINK) but calling esp_mesh_set_router doesn't seem to work - there is no activity on serial output. Also, it never fires event SC_STATUS_LINK_OVER. I also tried switching the router config using the wifi calls (surrounded by switching self organising off before it and then back on after it) that are in the example and this fails too sometimes core dumping

My workaround is to create a task in the mesh event handler to start the smart config process and waits for this task to set a bit to say it has got the data. The smart config task then stores the SSID and password in global variables when SC_STATUS_LINK is fired and then stops the smart config process, deletes the task, and sets the bit to say it has done it. On detecting the bit has been set, the mesh event handler reads the global variables and calls esp_mesh_set_router. This seems to work fine but adds unnecessary complexity to the code design. Any ideas why the switching in the smart config event handler as in the example doesn't work with mesh?

Code in the mesh event handler is...

Code: Select all

	EventBits_t uxBits;
	bool bSmartConfigDone = false;

	ESP_LOGI(HOMEIOT_TAG, "OnMeshNoParent: No parent found starting Smart Config");
	xTaskCreate(DoSmartConfig, "DoSmartConfig", 4096, NULL, 3, NULL);

    while (!bSmartConfigDone)
    {
    	// Wait for the Smart Config to set the get data bit to indicate it has got router info
    	uxBits = xEventGroupWaitBits(s_wifi_event_group, SMARTCONFIG_GOT_DATA_BIT, true, false, portMAX_DELAY);
        if(uxBits & SMARTCONFIG_GOT_DATA_BIT)
        {
            ESP_LOGI(HOMEIOT_TAG, "OnMeshNoParent: Received notification Smart Config has set data");

             mesh_router_t newinfo = {0};
            strcpy((char*)newinfo.ssid, sSmartConfigSSID);
            strcpy((char*)newinfo.password,sSmartConfigPassword );
            newinfo.ssid_len = strlen((char*) newinfo.ssid);
            memset(newinfo.bssid, '\0', 6);
            newinfo.allow_router_switch = true;

            ESP_LOGI(HOMEIOT_TAG, "OnMeshNoParent: Setting mesh router info to ssid[%s] password[%s] len[%d]", newinfo.ssid, newinfo.password, newinfo.ssid_len);
            esp_err_t err = esp_mesh_set_router(&newinfo);
            ESP_LOGI(HOMEIOT_TAG, "Finished set router err [%d = %s]", err, esp_err_to_name(err));
            bSmartConfigDone = true;
        }
    }

Smart config code is

Code: Select all

void DoSmartConfig (void *parm)
{
    EventBits_t uxBits;

	ESP_LOGI(HOMEIOT_TAG, "DoSmartConfig: Starting Smart Config");

    ESP_ERROR_CHECK( esp_smartconfig_set_type(SC_TYPE_ESPTOUCH) );
    ESP_ERROR_CHECK( esp_smartconfig_start(cbOnSmartConfigEvent, 1) );
    ESP_LOGI(HOMEIOT_TAG, "DoSmartConfig: Smart Config Started");

    while (1)
    {
        uxBits = xEventGroupWaitBits(s_wifi_event_group, ESPTOUCH_DONE_BIT, true, false, portMAX_DELAY);
        if(uxBits & ESPTOUCH_DONE_BIT)
        {
            ESP_LOGI(HOMEIOT_TAG, "DoSmartConfig: Stopping Smart Config");
            esp_smartconfig_stop();

            // Notify task which initiated the smart config process that we have got the data
            xEventGroupSetBits(s_wifi_event_group, SMARTCONFIG_GOT_DATA_BIT);
            vTaskDelete(NULL);
            ESP_LOGI(HOMEIOT_TAG, "DoSmartConfig: Smart Config task deleted");
        }
    }
}

Code for the event handler

Code: Select all

static void cbOnSmartConfigEvent(smartconfig_status_t status, void *pdata)
{
    switch (status) {
        case SC_STATUS_WAIT:
            ESP_LOGI(HOMEIOT_TAG, "cbOnSmartConfigEvent: SC_STATUS_WAIT");
            break;
        case SC_STATUS_FIND_CHANNEL:
            ESP_LOGI(HOMEIOT_TAG, "cbOnSmartConfigEvent: SC_STATUS_FINDING_CHANNEL");
            break;
        case SC_STATUS_GETTING_SSID_PSWD:
            ESP_LOGI(HOMEIOT_TAG, "cbOnSmartConfigEvent: SC_STATUS_GETTING_SSID_PSWD");
            break;
        case SC_STATUS_LINK:
            ESP_LOGI(HOMEIOT_TAG, "cbOnSmartConfigEvent: SC_STATUS_LINK");
            wifi_config_t *wifi_config = pdata;

            // Store the SSID and Password in the global variables to pass to the Mesh thread
            strcpy(sSmartConfigSSID, (char*)wifi_config->sta.ssid);
            strcpy(sSmartConfigPassword,(char*)wifi_config->sta.password );

            // Notify function that created the Smart config task that we are done
            // Switching router config is done in the mesh task as doing it here fails.
            ESP_LOGI(HOMEIOT_TAG, "cbOnSmartConfigEvent: Notifying Smart Config has data");
            xEventGroupSetBits(s_wifi_event_group, ESPTOUCH_DONE_BIT);

            break;
        case SC_STATUS_LINK_OVER: // This will never be executed as connection done in mesh thread after this is killed.
            ESP_LOGI(HOMEIOT_TAG, "cbOnSmartConfigEvent: SC_STATUS_LINK_OVER");
            xEventGroupSetBits(s_wifi_event_group, ESPTOUCH_DONE_BIT);
          break;
        default:
            break;
    }
}


ESP_yudong
Posts: 68
Joined: Wed May 16, 2018 12:31 pm

Re: BUG / Workaround : Mesh and smart config

Postby ESP_yudong » Thu Feb 21, 2019 1:20 pm

Hi lee,

Mesh event cb is belong to mesh_nwk_task, it should be non-block .

I'll try to reproduce your issue.

leenowell
Posts: 92
Joined: Tue Jan 15, 2019 1:50 pm

Re: BUG / Workaround : Mesh and smart config

Postby leenowell » Thu Feb 21, 2019 2:07 pm

Thanks. Is the smart config on the WiFi task by any chance? I wonder if this issue is related to my issue with espnow and mesh interoperability. Maybe the mesh task and the WiFi tasks don't interoperate correctly?

ESP_yudong
Posts: 68
Joined: Wed May 16, 2018 12:31 pm

Re: BUG / Workaround : Mesh and smart config

Postby ESP_yudong » Tue Mar 19, 2019 7:53 am

Our colleagues will provide an example to use smart config later...

karunt
Posts: 93
Joined: Sat Apr 03, 2021 7:58 am

Re: BUG / Workaround : Mesh and smart config

Postby karunt » Fri May 13, 2022 9:01 am

Any update on using smart config with wifi mesh? Are there APIs that allow for that or do we still need to implement work arounds?

Who is online

Users browsing this forum: No registered users and 65 guests