ESP deep sleep

Hi Costas,

yes, GIO16 and RST connected. I see in the Serial monitor that ESP resets but the communication was lost: “Your ESP8266 is not in network”.

If the ESP is asleep it will not be connected to Blynk so “Your ESP8266 is not in network” is right.

Ok only I’m not able to recover the connection. May be I’ve to move :;;

in the Void Setup ?

As per my earlier post, don’t put deepSleep in the main loop. Not Setup either, use void ReadSensor().

Hi Costas,

thank yuo very much,

Following your suggestion I’ve solved in this way :

void ReadSensor() {

while (Blynk.connect() == false) {
// Wait until connected

SensorsReading = “00.00”;

if (Serial.available()) {
SensorsReading = Serial.readString();

Voltage = SensorsReading.toFloat();

Blynk.virtualWrite(V1, Voltage);
Blynk.virtualWrite(V2, ESP.getVcc());



Now works fine.

thank you again


i am interested in deep sleeep too :slight_smile:

but why is there a delay() after deepSleep?

doesn’t it start back at the setup() once it is called?

Call it convention @Dave1829. Reset, restart and deepSleep sometimes like short delays after them but you are right the ESP reboots to setup() when it wakes.

1 Like

thank you :thumbsup:

and if i have multiple void readSensor() type functions, i put it at the last function?

before my void setup() & void loop()

Yes or after x amount of time.

1 Like

ah yes, because i can know how long my functions take to execute :slight_smile:

thanks again!

Thanks for your tips, I got curious and from yesterday my Wemos mini D1 is sleeping as expected feeded by a really small home made UPS (only 700 mAh). Every 20 minutes reads the data from a Sensor, sends an email, update the App and goes to sleep again.

In case there are still challenges, this code worked for me… I am using it for a weather monitoring project where the ESP only needs to wake up every so often, take a temp recording and then go back to sleep. All the work is done in Setup();

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SHT1x.h>                    //HumidityTemp Sensor SHT11

// how long to sleep between checking and TX temp/batt values - in seconds
#define SLEEP_LENGTH 60

const int led_pin = 15;  //blue LED on board
const char* ssid     = "xxx";
const char* password = "xxx";
char auth[] = "xxx";  

void setup()
  pinMode(led_pin, OUTPUT);

  Blynk.begin(auth, ssid, password);
    while (Blynk.connect() == false) {
     //Wait until connected

  //do stuff here.......   
  ESP.deepSleep(SLEEP_LENGTH * 1000000,WAKE_RF_DEFAULT);   //try the default mode

void loop()
 //do nothing here. The Setup() function takes care of it all as the ESP resets each time after it wakes from sleep

I think we are doing more or less the same…

It’s a lovely weather, isn’t it? :wink:


For sure! …and your app just gave me an idea! Thx.

1 Like

so my battery powered BME280 temp/humidity/pressure + BH1750 light sensor combo ESP8266 node is doing well with deep sleep, running off 2 x 18650 and LM2596 regulator.

it does thingspeak updates as well

my apparently very simple deep sleep code is:


my entire code for anyone interested or anyone who wants to provide advice/criticism on improving it is:

  BME280 Test.ino
  This code shows how to record data from the BME280 environmental sensor.
  This file is an example file, part of the Arduino BME280 library.
  Copyright (C) 2016  Tyler Glenn

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <>.

  Written: Dec 30 2015.
  Last Updated: Jan 1 2016.

//#define BLYNK_PRINT Serial //this is the debugging for Blynk
//#define BLYNK_DEBUG        // Optional, this enables 'full' debugging detailed prints

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <BME280.h>
#include <BH1750FVI.h>
#include <Wire.h>

WiFiClient client;
BH1750FVI LightSensor;

BME280 bme;                   // Default : forced mode, standby time = 1000 ms
SimpleTimer timer;


bool debug = 1; //turns serial prints on (1) or off (0)

int vcc = ESP.getVcc(); //readvdd33();
double cellarDewPoint;
float medianHum;
float medianTemp;
float node3DewPoint;

const char* ssid = "xxx";
const char* password = "xxx";

const char* serverThingspeak = ""; //this is the Thingspeak address
String apiKeyThingspeak = "xxx"; // Channel 78844

char authBlynk[] = "xxx"; //insert token generated by Blynk

bool metric = true;

void printBME280Data()
  float temp, hum, pres;
  long rssi = WiFi.RSSI();
  uint8_t pressureUnit(1);     // unit: B000 = Pa, B001 = hPa, B010 = Hg, B011 = atm, B100 = bar, B101 = torr, B110 = N/m^2, B111 = psi

  bme.ReadData(pres, temp, hum, metric, pressureUnit);   // Parameters: (float& pressure, float& temp, float& humidity, bool celsius = false, uint8_t pressureUnit = 0x0)
  node3DewPoint = dewPointAccurate(temp, hum);

  uint16_t lux = LightSensor.GetLightIntensity();// Get Lux value

  if (debug == 1) {
    Serial.print("Temp: ");
    Serial.print("Humidity: ");
    Serial.println("% RH");
    Serial.print("Pressure: ");
    Serial.println(" hPa");
    Serial.print("Dew Point: ");
    Serial.println(" 'C");
    Serial.print("Light: ");
    Serial.println(" lux");
  Blynk.virtualWrite(V11, temp); // this sends the reading to the Blynk virtual pin
  Blynk.virtualWrite(V9, hum); // this sends the reading to the Blynk virtual pin
  Blynk.virtualWrite(V2, pres); // this sends the reading to the Blynk virtual pin
  Blynk.virtualWrite(V8, node3DewPoint); // this sends the reading to the Blynk virtual pin
  Blynk.virtualWrite(V19, vcc); // this sends the reading to the Blynk virtual pin
  Blynk.virtualWrite(V18, rssi); // this sends the reading to the Blynk virtual pin
  Blynk.virtualWrite(V4, lux); // this sends the reading to the Blynk virtual pin

  if (client.connect(serverThingspeak, 80))
    String postStr = apiKeyThingspeak;
    postStr += "&field1=";
    postStr += String(temp);
    postStr += "&field2=";
    postStr += String(hum);
    postStr += "&field3=";
    postStr += String(pres);
    postStr += "\r\n\r\n";
    postStr += "&field4=";
    postStr += String(rssi);
    postStr += "\r\n\r\n";
    postStr += "&field5=";
    postStr += String(vcc);
    postStr += "\r\n\r\n";
    postStr += "&field6=";
    postStr += String(lux);
    postStr += "\r\n\r\n";
    postStr += "&field7=";
    postStr += String(node3DewPoint);
    postStr += "\r\n\r\n";

    client.print("POST /update HTTP/1.1\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + apiKeyThingspeak + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
  if (debug == 1)
    Serial.println(F("just thingspoke"));

  Blynk.tweet((String("NODE_03: temperature: ")) + temp + (" 'C, humidity: ") + hum + ("'C, dew point: ") + node3DewPoint + ("'C, volts: ") + vcc + ("mV, WiFi: ") + rssi + ("dBm, light: ") + lux +("lx."));

  if (debug == 1)
    Serial.println(F("just tweeted"));
void setup()
    Serial.print(F("File name: "));
    if (debug == 1) {
      Serial.println(F("NODE03 - TPL Bug prototype BATTERY POWER - with Thingspeak & Blynk & Running Median & Twitter & Deep Sleep"));
      Serial.print(F("File name: "));
      Serial.print(F("ESP voltage: "));
    WiFi.begin(ssid, password);
    Serial.println(F("Where's some WiFi??"));
    while (WiFi.status() != WL_CONNECTED) {

    Serial.println(F("Found some WiFi!"));
    long rssi = WiFi.RSSI();
    Serial.print(F("WiFi signal strength (RSSI): "));
    Serial.println(F(" dBm"));

    Blynk.begin(authBlynk, ssid, password);
    Serial.println(F("Where's Blynk??"));
    while (!Blynk.connect()) {

    Serial.println(F("Blynk started! "));
//    bool isFirstConnect = true; // Keep this flag not to re-sync on every reconnection
//    BLYNK_CONNECTED(); // This function will run every time Blynk connection is established
//    {
//      if (isFirstConnect) {
//        Blynk.syncAll(); // where is the sync.All function supposed to go?
//        isFirstConnect = false;
//      }
//    }

    timer.setInterval(500L, printBME280Data);
    LightSensor.SetAddress(Device_Address_L);//'L' = Address 0x5C ???

    while (!Serial)
    } // Wait

    while (!bme.begin())
      Serial.println("Could not find BME280 sensor!");

void loop()
{; // Initiates Blynk; // Initiates SimpleTimer

/*-----( Declare User-written Functions )-----*/
// dewPoint function NOAA
// reference (1) :
// reference (2) :
double dewPointAccurate (double celsius, double humidity)
  // (1) Saturation Vapor Pressure = ESGG(T)
  double RATIO = 373.15 / (273.15 + celsius);
  double RHS = -7.90298 * (RATIO - 1);
  RHS += 5.02808 * log10(RATIO);
  RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO ))) - 1) ;
  RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
  RHS += log10(1013.124); // NB = 1013 = absolute air pressure from BME280 sensor!!!!???????????????

  // factor -3 is to adjust units - Vapor Pressure SVP * humidity
  double VP = pow(10, RHS - 3) * humidity;

  // (2) DEWPOINT = F(Vapor Pressure)
  double T = log(VP / 0.61078); // temp var
  return (241.88 * T) / (17.558 - T);
/* ==== END Functions ==== */

@Dave1829 we were quite disappointed with the performance of our 18650’s even though they power one of the world’s most famous electric cars, albeit 7000 of them rather than just a handful.

If my maths is right you are sleeping for 5 minutes. We sleep for 5 minutes during the day and 60 minutes at night. We were leaving our OLED on during sleeps so the last available data was visible at all times. This was drawing about 8mA but we will probably clear the display during sleep which takes us down to about 0.2mA.

How long are your 18650’s lasting between each charge?

This step-down is inefficient and you are losing a lot of battery capacity because of that try to find more efficient step down.

@costas - not sure how long, it’s only been 7 days…

@conkerh - I have the MP1584EN coming, are these ok?

Defo better but not the best but you should achieve around 90% or more efficiency with normal load from ESP, maybe 0.5 day more. If you look at efficiency curve you will notice that it is terrible for low current which is the problem here.

BTW you should use Buck/Boost converter as operating voltage of 18650 is from 3v-4.2v

It’s also quite important to note that efficiency during deep sleep will most likely drop to as low as 50% or even less…

Actually put an output from your DC/DC converter to 3V this will most likely give you bit more juice out of battery.

I’m using 2 batteries… These lasted almost 8 days… Maybe if I change it to 10min readings and lower the regulated voltage to 3v it could last a month?

What would be the best voltage converter to use?