ESP8266 Light Sleep Mode

By snorlaxprime

This is the continuation from the previous blog about ESP8266 Deep Sleep Mode, I am trying to use the other sleep mode. So I embark on the journey to find out how to make use of the Light Sleep Mode. Several codes that I have find causes my ESP8266 to crash miserably.

The concept is to let ESP8266 to Light Sleep indefinitely, only to be waken up when there is a level changes (HIGH to LOW) or (LOW to HIGH) on one of the GPIO pin. This is also called Forced Light Sleep.

After pulling my hair for several days trying to get the Light Sleep Mode to work, I finally figured out how to do this. So here I am sharing my findings. I hope this will be useful for people trying to implement the Light Sleep Mode.

Step 1. Setup() section

Unlike the deep sleep, where most of the working code is in the Setup() section, for Light Sleep, the code will be in the Loop() section, the Setup() section will be used as per normal to initialise the serial connection and initialisation of GPIO pins. This is shown below:

void setup() {
   gpio_init(); // Initilise GPIO pins

Step 2. Loop() section

The loop section contains the code where we initialise the WiFi, this is because when during the Light Sleep the WiFi will be off (shown in the above diagram), upon waking up we will need to initialise the WiFi again. The Loop() section code is shown below:

void loop() {
    // insert working code here e.g.: post Temperature value
       Serial.println("Going to sleep now");
   Serial.println("Wake up");

As you can see in the code above we start with Initialising WiFi, and then followed by 200 ms delay and we are going to sleep. The sleep code is performed by light_sleep() function. It will only wake up when the GPIO pin is triggered, this will be explained in the next section. When ESP8266 wakes up, it will have 200 ms delay before printing the “Wake up” string in the Serial console, then it will perform the initialisation of WiFi again. Ideally the working code will be inserted after initWifi() function.

Step 3. Light_sleep() function

The Light_sleep() function is used to disconnect the WiFi and then forced the ESP to go to sleep, and defining the GPIO pin that will makes it wake up. This is shown in the code below:

void light_sleep(){
   wifi_fpm_set_sleep_type(LIGHT_SLEEP_T); // set sleep type, the above    posters wifi_set_sleep_type() didnt seem to work for me although it did let me compile and upload with no errors 
   wifi_fpm_open(); // Enables force sleep
   gpio_pin_wakeup_enable(GPIO_ID_PIN(2), GPIO_PIN_INTR_LOLEVEL); // GPIO_ID_PIN(2) corresponds to GPIO2 on ESP8266-01 , GPIO_PIN_INTR_LOLEVEL for a logic low, can also do other interrupts, see gpio.h above
   wifi_fpm_do_sleep(0xFFFFFFF); // Sleep for longest possible time

As you can see in the code above, we are using GPIO pin 2, (D4) to wake up the ESP8266 from the Light sleep. It will wake up when D4 is changing transition from HIGH to LOW as per the code above.

To be able to use this we need to include the gpio.h in the definition. This is done in the code below:

extern "C" {
   #include "gpio.h"

We also need to include user_interface.h to allow us to the the LIGHT_SLEEP_T delay mode as shown in the following code:

// Required for LIGHT_SLEEP_T delay mode
 extern "C" {
   #include "user_interface.h"

Finally, last but not the least, for completeness, I have also included the initWifi() function as below to connect to WiFi:

void initWifi() {
   WiFi.begin(ssid, password);
   while ((WiFi.status() != WL_CONNECTED)) {
   Serial.print("WiFi connected, IP address: "); Serial.println(WiFi.localIP());

You can see it in action, on the following video:

That’s all for now, I hope you are able to use the Light sleep function without too much trouble. Please let me know if you have any questions, and don’t forget to subscribe to get more tips on IoT projects.