ESP8266 Light Sleep

I saw many tutorials about using Deep Sleep for saving power and running an esp8266 for weeks on a battery.
While deep sleep gets by far the largest power savings, it has a couple quirks:

  • the device isn’t responsive to commands sent from the app
  • on wakeup, the sketch is restarted rather than resumed

Since i didn’t need to run on a battery, but just to produce less heat (a thermometer is attached to my nodemcu), I started looking into light sleep.

It doesn’t take much code to use it.
You just need to add 2 lines in your setup to make sure the ESP is in client mode and enable light sleeping:

WiFi.mode(WIFI_STA);
wifi_set_sleep_type(LIGHT_SLEEP_T);

Now, every time you use delay(ms) the board will enter light sleep for the duration of the delay itself.

This is the reason we do a thing which is not well regarded among blynkers: we add a delay in our loop.

Longer sleep means more savings, but commands you send through the app will be received on the next Blynk.run(), which introduces a larger average latency.

So, my basic Blykn sketch with light sleep would look like this:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

  //  Force the ESP into client-only mode
  WiFi.mode(WIFI_STA); 

  //  Enable light sleep
  wifi_set_sleep_type(LIGHT_SLEEP_T);
}

void loop()
{
  Blynk.run();
  delay(1000);  //  Put the esp to sleep for 1s
}

This gives me the following current absorption on a NodeMcu board, powered via USB. I used an arduino and a 10Ohm shunt to take the measurements, so the accuracy is questionable, but it should be good enough. [details=**Graph description**] * The higher ~75mA peaks correspond to when the board wakes up and runs `Blynk.run()`. * The lowest flats at ~11mA correspond to light sleep. * Slightly higher ~15mA flats happen when the board has some task running and enters modem_sleep instead. * Smaller peaks correspond to the arrival of a WiFi Beacon. [/details] The average absorption here is ~23mA, which is 3 times less than the normal 70mA absorption of the same nodemcu board in client mode.

This seems a bit unsatisfactory, but keep in mind that the nodemcu board includes a 3.3V regulator and an USB Serial adapter, which are likely responsible for most of the 12mA drain during sleep.
A bare ESP should drain much less (on the order of 1mA).

For details on disabling the regulator and serial adapter on a nodemcu, check this article (it’s an hardware mod).

Also, some ESPs have a defect where the WiFi transmitter turned off will cause interference with the ADC: https://github.com/esp8266/Arduino/issues/2070

If light sleep causes sudden, unexplicable downwards peaks in you analog pin readings, try to ping something just before.
I suggest you use this library to ping your wifi router.
You can get the IP of your router through the function WiFi.gatewayIP().

16 Likes

this is a fantastic post, thank you for sharing!

i will try it out…

Thank you @Gspin!

Nice one :smile:

Thanks :smiley:
If you try this, do tell if/how well it worked, and let out your doubts so i can use them to improve the explanation :wink:

I’m interested in knowing if you did anything special in regards to measuring the esp current? I can’t get my 01 to work properly when hooked up to my multimeter…

3.3v -> positive on MM -> COM on MM -> esp VCC.

Meter set to 200mA max measurement, trying to see my circuit consumption but my device goes haywire.

If I short-circuit red/black on my multimeter it works fine when voltage is applied, but through it, it goes bonkers. I can successfully measure parts of my circuit so the meter isn’t flawed.

At the moment my esp is awake for 5 sec, posting to terminal then sleeps for 30.
Also if I short circuit on power on, wait for sleep mode then break the short-circuit, I get measurements at 20uA until it wakes up, then I get stuck at 135 mA measurements and esp stops working.

So strange.

it could be your dmm. did you checked what voltage drop you have on the dmm?
if a poor design, can have a significant voltage drop, and that can cause the esp to malfunction. (just a quick idea)

1 Like

Agreeing with wanek: you have an excessive voltage drop across your multimeter.
A current meter should have very low impedance, maybe the terminals aren’t well plugged or the plugs are oxidized.

Regarding what i did:
I used the 10 Ohm resistor to indirectly measure the current by measuring its voltage drop.

The nodemcu was unable to connect normally because connecting requires some 140mA.
The resistor made the USB voltage drop from 5 to 3.6V, too low for the 3.3V regulator to work properly, so the board would be stuck connecting.

Temporarily bypassing the resistor allowed the device to connect, then it would only require 80mA at most, and a 0.8 drop to 4.2V would be no trouble.

Thank you for your answers!
The voltage drop when measuring voltage goes from 3,19V when the esp is awake to full 3.3V when it is in deep sleep.
Is that a horrible voltage drop? My DMM is rather new and the cables to it show no sign of degradation.

I will try measuring again to night if my kid allows it :slight_smile: I’ll add in some photos

3.19V across the ESP should be fine.
Did you measure this with another multimeter while also measuring current with the incriminated one?

No unfortunately I don’t have a spare one, I’ll probably order one this month though :stuck_out_tongue:

then how did you got the 3.19v?
do you have an axiomet dmm?

I got 1, dont have one extra for checking voltage while measuring current on another :stuck_out_tongue:

Got me one of theese

How would i be able to wake up the module from light sleep with a push button/gpio on your example?

thx

I didn’t research into this myself, but there is something at page 9 of this document from espressif: http://www.espressif.com/sites/default/files/9b-esp8266-low_power_solutions_en_0.pdf

I don’t have the function wifi_set_sleep_type on Wemos D1 mini…

Add the following at the top of your sketch:

extern "C" {
  #include "user_interface.h"
}
1 Like

Excellent solution! On the NodeMCU board the use of Light Sleep instead of Deep Sleep in Blynk projects where it is required to make more efficient use of energy management.
There is a sacrifice in the responsiveness of the application, but the impact is not so noticeable in applications that do not require real time control.

wifi_set_sleep_type(LIGHT_SLEEP_T); is turning off the device and it doesn’t come back online, is there another way to sleep the D1 mini?

@Yamashiro you already have a topic of your own asking about this. Read the other topics, but please keep your questions/feedback in yours for continuity. BTW, sleep is not a Blynk function. Google for more details on how it works.