ESP8266 does not enter sleep light mode with programmed time

Hey guys,
I'm having trouble getting the ESP8266 to sleep light mode.
From the description in this document, (with similar example on the final pages), if I use a value less than 0xFFFFFFF, in wifi_fpm_do_sleep(), the ESP should go into sleep light mode forever.
And it can be woken up by a GPIO.
Using these conditions I can get it to work correctly.
ESP8266 enters sleep light mode, and exits when I put the GPIO 2 in LOW level. (set with wake up GPIO).

Per the document, if I use a value less than 0XFFFFFF, in
wifi_fpm_do_sleep(), this value should be used as a timer to wake up the ESP8266.
But when using any recommended value (between 10000 and 268435454) ESP does not go into sleep light mode.

There is relatively little information about sleep light mode.
I searched here in the forum with the argument "ESP8266 sleep light mode" and I didn't find any topic that helped me.
If anyone has any information that can help me to solve the problem or for me to give up the project,
Thank you in advance.

Ref: Document
https://www.espressif.com/sites/default/files/documentation/2c-esp8266_non_os_sdk_api_reference_en.pdf

APIs
https://docs.espressif.com/projects/esp8266-rtos-sdk/en/latest/api-reference/system/sleep_modes.html

Code to sleep the ESP8266.

#include "user_interface.h"
#include "gpio.h"
#define WAKE_UP_PIN 2
//--------------------------------------------------------------------------
void setup() {
  Serial.begin(115200);
  pinMode(WAKE_UP_PIN, INPUT_PULLUP);
  Serial.println(" ");
  Serial.println("Start");
}
//--------------------------------------------------------------------------
void loop() {
  Serial.println("Sleep");
  light_sleep();
  delay(10);
  Serial.println("Wake Up");
}
//--------------------------------------------------------------------------
void light_sleep() {
  wifi_station_disconnect();                  // Disconecte station
  wifi_set_opmode(NULL_MODE);                 // set WiFi  mode  to  null  mode.
  wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);     // light sleep
  wifi_fpm_open();                            // enable  force sleep
  gpio_pin_wakeup_enable(WAKE_UP_PIN, GPIO_PIN_INTR_LOLEVEL); //
  wifi_fpm_do_sleep(268435455);               // (0xFFFFFFF)  Sleep Forever
  //wifi_fpm_do_sleep(268435454);             // (0xFFFFFFE)  No Sleep
}

What does the wifi_fpm_do_sleep() call return?
Did you notice that the argument value is in µs? So the maximum value let the ESP8266 sleep for about 4 minutes.

Hi,
thanks for your answer.
It took me a while to get back because I was traveling.

I know the maximum time is ~= 4 minutes, but I only need 1 minute of sleep.

I used the following sequence of commands

   int err = wifi_fpm_do_sleep(0xFFFFFFE);
   Serial.print("ERROR: ");
   Serial.println(err);

and got the following result:
ERROR: 0
ERROR: 0
ERROR: 0
ERROR: 0
ERROR: 0

I kept searching the internet and found a code that is working.
I'm studying it to understand why mine doesn't work properly.
The code is as follows:
ref: WeMos D1 mini (esp8266), the three type of sleep mode to manage energy savings – Part 4 – Renzo Mischianti

  #include "Arduino.h"
  #include <ESP8266WiFi.h>
  // Required for LIGHT_SLEEP_T delay mode
  extern "C" {
  #include "user_interface.h"
  }

  const char* ssid = "xxxxxxx";
  const char* password = "yyyyyyyyyyyy";
  //----------------------------------------------------------------
  //The setup function is called once at startup of the sketch
  void setup() {
  Serial1.begin(115200);
  while (!Serial1) { }

  Serial1.println();
  Serial1.println("Start device in normal mode!");

  WiFi.mode(WIFI_STA);

  WiFi.begin(ssid, password);
  Serial1.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial1.print(".");
  }
  Serial1.println("");
  Serial1.print("Connected to ");
  Serial1.println(ssid);
  Serial1.print("IP address: ");
  Serial1.println(WiFi.localIP());
  }
  //----------------------------------------------------------------
  void callback() {
  Serial1.println("Callback");
  Serial1.println(millis());
  Serial.flush();
  }
  //----------------------------------------------------------------
  void loop() {
  Serial1.println("Enter light sleep mode");

  // Here all the code to put con light sleep
  // the problem is that there is a bug on this
  // process
  //wifi_station_disconnect(); //not needed
  uint32_t sleep_time_in_ms = 10000;
  wifi_set_opmode(NULL_MODE);
  wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);
  wifi_fpm_open();
  wifi_fpm_set_wakeup_cb(callback);
  wifi_fpm_do_sleep(sleep_time_in_ms * 1000 );
  delay(sleep_time_in_ms + 1);

  Serial1.println("Exit light sleep mode");

  WiFi.begin(ssid, password);
  Serial1.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial1.print(".");
  }
  Serial1.println("");
  Serial1.print("Connected to ");
  Serial1.println(ssid);
  Serial1.print("IP address: ");
  Serial1.println(WiFi.localIP());

  wifi_set_sleep_type(NONE_SLEEP_T);
  delay(10);  //  Put the esp to sleep for 15s
  }

Maybe https://www.esp8266.com/viewtopic.php?p=53042 ...

void fpm_wakup_cb_func1(void)  {
  wifi_fpm_close(); // disable force sleep function
  //wifi_set_opmode(STATION_MODE); // set station mode
  //wifi_station_connect();
  // Serial.println("Woken up...");
}
void sleepNow(int MILI) {
  // Serial.println("Going to sleep...");
  wifi_fpm_set_sleep_type(LIGHT_SLEEP_T); // light sleep
  wifi_fpm_open(); // enable force sleep
  wifi_fpm_set_wakeup_cb(fpm_wakup_cb_func1); // Set wakeup callback
  wifi_fpm_do_sleep(MILI * 1000);
  delay(MILI + 10);
}

Other possible relevant links:
https://www.google.com/search?q=wifi_fpm_do_sleep+site

Hi,
thanks for your attention.
About the first link: " Force ligth sleep mode reset my ESP - Everything ESP8266 "

I tested both codes: The one from the author " SeregaKai " and the answer from " Orcanbull ". Both had the same problem.
ESP does not go into sleep mode for 4 seconds as tested.
1 sec = 1000 x 4000 at wifi_fpm_do_sleep(1000UL * 4000UL).
Current time between sleep and wake up 1 ms.

Sleep
Wake Up
1

Sleep
Wake Up
1

extern "C" {
#include "user_interface.h"
}
unsigned long myTime;
//------------------------------------------------ ----------------
//The setup function is called once at startup of the sketch
void setup() {
  Serial.begin(115200);
}
//------------------------------------------------ ----------------
void loop() {
  Serial.println("Sleep");
  sleepNow();
}
//------------------------------------------------ ----------------
void fpm_wakup_cb_func1() {
  wifi_fpm_close(); // disable force sleep function
}
//------------------------------------------------ ----------------
void sleepNow() {
  myTime = millis();

  wifi_set_opmode(NULL_MODE);
  wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);
  wifi_fpm_set_wakeup_cb(fpm_wakup_cb_func1);
  wifi_fpm_open();
  wifi_fpm_do_sleep(1000UL * 4000UL);
  Serial.println("Wake Up");
  Serial.println(millis() - myTime);
  Serial.println("");
}

About the search argument:
Using the search argument you suggested, I only found 9 links.
but I had already seen all 9.
I'll make a post on the expressif forum.

About my comment that it had worked in reality it doesn't work.
It simply stops with a delay at the end of the light sleep routine.

Good luck. You may achieve a few answers on the forum:
https://esp32.com/viewforum.php?f=19

Remember that "Arduino" on ESP32 is not a commercial endeavor but more of a side-line same as the STM32duino.com site, it is a best-effort. Espressif professional developers are all about raw IDF usage and the Arduino port represents many wrappers and some kludgy code.
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/sleep_modes.html

In my opinion, were there "sleep/wake: issues in the published IDF, Espressif would be on that issue already.

https://docs.espressif.com/projects/esp-idf/en/v4.2/esp32/

PDF bugs:
https://www.espressif.com/sites/default/files/documentation/eco_and_workarounds_for_bugs_in_esp32_en.pdf

Hi,
thanks, but my difficulty is not with ESP32, but with ESP8266.

https://www.esp8266.com/viewforum.php?f=25

Same individual did both ports, but IMO the ESP32 is better. Best-effort stills applies.

Good luck.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.