2

I have an application that uses an ESP8266 running ESP_RTOS_SDK version 3.4 and an STM8. It is solar powered, so minimising current consumption is crucial. It works in three modes:

  • between events: the ESP8266 is in deep sleep and the STM8 is collecting data
  • during an event: the ESP8266 is in light sleep and the STM8 wakes it up every 10 seconds with some data
  • after an event: the ESP8266 wakes up fully, connects to wifi, sends all of the collected data.

If I disable light sleep, everything works fine. With light sleep enabled, The light sleep itself works fine but the ESP8266 does not connect to wifi.

ESP-IDF light sleep is documented here. This is my light sleep function:

// -------------------------------------------------------------------
//  in light sleep, the processor is stopped.
//   we wake up on a WAKE=low

void light_sleep (void) {

  gpio_wakeup_enable(GPIO_WAKE_PIN, GPIO_INTR_LOW_LEVEL);
  esp_sleep_enable_gpio_wakeup();
  esp_sleep_enable_timer_wakeup (10000000L);

  esp_light_sleep_start ();
  vTaskDelay (1);

  esp_sleep_disable_wakeup_source (ESP_SLEEP_WAKEUP_GPIO);
  esp_sleep_disable_wakeup_source (ESP_SLEEP_WAKEUP_TIMER);

}

This is the code that I use to start the wifi:

// ------------------------------------------------------------------------------
static void app_wifi_start (void) {

    wifi_config_t config = {};

    ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &ip_event_handler, NULL));

    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));

    strncpy((char *)&config.sta.ssid, wifi_config.remote_ssid, sizeof (config.sta.ssid));
    strncpy((char *)&config.sta.password, wifi_config.remote_password, sizeof (config.sta.password));
    if (strlen((char *)config.sta.password)) {
        config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
    }
    config.sta.pmf_cfg.capable = true;
    config.sta.pmf_cfg.required = false;

    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &config) );

    ESP_ERROR_CHECK(esp_wifi_start());

    esp_wifi_connect(); 

}

The return code from esp_wifi_connect () is ESP_OK.

My question is: how do I make wifi start after a light sleep?


Update: this is how I stop the wifi.
// ----------------------------------------------------------
static void app_wifi_stop (void) {

    ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &ip_event_handler));
    ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler));

    switch (current_mode) {
        case WIFI_MODE_STA:
        case WIFI_MODE_APSTA:

            esp_wifi_disconnect ();
            break;

        case WIFI_MODE_AP:
            break;
   
        default:
            break;    
    }

    ESP_ERROR_CHECK(esp_wifi_stop ());

    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
    current_mode = WIFI_MODE_NULL;
    sta_connected = false;
    ap_connections = 0;

}
JavaLatte
  • 378
  • 5
  • 18

1 Answers1

1

I don't know if you solved the problem. It's been a long time. Well... Before going into light sleep, you should put the wifi in power saving mode. Also, you must call esp_wifi_stop() to stop the wifi task. However, you can only call esp_wifi_stop() when you are sure that no service is using wifi in the background. For example, if you are connected to an MQTT broker, you must make sure your MQTT queue is empty and call esp_mqtt_client_stop(). So after that, you're ready to call esp_wifi_stop() and go into light sleep by calling esp_light_sleep_start();.

  • Thanks for your feedback. I have updated my question to show how I stop the wifi. My app will not try to light sleep if any of its own modules is using/wants to use wifi. The disconnect stops the web server, which is the only ESP-provided functionality. What exactly do you mean by "put the wifi in power saving mode"? – JavaLatte Feb 01 '22 at 02:03
  • ESP-IDF is an operating system. Your application makes requests that are executed by the OS. Your application may not be using wifi, but the OS may be finishing executing some requests from your application. So, before stopping the wifi, you must "ask" the OS if the queues of the services you are using are empty. Well... That's what I would do. Finally... Putting the modem in power save mode is a prerequisite for ESP32. I'm not sure about the 8266. But... See `esp_wifi_fpm_set_sleep_type()` and `esp_wifi_fpm_open()`. I hope it helps! – José de França Feb 01 '22 at 21:47
  • esp_wifi_fpm_set_sleep_type is deprecated: if you call it anyway, you get a message saying "this API has been removed". Regarding pending requests, I have cut down to app to just opening and closing wifi, with a 10 second delay after closing: it works every time before entering light sleep mode, and fails every time after light sleep. – JavaLatte Jul 24 '22 at 09:27