Page 1 of 1

sleep mode and restarting Wifi

Posted: Mon Jul 23, 2018 6:50 pm
by mzimmers
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...

Re: sleep mode and restarting Wifi

Posted: Mon Jul 23, 2018 7:03 pm
by hassan789
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

Re: sleep mode and restarting Wifi

Posted: Mon Jul 23, 2018 7:52 pm
by mzimmers
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...

Re: sleep mode and restarting Wifi

Posted: Mon Jul 23, 2018 8:00 pm
by mzimmers
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);

Re: sleep mode and restarting Wifi

Posted: Mon Jul 23, 2018 8:43 pm
by ESP_igrr
@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.

Re: sleep mode and restarting Wifi

Posted: Mon Jul 23, 2018 10:07 pm
by mzimmers
Thanks, igrr. So, do I need to call stop and disconnect, or is stop enough?

Re: sleep mode and restarting Wifi

Posted: Tue Jul 24, 2018 4:05 am
by ESP_igrr
Calling esp_wifi_stop is enough.

Re: sleep mode and restarting Wifi

Posted: Tue Jul 24, 2018 7:00 pm
by mzimmers
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...

Re: sleep mode and restarting Wifi

Posted: Wed Jul 25, 2018 10:36 am
by ESP_igrr
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.

Re: sleep mode and restarting Wifi

Posted: Wed Jul 25, 2018 5:18 pm
by mzimmers
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?