(solved) re-starting wifi

User avatar
mzimmers
Posts: 503
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

(solved) re-starting wifi

Postby mzimmers » Mon Jul 27, 2020 11:58 pm

Hi all -
(ESP32, idf v4.0.1)

I'm working on a device that falls back to battery power when it loses line power. When on battery power, it stops Wifi (to save the battery). When an event (like a button push) occurs, it attempts to re-start Wifi. But I get the following error:

Code: Select all

esp_wifi_connect() returned error 0x3002.
Which translates to wifi not started. But, it *is* started, at least according to my event handler:

Code: Select all

        case WIFI_EVENT_STA_START:
            // step 3.3 in https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/wifi.html
            ESP_LOGI(TAG, "event_handler(): Wifi started at %s.", currentTime.c_str());
            tasks->wifi->setCommsState(WIFI_STATE_STARTED);
            err = esp_wifi_connect();
            if (err == ESP_OK)
            {
                ESP_LOGI(TAG, "event_handler(): esp_wifi_connect() returned successfully.");
            }
            else
            {
                ESP_LOGW(TAG, "event_handler(): esp_wifi_connect() returned error 0x%x.", err);
            }
            break;

Code: Select all

I (843862) Wifi: event_handler(): Wifi started at Mon Jul 27 16:52:29 2020.
I (843862) Wifi: setCommsState(): changing state to Started.
W (843862) Wifi: event_handler(): esp_wifi_connect() returned error 0x3002.
Am I missing a step in the restart process? I thought I could proceed directly to a connect when started.

Thanks...
Last edited by mzimmers on Mon Aug 03, 2020 4:23 pm, edited 1 time in total.

istokm
Posts: 14
Joined: Thu Jun 25, 2020 12:11 pm

Re: re-starting wifi

Postby istokm » Tue Jul 28, 2020 7:26 am

Strange, that should indeed work. I've got a few ideas that you can look into:
- by chance, is the switch falling through when it shouldn't?
- do you have error checks on the other wifi functions? Just so we are sure there wasn't an error earlier in the chain.
- how did you stop the wifi? just a 'esp_wifi_stop()' call? If so, when did you start it again?

If all of that is as it should be, maybe enable debug logging and see what the wifi driver is actually doing at that moment, I've also come across a event that got fired when it shouldn't: viewtopic.php?f=13&t=16271

User avatar
mzimmers
Posts: 503
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: re-starting wifi

Postby mzimmers » Tue Jul 28, 2020 2:22 pm

- by chance, is the switch falling through when it shouldn't?
Highly doubtful.
- do you have error checks on the other wifi functions? Just so we are sure there wasn't an error earlier in the chain.
I do, but I'm wondering what you mean by "earlier in the chain." This is a restart, so I've already called all the init functions. I don't need to call any of them again after a restart, do I?
- how did you stop the wifi? just a 'esp_wifi_stop()' call? If so, when did you start it again?
First a esp_wifi_disconnect(), followed immediately by a esp_wifi_stop(). Time of restart isn't fixed, but it's after I see the message that Wifi has stopped.

Thanks...

istokm
Posts: 14
Joined: Thu Jun 25, 2020 12:11 pm

Re: re-starting wifi

Postby istokm » Wed Jul 29, 2020 11:13 am

Highly doubtful.
What I meant is, I just wanted to make sure it's not something as trivial as a forgotten break - not that the compiler screwed up :D
First a esp_wifi_disconnect(), followed immediately by a esp_wifi_stop()
I personally preffer only calling esp_wifi_stop (as it does both) but it shouldn't cause an issue if they are called synchronously.

Also if you can, try calling esp_wifi_connect sequentially after esp_wifi_start (outside of the start event), as I have a feeling the wifi event loop might be a bit bugged - but can't tell you for sure without them releasing the source.

I have it implemented as such:

Code: Select all

esp_err_t err;

err = esp_wifi_start();
if (err != ESP_OK) {
	ESP_LOGE(TAG, "[%s] esp_wifi_start(): %d %s",
   		__func__, err, esp_err_to_name(err));
	return ERR_INTERNAL;
}

err = esp_wifi_connect();
if (err != ESP_OK) {
	ESP_LOGE(TAG, "[%s] esp_wifi_connect(): %d %s",
		__func__, err, esp_err_to_name(err));
	return ERR_INTERNAL;
}
Last edited by istokm on Thu Jul 30, 2020 6:22 am, edited 1 time in total.

User avatar
mzimmers
Posts: 503
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: re-starting wifi

Postby mzimmers » Wed Jul 29, 2020 4:17 pm

Thanks for the suggestions. I do in fact, call esp_wifi_connect() after esp_wifi_start(); it happens on a start event.

I added some code that stops and re-starts wifi if the connect fails; this seems to be working.

Code: Select all

        case WIFI_EVENT_STA_START:
            // step 3.3 in https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/wifi.html
            ESP_LOGI(TAG, "event_handler(): Wifi started at %s.", currentTime.c_str());
            tasks->wifi->setCommsState(WIFI_STATE_STARTED);
            err = esp_wifi_connect();
            if (err == ESP_OK)
            {
                ESP_LOGI(TAG, "event_handler(): esp_wifi_connect() returned successfully.");
            }
            else
            {
                tasks->wifi->setCommsState(WIFI_STATE_DISCONNECTED);
                ESP_LOGW(TAG, "event_handler(): esp_wifi_connect() returned error 0x%x.", err);
                esp_wifi_stop(); // this is the new code I added.
                esp_wifi_start(); // this is the new code I added.
            }
            break;
I may report back with some additional questions about wake-up, but this particular problem seems to be fixed.

Who is online

Users browsing this forum: iotdeveloperfw and 42 guests