Problem waking Wemos mini D1 for DeepSleep

Hi. Very new to Blynk, Arduino & coding so please bare with me.

I have built a Water level monitoring system for use with my outside water tanks. It uses an ultrasonic detector and a Wemos Mini D1 for wifi communication. I have added a DeepSleep function to save battery power. The current code sets DeepSleep for 2 minutes and all works well however if I try and extend the DeepSleep time beyond 2 minutes, it seems the Wemos Mini D1 doesn’t want to wake up. I have tried three, five, ten minutes but the system remains offline. Anything up. to 2 minutes works great. What am I doing wrong?

Code listed below (apologies if it is clunky).


// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "XXXXXXX"
#define BLYNK_DEVICE_NAME "House Water tank"
#define BLYNK_AUTH_TOKEN "XXXXXDDXXXX"

// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define trig D5   // Trig pin
#define echo D6 
char auth[] = BLYNK_AUTH_TOKEN;
int depth = 247.5;      //depthof tank
int r = 175.0;           // radius of tank in cm
int volume;                // capacity in liters

unsigned int raw=0;
float volt=0.0;
float bat_percentage=0.0;
int analogInPin  = A0; 

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

BlynkTimer timer;
 
void waterlevel()
{
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  
  digitalWrite(trig, LOW);
  long t = pulseIn(echo, HIGH);
  long cm = t / 29 / 2;
  Serial.println(cm);
  long level= depth-cm;
  if (level<0)
  level=0;
  long vol = level;
  Serial.println(level);
  level = map(level,0,depth-27.5,0,100);
  Blynk.virtualWrite(V0, level);
    long v = ((3.14*(r*r))*(vol)); // formula to calculate volume in cubic cm
  long volume = v/1000; // final capacity in liters
   Serial.println(volume);
   
   Blynk.virtualWrite(V1, volume);
  delay(200);
  
  raw = analogRead(A0);
  
volt=raw/1023.0;
volt=volt*4.2;
 Serial.println(volt);


     
     Blynk.virtualWrite(V2, volt);
  delay(200);

  ESP.deepSleep(120e6);

}


void setup()
{
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(A0, INPUT);
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(10L, waterlevel);

  
}

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




  }

When you’re writing deep sleep code for use with Blynk it’s best to throw away all the normal Blynk rules about code structure, the use of timers etc and take a totally different approach.

Take a read through this topic…

in particular the code in post #17 which solves the issue of the device not going to sleep if it can’t connect to Blynk.

Pete.