How do I stop wifi so that it can be successfully restarted? [SOLVED]

adamwilt
Posts: 13
Joined: Sun Dec 06, 2020 1:54 am

How do I stop wifi so that it can be successfully restarted? [SOLVED]

Postby adamwilt » Sun Dec 20, 2020 1:52 am

I'm trying to build wifi AP and STA modes that I can start and stop at will.

I call esp_netif_init() once, at startup.

Then I start up wifi using code similar to this simplified pseudocode:

Code: Select all

    _espNetif = esp_netif_create_default_wifi_[ap|station]();
    esp_wifi_init();
    esp_wifi_set_mode();
    esp_wifi_set_config();
    esp_wifi_start();
And to stop wifi, I do this:

Code: Select all

    esp_wifi_stop();
    esp_wifi_deinit();
    esp_netif_destroy(_espNetif);

It works great the first time, but subsequent starts fail as soon as any wifi traffic occurs. In the code as shown, it's a crash:

Code: Select all

Guru Meditation Error: Core  0 panic'ed (InstructionFetchError). Exception was unhandled.
If I reduce the amount of teardown/rebuild — for example, if I just call esp_wifi_stop() to stop wifi and call esp_wifi_start() to restart it — the code won't crash, but it will hang as soon as wifi traffic happens, and in hangs in the same place (as far as I can tell) that it crashes in the full teardown/rebuild code.

So what am I missing?

Solution: Add a call to esp_wifi_clear_default_wifi_driver_and_handlers() in the teardown sequence, e.g.,

Code: Select all

    esp_wifi_stop();
    esp_wifi_deinit();
    esp_wifi_clear_default_wifi_driver_and_handlers(_espNetif); // <-add this!
    esp_netif_destroy(_espNetif);
(Post left in place in case it's helpful to anyone else.)
Last edited by adamwilt on Sun Dec 20, 2020 11:33 pm, edited 3 times in total.

Who is online

Users browsing this forum: No registered users and 288 guests