It mainly runs in light sleep and wakes up every few seconds to take a reading. After collecting a set of readings, it turns on wifi and sends it to a server, then turns off wifi and starts collecting another set of data.
Unfortunately, Wifi does not always start after a light sleep:
- reboot, start wifi ... works
reboot, light sleep, start wifi ... works
reboot, start wifi, stop wifi, light sleep, start wifi ... does not work
I would appreciate suggestions about how to make it start.
I am using the following code to enter light sleep:
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_LOGI (TAG, "Entering light sleep");
esp_light_sleep_start ();
vTaskDelay (1);
// Wake up here
esp_sleep_disable_wakeup_source (ESP_SLEEP_WAKEUP_GPIO);
esp_sleep_disable_wakeup_source (ESP_SLEEP_WAKEUP_TIMER);
ESP_LOGI (TAG, "Awake again");
}
This works, but when I try to start Wifi after a light sleep, nothing happens. I stop wifi before light sleep and restart if afterwards using these two routines, which work correctly before a light sleep:
static void app_wifi_start (wifi_mode_t mode, int retries) {
max_retries = retries;
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(mode));
current_mode = mode;
switch (mode) {
case WIFI_MODE_STA:
sta_prepare ();
break;
case WIFI_MODE_AP:
ap_prepare ();
break;
case WIFI_MODE_APSTA:
ap_prepare ();
sta_prepare ();
break;
default:
break;
}
ESP_ERROR_CHECK(esp_wifi_start());
}
// -----------------------------------------------------------------------------------------------
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:
sta_cancel ();
break;
case WIFI_MODE_AP:
ap_cancel ();
break;
case WIFI_MODE_APSTA:
ap_cancel ();
sta_cancel ();
break;
default:
break;
}
ESP_ERROR_CHECK(esp_wifi_stop ());
vTaskDelay (1000 / portTICK_RATE_MS);
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
current_mode = WIFI_MODE_NULL;
sta_connected = false;
}