sleep mode and restarting Wifi

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

sleep mode and restarting Wifi

Postby mzimmers » Mon Jul 23, 2018 6:50 pm

Hi all -

I'm trying to implement the use of one of the sleep modes for power conservation. My initial idea was to have a low-power loop that:
  • shuts down wifi (via esp_wifi_disconnect)
  • enters a timed sleep mode
  • restarts wifi
  • does some processing
I don't know what I'm doing wrong, but I don't seem to be restarting wifi successfully after I wake up. I say "seem to" because debugging this is rather elusive; in order to enter sleep mode, I disconnect my serial cable which of course loses all logging.

I'd be happy to share code, but right now, I'm interested in whether my algorithm seems valid, or if I'm doing it wrong.

Thanks...

User avatar
hassan789
Posts: 156
Joined: Thu Jun 29, 2017 2:15 am

Re: sleep mode and restarting Wifi

Postby hassan789 » Mon Jul 23, 2018 7:03 pm

1. is this light-sleep, or deep-sleep?
2. have you tested the wifi power-save example already?
https://github.com/espressif/esp-idf/tr ... power_save

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

Re: sleep mode and restarting Wifi

Postby mzimmers » Mon Jul 23, 2018 7:52 pm

Hi Hassan -

I'm willing to use either light or deep sleep. I've tried both, but neither seem to work as I expected.
No, I hadn't tried that example. In fact, I wasn't aware of the esp_wifi_set_ps() routine. The readthedocs page is somewhat brief on this; is there somewhere else this is better described?

Thanks...

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

Re: sleep mode and restarting Wifi

Postby mzimmers » Mon Jul 23, 2018 8:00 pm

Here's a code fragment that shows what I'm trying to do. I welcome any input.

Code: Select all

        // if we're on battery power.
        else if (m_powerSourceNow == POWER_SRC_BATTERY)
        {
            // if we're transitioning to battery power,
            // turn off the LED, enable the pushbutton wakeup
            // and stop Wifi.
            if (m_powerSourcePrevious != m_powerSourceNow)
            {
                msg.buildLog("Now on battery power.");
                m_params->worker->sendToUtility(msg);

                // turn off the LED.
                lqe.type = MSG_LED_SET_PATTERN;
                lqe.value = LEDC_SOLID_OFF;
                xQueueSendToBack(m_params->ledQueue, &lqe, 0);

                // enable wakeup timer.
                esp_sleep_enable_timer_wakeup(SLEEP_INTERVAL);

                // enable wakeup by pushbutton.
                esp_sleep_enable_ext0_wakeup(BUTTON_GPIO, 0);

                msg.buildLog("Disconnecting wifi.");
                m_params->worker->sendToUtility(msg);
                esp_wifi_disconnect();
                vTaskDelay(100); // give worker time to process message.
            }
            // sleep.
            ESP_ERROR_CHECK(esp_light_sleep_start());
            //esp_deep_sleep_start();

            // upon waking, get the Wifi going again.
            m_params->worker->connect(); // calls esp_wifi_connect().
            while (m_params->worker->getSocketState() == SOCKET_DISCONNECTED)
            {
                vTaskDelay(100);
            }

            //esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
            msg.buildLog("Woke from sleep mode.");
            m_params->worker->sendToUtility(msg);

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: sleep mode and restarting Wifi

Postby ESP_igrr » Mon Jul 23, 2018 8:43 pm

@mzimmers You also need to call esp_wifi_stop before entering light sleep mode, and call esp_wifi_start after exiting it. The stopping part is mentioned in https://docs.espressif.com/projects/esp ... leep-modes.

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

Re: sleep mode and restarting Wifi

Postby mzimmers » Mon Jul 23, 2018 10:07 pm

Thanks, igrr. So, do I need to call stop and disconnect, or is stop enough?

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: sleep mode and restarting Wifi

Postby ESP_igrr » Tue Jul 24, 2018 4:05 am

Calling esp_wifi_stop is enough.

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

Re: sleep mode and restarting Wifi

Postby mzimmers » Tue Jul 24, 2018 7:00 pm

Thank you.

I'd like to confirm that I understand how this works. In the following code passage:

Code: Select all

                // enable wakeup timer.
                esp_sleep_enable_timer_wakeup(SLEEP_INTERVAL);
                err = esp_wifi_stop();
              
            // sleep.
            ESP_ERROR_CHECK(esp_light_sleep_start());
         
            // upon waking, get the Wifi going again.
            err = esp_wifi_start();
after the timer ends sleep mode, does program control fall through to the esp_wifi_start() call? Because I don't seem to be getting there.

Thanks...

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: sleep mode and restarting Wifi

Postby ESP_igrr » Wed Jul 25, 2018 10:36 am

Yes, basically esp_light_sleep_start function will return when light sleep ends (due to one of the wakeup sources).

If you are having issues with this bit of code, suggest removing the WiFi related bits and verify that sleep and wakeup works correctly first.

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

Re: sleep mode and restarting Wifi

Postby mzimmers » Wed Jul 25, 2018 5:18 pm

Debugging this is miserably difficult...I really need an IDE.

So what happens to the other tasks when one task calls esp_light_sleep_start()? Do they suspend as well, or do they continue?

Who is online

Users browsing this forum: Esp_Noobe_Sag, MicroController and 76 guests