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