Deep Sleep with Wemos TTGO ESP32

Hi Blynk community,

in my project I use a Wemos® TTGO WiFi + Bluetooth Battery ESP32 0,96 OLED with a BM280 sensor. To safe battery I use switch widget for deep sleep (60 sec). For the first time (typically 2 - 3 hrs) it seems that all works fine : if the switch is on (deep sleep) the oled goes off and the system goes in deep sleep mode for 60 sec and so on. But sometime it seems that after the wake up the sync all doesn’t work properly : the system doesn’t see the switch on and in such a way it doesn’t go in deep sleep mode (i.e. the system stay on).
I’ve made some tests but I don’t understand where is the problem. Some suggestion ?

// V1 : Humidity BME280 - spi
// V2 : Temperature BME280 - spi
// V3 : Pressure BME280 - spi
// V4 : Altitude BME280 - spi
// V5 : Vcc internal
// V6 : Led deep sleep status
// V7 : deep sleep switch

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <SimpleTimer.h>
#include <Wire.h>
#include <SPI.h>
#include "SSD1306.h" 
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

SSD1306  display(0x3c, 5, 4);

#define BME_SCK 16
#define BME_MISO 17
#define BME_MOSI 18
#define BME_CS 19

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI

WidgetLED led2(V6);
int DeepSleep_on = 0;
#define uS_TO_S_FACTOR 1000000
#define TIME_TO_SLEEP 60

char auth[] = "xxxx";

SimpleTimer timer;  

bool isFirstConnect = true;
BLYNK_CONNECTED() {
if (isFirstConnect) {
Blynk.syncAll();
isFirstConnect = false;
}
led2.off(); 
}

float Humidity;
float Temperature;
float Pressure;
float Altitude;

void setup() {

  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  
  display.displayOn();
  bme.begin();  

  display.init();
  display.setContrast(255);
  display.setFont(ArialMT_Plain_10);
 
  display.clear();
  display.drawString(0,10,">>>>> BME280 <<<<<");  
  display.display();
  delay(1000);
  
  Blynk.begin(auth, "xxxx", "xxxx");
  timer.setInterval(5000L, ReadSensor);
  Blynk.notify("WEMOS_METEO Ready");

}


void loop() { 
Blynk.run();
timer.run();
}

void ReadSensor() {  

Humidity = bme.readHumidity();
Temperature = bme.readTemperature();
Pressure = bme.readPressure() / 100.0F;
Altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);

  display.clear();
  display.drawString(0,10, "Hum");
  display.drawString(50,10, String(Humidity));
  display.drawString(0,30,"Temp");
  display.drawString(50,30, String(Temperature));
  display.drawString(0,20, "Alt");
  display.drawString(50,20, String(Altitude));
  display.drawString(0,40,"Press");
  display.drawString(50,40, String(Pressure));
  
  display.display();
  delay(100);
  display.clear();

float VBAT = (127.0f/100.0f) * 3.30f * float(analogRead(34)) / 4096.0f;  

Blynk.virtualWrite(V1, Humidity);
Blynk.virtualWrite(V2, Temperature);
Blynk.virtualWrite(V3, Pressure);
Blynk.virtualWrite(V4, Altitude);
Blynk.virtualWrite(V5, VBAT);
  
}


//Deep Sleep switch
 BLYNK_WRITE(V7)
 {   
   DeepSleep_on = param.asInt(); 
  if (DeepSleep_on == 1)
   {
   display.displayOff();
   led2.on();
   Blynk.notify("Deep Sleep");
   esp_deep_sleep_start();
   delay(100);
   }   
 }

I do not have any experience with ESP32, but maybe this might help. Not that how you are currenlty doing it is wrong.

You could try deleting this

bool isFirstConnect = true;
BLYNK_CONNECTED() {
if (isFirstConnect) {
Blynk.syncAll();
isFirstConnect = false;
}
led2.off(); 
} 

and adding this to the end of the ReadSensor function

Blynk.syncVirtual(V7);

and this to the BLYNK_WRITE(V7)

else{
led2.off();
}
1 Like

You should consider switching to Blynk.config() as your current method could leave your device in a holding pattern if it doesn’t connect to the server.

http://docs.blynk.cc/#blynk-firmware-configuration-blynkconfig

@Toro_Blanco, thank you very much for your help. I’ve tried your suggestion for two days but the behavior after deep sleep is the same : sometime ESP32 lost the communication or doesn’t sync with switch widget.

Following @Gunner now I’m trying with this (from a project by @Costas but also you can see keeping your suggestion) :


// V1 : Humidity BME280 - spi
// V2 : Temperature BME280 - spi
// V3 : Pressure BME280 - spi
// V4 : Altitude BME280 - spi
// V5 : Vcc internal
// V6 : Led deep sleep status
// V7 : deep sleep switch


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <SimpleTimer.h>
#include <Wire.h>
#include <SPI.h>
#include "SSD1306.h" 
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

SSD1306  display(0x3c, 5, 4);

#define BME_SCK 16
#define BME_MISO 17
#define BME_MOSI 18
#define BME_CS 19

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI

WidgetLED led2(V6);
int DeepSleep_on = 0;
#define uS_TO_S_FACTOR 1000000
#define TIME_TO_SLEEP 300

char auth[] = "";
char ssid[] = "";
char pass[] = "";
bool Connected2Blynk = false;

SimpleTimer timer;  

float Humidity;
float Temperature;
float Pressure;
float Altitude;

void setup() {

  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  
  display.displayOn();
  bme.begin();  

  display.init();
  display.setContrast(255);
  display.setFont(ArialMT_Plain_10);
 
  display.clear();
  display.drawString(0,10,">>>>> BME280 <<<<<");  
  display.display();
  delay(1000);
  
  Blynk.connectWiFi(ssid, pass);
  
  timer.setInterval(11000L, ReadSensor);
  Blynk.notify("WEMOS_METEO Ready");
  
  Blynk.config(auth);
  Blynk.connect();   
}

void loop() { 

if(Blynk.connected()){
    Blynk.run();
  }
  
  timer.run();
}

void ReadSensor() {  

if(!Blynk.connected()){
    
    Blynk.connect();  // try to connect to server with default timeout
  }
  else{
    Blynk.notify("WEMOS_METEO Ready");    
  }

Blynk.syncVirtual(V7);

Humidity = bme.readHumidity();
Temperature = bme.readTemperature();
Pressure = bme.readPressure() / 100.0F;
Altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);

  display.clear();
  display.drawString(0,10, "Hum");
  display.drawString(50,10, String(Humidity));
  display.drawString(0,30,"Temp");
  display.drawString(50,30, String(Temperature));
  display.drawString(0,20, "Alt");
  display.drawString(50,20, String(Altitude));
  display.drawString(0,40,"Press");
  display.drawString(50,40, String(Pressure));
  
  display.display();
  delay(100);
  display.clear();

float VBAT = (127.0f/100.0f) * 3.30f * float(analogRead(34)) / 4096.0f;  

Blynk.virtualWrite(V1, Humidity);
Blynk.virtualWrite(V2, Temperature);
Blynk.virtualWrite(V3, Pressure);
Blynk.virtualWrite(V4, Altitude);
Blynk.virtualWrite(V5, VBAT);
}


//Deep Sleep switch
 BLYNK_WRITE(V7)
 {   
   DeepSleep_on = param.asInt(); 
  if (DeepSleep_on == 1)
   {
   display.displayOff();
   led2.on();
   Blynk.notify("Deep Sleep");
   esp_deep_sleep_start();
   delay(100);
   }   
   else{
   led2.off();
}
 }

Okay, also with these modifications after deep sleep wake up the system hang up or doesn’t sync with widgets status.
I suppose that may this depend on :

  • the board (i’ll check with another one);
  • non correct procedure before and after wake up from deep sleep;
  • battery voltage dependence (??).

some opinion on that?

Have you Googled this issue?

I did a super quick search and even quicker scan… AKA didn’t really look that hard :blush: so while this may or may not be related… it is just the tip of the iceberg.

Yes I’ve Googled but not with your efficiency :wink:

Thank you in any case.

I come back on this issue after many trials but no good results.

Same results updating Arduino esp32 revision.

I’ve tried to use RTC_DATA_ATTR to collect reboots numbers and after 25 reboots I use esp_restart() to software restart the module but the behavior is the same with two different Wemos ESP32 modules.

Goolge continuing…

Maybe try using some very simple code, and see if the issue is with the ESP32 itself, or maybe a coding issue. Something like: wake up, send a notification and then go back to sleep. If the issue still persists it may be something with the hardware itself. From what I gather about the ESP32s is that they are still hammering out all the kinks. Maybe this is one they are working on, or have yet to discover.

Good suggestion @Toro_Blanco,

I’ve removed BME280 and all libraries (SPI, wire sensor) need for this sensor : no problem after 3 days.
With Google i’ve found this discussion https://github.com/espressif/arduino-esp32/issues/839.
If i understand well there is still some problem in Esp32 with I2C (& SPI?).

1 Like

Spoken too soon…same issue after 3.5 days.

Someone met the same problem?

Update : also using external wake up, after random number of reboot the system hang up.
Same behaviour with three different esp32 model ( all by Bangood).
It seems that esp32 has some problem to manage the wake up after deep sleep…