Deep sleep with battery

Hi all,
I’m very new to the whole work of IOT and Bynk but really enjoying getting into some projects. I have a problem with a recent moisture sensor I have built and designed to run off a battery with solar charging. I have implemented deep sleep into the sketch and this is where the problems started. No problems when I was running off a 5V supply but as soon as I ran off the battery, blynk upload became very hit and miss. Sometimes I got nothing, other times everything.
I have seen a few references to this topic on the forum but nothing that has been suggested has worked. I got a sense from some comments that uploading to blynk with deep sleep is problematic and wonder if anyone can enlighten me. Is there something in the coding I need to pay attention to (My coding skills are not far off zero). I have attached my sketch below.
What I have tried

  1. Introduce a delay before and after the deep sleep command
  2. Use the blynk sketch builder with nothing in the loop except the blynk run and time run commands. Main processing done in the void mytimer
  3. Used the blynk simple timer

Cheers
Steve

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


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "Filter.h"
#include <TimeLib.h>
#include <WidgetRTC.h>


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "my blynk key";

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

int sensorPin = A0;
int power = 14;  //Set pin to power sensor
int saturated = 600;  //Set value at saturated
int dry = 400; //Set value at dry
int sensorValue = 0;
int Percentmoisture = 0;




BlynkTimer timer;

WidgetRTC rtc;


 BLYNK_CONNECTED() {
  // Synchronize time on connection
  rtc.begin();
  }

void myTimerEvent()
{
 // Check wifi signal strength
long rssi = WiFi.RSSI();
  Serial.print("RSSI:");
  Serial.println(rssi);
  

 digitalWrite(power, HIGH);  // Turn on sensor
 delay(1000);
 sensorValue = analogRead(sensorPin);
 
 if (sensorValue < dry) Percentmoisture = 0;
 else if (sensorValue > saturated) Percentmoisture = 100;
 else Percentmoisture = (sensorValue - dry) * 100 / (saturated - dry); //Calculate percentage moisture
 
 Serial.print("Raw sensor value");
 Serial.println(sensorValue);
 Serial.print("Percent moisture");
 Serial.println(Percentmoisture);

 // Get time from RTC widget
 // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details
  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();

  // Send data to Blynk
  Blynk.virtualWrite(V1, currentTime); //Send time to blynk 
  Blynk.virtualWrite(V2, currentDate); //Send date to blynk 
  Blynk.virtualWrite(V3, sensorValue); //Send sensor value to blynk 
  Blynk.virtualWrite(V4, rssi); //Send wifi signal strength to blynk
  Blynk.virtualWrite(V5, Percentmoisture);  //Send percentage moisture to blynk
  Blynk.virtualWrite(V6, Percentmoisture);  //Send percentage moisture to blynk
  
  digitalWrite(power, LOW); // turn off sensor power
  
 
 Serial.println("Going into deep sleep");
 delay(100);
 ESP.deepSleep(3.9e9); // deepSleep time is defined in microseconds
 delay(100);  
}

void setup()
{
  // Debug console
  Serial.begin(115200);
  
  pinMode(power, OUTPUT); // Define power pin as output
  
  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
   
  // Setup a function to be called every second
  timer.setInterval(10000L, myTimerEvent);
  
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

I have never used sleep on my ESPs, so can’t totally help there… but I do believe that you will need to rethink your timing strategy as I believe the interval timer will stop processing while in sleep, thus skewing the schedule.

And you really should avoid delay() over a few ms in your code as it blocks ALL processing.

in deep sleep ONLY the RTC is working, the CPU is off: so no processing. I’d suggest that you have a look at the simpletimer library (in the code) to see how it works. If its linked to the RTC then it should work, if it uses its own timer then as Gunner said: it won’t .

edit: why are you using a timer event in the first place? form what I see is: you do a measurement then turn off everything for an ~hour and the everything back on. I would suggest to simply place the timerEvent inside the BLYNK_CONNECTED routine, turning the deepsleep effectively into the timer.

Many thanks for the feedback - much appreciated. Unfortunately I’m not much further advanced. You were spot on wolph42 that I don’t need the timer so I removed it and put the myTimerEvent function into the BLYNK_CONNECTED function (CODE ATTACHED). Still not getting updates to Blynk and the date now reads 1970 so the time is not syncing properly any longer. I thought BLYNK_CONNECTED wouldn’t execute until the connection was established so not sure what is going on here. In desparation I put all of the code into the setup function and got the same result.

Any other thoughts welcome.

Cheers
Steve

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


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WidgetRTC.h>


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

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

int sensorPin = A0;
int power = 14;  //Set pin to power sensor
int saturated = 600;  //Set value at saturated
int dry = 400; //Set value at dry
int sensorValue = 0;
int Percentmoisture = 0;






WidgetRTC rtc;


 BLYNK_CONNECTED() {
  // Synchronize time on connection
  rtc.begin();
  myTimerEvent();
  }

void myTimerEvent()
{
 // Check wifi signal strength
long rssi = WiFi.RSSI();
  Serial.print("RSSI:");
  Serial.println(rssi);
  

 digitalWrite(power, HIGH);  // Turn on sensor
 delay(1000);
 sensorValue = analogRead(sensorPin);
 
 if (sensorValue < dry) Percentmoisture = 0;
 else if (sensorValue > saturated) Percentmoisture = 100;
 else Percentmoisture = (sensorValue - dry) * 100 / (saturated - dry); //Calculate percentage moisture
 
 Serial.print("Raw sensor value");
 Serial.println(sensorValue);
 Serial.print("Percent moisture");
 Serial.println(Percentmoisture);

 // Get time from RTC widget
 // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details
  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();

  // Send data to Blynk
  Blynk.virtualWrite(V1, currentTime); //Send time to blynk 
  Blynk.virtualWrite(V2, currentDate); //Send date to blynk 
  Blynk.virtualWrite(V3, sensorValue); //Send sensor value to blynk 
  Blynk.virtualWrite(V4, rssi); //Send wifi signal strength to blynk
  Blynk.virtualWrite(V5, Percentmoisture);  //Send percentage moisture to blynk
  Blynk.virtualWrite(V6, Percentmoisture);  //Send percentage moisture to blynk
  
  digitalWrite(power, LOW); // turn off sensor power
  
 
 Serial.println("Going into deep sleep");
 delay(100);
 ESP.deepSleep(3.9e9); // deepSleep time is defined in microseconds
 delay(100);  
}

void setup()
{
  // Debug console
  Serial.begin(115200);
  
  pinMode(power, OUTPUT); // Define power pin as output
  
  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
   
  
  
}

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

You still want this in the setup

Thanks Gunner. Do you mean just include this line in setup (after Blynk.begin) as well as in BLYNK_CONNECTED). I tried this but no luck on the time and date. Still showing 1970.

Cheers

Just an update - it seems to be working but not exactly sure what I did. The only change I made was to split the BLYNK_WRITE block I had where I was sending all the data to Blynk and split it so that I wrote the data directly after it was calculated.

Thanks for your help.

Have you had any luck? I’ve have similar issue with deep sleep. Some data will come across and other times it will only be a sensor or two that updates in blynk. I can’t find a pattern…

hi @m_roberts001
Sorry I’ve just come across your post. I gave up on the deep sleep bit as, like you, I couldn’t find a pattern. Found a way to mains power my device so can keep it on all the time. In saying this, if I were to try again, there is a piece of code I didn’t have in my sketch relating to waiting until Blynk is connected before continuing. I’m hypothesising that the sketch was running through and going back to sleep before Blynk had connected but on other occasions it did connect in time and the sensor was read. The code is

while (Blynk.connect() == false)  //wait until Blynk is connected before proceeding
  {
  }

This is probably pretty basic for experienced coders but I didn’t have it. It is now part of every sketch. One day, I might get back to trying it again. If you have this already, don’t think I’m much help I’m afraid.

Cheers
Steve