With deep sleep function the board don't get online again

Hello guys.
I have a problem with my Blynk project that i made for my friend and his bees. All sensor are working well and also with the blynk app it goes well but if i put the deep sleep function in the sketch for battery consumption saving the board get online only at the beginning, then goes offline and never gets online again. When i had on the beginning of the project the sketch only with the voltage sensor and a DHT sensor, the deep sleep funktion was running well but when i put all my sensors in the sketch, the deep sleep is not running and the board is then still offline.
I have googled for hours but not solved this problem. I have tried the deep sleep function also in the void setup section but nothing works.
Can ou please help me? I’m a beginner with programming so i need help from you experts :slight_smile:

Thank you very much

I use a Wemos D1 mini pro board.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <SoftwareSerial.h>
#include <SimpleTimer.h>
#include <HX711.h>
#include <Adafruit_ADS1015.h>

// _________________________________ //

//ADS1115:

Adafruit_ADS1115 ads;

float Voltage0 = 0.0;
float Voltage1 = 0.0;
float Voltage2 = 0.0;
float Voltage3 = 0.0;

//HX711 scale:

const int SCALE_DOUT_PIN = D7;
const int SCALE_SCK_PIN = D8;
HX711 scale(SCALE_DOUT_PIN, SCALE_SCK_PIN);

float weight;
float calibration_factor = -21000; 

//Voltage sensor:

float Vsensor = A0;  
float vout = 0.00; 
float vin = 0.00; 
float R1 = 30000;     
float R2 = 7500;   
float value = 0.00; 

//Light sensor:

int Lsensor;
int light;
int brightness;

//Temperature and humidity sensors:

#define DHT1PIN D3  
#define DHT2PIN D4

#define DHT1TYPE DHT22     
#define DHT2TYPE DHT11   

DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);

//Blynk app:

int sendingtime = 1000L;
char auth[] = "xxxxxxxx";
char ssid[] = "xxxxxxxx";
char pass[] = "xxxxxxxx";

#define BLYNK_PRINT Serial
BlynkTimer timer;

// ________________________________ //


void sendDHT()
{
  float h1 = dht1.readHumidity();
  float t1 = dht1.readTemperature(); 

  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature(); 
  
  Blynk.virtualWrite(V1, h1);
  Blynk.virtualWrite(V2, t1);
  Blynk.virtualWrite(V7, h2);
  Blynk.virtualWrite(V6, t2);
}

void sendWeight()
{
  
  weight = (scale.get_units(20));
  weight = weight - 23.60;
  Blynk.virtualWrite(V4, weight);

}
  
void sendAnalogs()
{
  int16_t adc0, adc1, adc2, adc3;

  adc0 = ads.readADC_SingleEnded(0);
  adc1 = ads.readADC_SingleEnded(1);
  adc2 = ads.readADC_SingleEnded(2);
  adc3 = ads.readADC_SingleEnded(3);
  
  Voltage0 = (adc0 * 0.125)/1000;
  Voltage1 = (adc1 * 0.125)/1000; 
  Voltage2 = (adc2 * 0.125)/10;
  Voltage3 = (adc3 * 0.125)/1000;

  Voltage3 = 4.1 - Voltage3;
    
float sensorVal       = 0; 
float smoothedVal     = 0;    
int samples           = 10;    

sensorVal = Voltage2;
smoothedVal = smoothedVal + ((Voltage2 - smoothedVal)/samples);

smoothedVal = map(smoothedVal, 10, 50, 100, 0);  


  Blynk.virtualWrite(V10, Voltage0);
  Blynk.virtualWrite(V11, Voltage1);
  Blynk.virtualWrite(V12, smoothedVal);
  Blynk.virtualWrite(V13, Voltage3);  

}

void sendVoltage()
{

float sdata = 0; 
   
   value = analogRead(Vsensor); 
   vout = (value * 5.0) / 1640.0; 
   vin = vout / (R2/(R1+R2));
 
   Serial.print("INPUT V= "); 
   Serial.println(vin,4); 
   sdata = vin; 
   Blynk.virtualWrite(V3, sdata);

}

void goToSleep(){

   ESP.deepSleep(5 * 3 * 1000000); //sleep for 15 seconds


}

void setup()
{
  
  Serial.begin(9600);

  pinMode(Vsensor, INPUT);

  Blynk.begin(auth, ssid, pass);
  
  scale.set_scale(calibration_factor);
  //scale.tare();
  
  dht1.begin();
  dht2.begin();

  ads.setGain(GAIN_ONE);
  ads.begin();

  
  timer.setInterval(sendingtime, sendDHT);
  timer.setInterval(sendingtime, sendVoltage);
  timer.setInterval(sendingtime, sendWeight);
  timer.setInterval(sendingtime, sendAnalogs);
  timer.setInterval(5000L, goToSleep);

}


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

Hi,

You are running gotoSleep() function every 5 seconds and this function make esp sleep for 15 seconds, so ESP will always to be sleeping, isn’t it??

Regards,
Gaurav Barwalia

Hello.
Thank You for the answer. Yes the math doesn’t lie

but this was my last try…i have tryed also the function ESP.deepSleep(15e6) (or more second) in the void setup section or in a void sensor section without calling the sleep every time with the gotoSleep() function and it also don’t worked…only at the beginning for one time the board was online, send the values and than never was online again

First of all, do you have pin D0 connected to the RST pin? If not then it will never wake up.

Secondly, when you write Blynk code for deep sleep all the normal Blynk rules go out of the window. You SHOULD put everything in the void loop and you SHOULDNT use timers.

I did quite a bit of work with @christophebl on his beehive project, which is summarised here:

It’s also worth noting that Blynk.begin is a blocking function and if your device can’t connect to WiFi or Blynk then it will keep trying until the battery goes flat.
@christophebl’s code solves that by using Blynk.connect and restricting the number of attempts before forcing the device to sleep.

Pete.

1 Like

Hello Pete.
Yes the pins are connected and all works with one or two sensors but with all it don’t work. Thank you for the advice and the link of the other topic. I will read it and try to solve the problem.

Then i give a report if it works.

thank you very much

Jan

Hello Friends.
So i have tried rewrite my sketch with the help from Pete and the project from his link and the deep sleep mode was now working well. But i noticed that the board doesnt schowed that it is connected to the internet (but it was) and was counting the connecting attempts sometimes to the limit and sometimes write that it is connected, sometimes not and goes to sleep again.

So i needed to change the contition from:

if (WiFi.status() != WL_CONNECTED)

to the contidion:

if (WiFi.localIP().isSet())

i have also leave the timers in the sketch and now all works well without a problem…it is always connected after the ESP wakes up

So, many thanks to You Pete for your help :slight_smile:

Best regards
Jan

1 Like

That’s interesting. Glad you found something that works.

My father was a beekeeper for many years, a long time before IoT was a thing, which is one of the reasons I worked with @christophebl to get his system working.

Pete.