JavaLatte
JavaLatte

Reputation: 378

Wifi does not start after light sleep

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:

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;

}

Upvotes: 2

Views: 1197

Answers (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();.

Upvotes: 1

Related Questions