No Data since adding deep sleep to code

Hi
Without the deep sleep code the program works great, as soon as I try the deep sleep version I do not get any data from the DHT22. The ESP32 goes to sleep & wakes up fine. Please advise

/*************************************************************

  This example shows how value can be pushed from Arduino to
  the Blynk App.

  WARNING :
  For this example you'll need Adafruit DHT sensor libraries:
    https://github.com/adafruit/Adafruit_Sensor
    https://github.com/adafruit/DHT-sensor-library

  App project setup:
    Value Display widget attached to V5
    Value Display widget attached to V6
 *************************************************************/
// 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 "TMPLDpJzlkfM"
#define BLYNK_DEVICE_NAME "ESP32"
#define BLYNK_AUTH_TOKEN "_0B2R4U-N-q9u44azYzL6dJbm6EKZzFm";
#define uS_TO_S_FACTOR 100000
#define TIME_TO_SLEEP 300 

// Comment this out to disable prints and save space
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <DHT_U.h>

char auth[] = BLYNK_AUTH_TOKEN;

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

#define DHTPIN 4          // What digital pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11     // DHT 11
#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)){
    Serial.println("Failed to read from DHT sensor!");
  return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V6, h);
  Blynk.virtualWrite(V5, t);
}


void setup()
{
 

  // Debug console
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
  Blynk.run();
  timer.run();
  

  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + " Seconds");

  Serial.println("Going to sleep now");
  delay(100);
  Serial.flush();
  esp_deep_sleep_start();
  Serial.println("This message will never be printed");

}

void loop() {

Please provide what you are seeing on the serial monitor before and after deep sleep :zzz:

There’s no point in having a timer when using deep sleep.

You should put a call to sendSensor() and this code…

inside a BLYNK_CONNECTED() function and add some more Blynk.run() commands before sending the device to sleep.

Pete.

Thank you Pete for your prompt reply
I think I did as you suggested but sadly still information on the dashboard.
It will be an achievement when it is working for sure.


  This example shows how value can be pushed from Arduino to
  the Blynk App.

  WARNING :
  For this example you'll need Adafruit DHT sensor libraries:
    https://github.com/adafruit/Adafruit_Sensor
    https://github.com/adafruit/DHT-sensor-/';

  App project setup:
    Value Display widget attached to V5
    Value Display widget attached to V6
 *************************************************************/
// 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 "TMPLDpJzlkfM"
#define BLYNK_DEVICE_NAME "ESP32"
#define BLYNK_AUTH_TOKEN "_0B2R4U-N-q9u44azYzL6dJbm6EKZzFm";
#define uS_TO_S_FACTOR 100000
#define TIME_TO_SLEEP 300

// Comment this out to disable prints and save space
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <DHT_U.h>

char auth[] = BLYNK_AUTH_TOKEN;

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

#define DHTPIN 4          // What digital pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11     // DHT 11
#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);


// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V6, h);
  Blynk.virtualWrite(V5, t);
}


void setup()
{


  // Debug console
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  dht.begin();

  // Setup a function to be called every second
}

BLYNK_CONNECTED() {

  sendSensor();
  
  Blynk.run();
  

  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + " Seconds");

  Serial.println("Going to sleep now");
  
  delay(100);
  Serial.flush();
  esp_deep_sleep_start();
  Serial.println("This message will never be printed");

}

void loop() {


}

Pete.

The temperature is 10 degrees too low & the humidity shows 100%
At least I have some data now though.

For what it’s worth …

I would place the dht.begin() before the blynk.begin(…).

I am not sure how the “BLYNK_CONNECTED” is called, technically. As a callback from blynk.begin or some kind of interrupt. (I probably should know, but I dont…)
Anyway, placing the dht.begin before, ensures it is executed fully before the SendSensor (which include reading the sensor) is executed. To avoid risking you are trying to read an unitialized sensor.
(This might be unnecessary. I am not sure, but placing before would be safer. In my opinion.)

Another thing may be that you are reading to soon after initialising. I dont remember, but the DHT sensor might need a short time to pass after init until first stable reading can be done. (I could be wrong.) But if you experience “unstable” readings you could try a “delay (1000)” after the dht.begin and before blynk.begin.
As said, I am not sure, but could be a simple thing to test.

Blynk.connected is a callback from the Blynk library, which I think is triggered when Blynk.run is executed and the status has changed from disconnected to connected.

I think some serial prints would be very handy to help diagnose what’s happening.

Pete.

This is interesting, I now have data but it is not going to sleep, since the addition of the delay.

Thank you for your help.
Your advice fixed my issue.
Many thanks