ESP8266 + DHT + deepsleep problem

Hi there,
I am running my project successfully with only one problem. I have ESP8266-12e NodeMCU Dev kit with DHT11 connected to Blynk. Everything is working well, but I added esp.deepsleep into code as I plan to run it on battery (now still powered from wall socket). In general also after deepsleep was implemented project is running OK, but several times when I checked the Blynk application I have seen in my Gauge no values (just ----). After another deepsleep cycle values were back.
So my question is, what could time to time cause values are not present? ESP goes to deepsleep sooner than values are sent to server? should I add some delay() before esp.deepsleep is called (or after?)? Or is there any other problem I amnot aware but maybe somebody from here knows?

Here is my code if it could help

/**************************************************************
 * Blynk is a platform with iOS and Android apps to control
 * Arduino, Raspberry Pi and the likes over the Internet.
 * You can easily build graphic interfaces for all your
 * projects by simply dragging and dropping widgets.
 *
 *   Downloads, docs, tutorials: http://www.blynk.cc
 *   Blynk community:            http://community.blynk.cc
 *   Social networks:            http://www.fb.com/blynkapp
 *                               http://twitter.com/blynk_app
 *
 * Blynk library is licensed under MIT license
 * 
 *
 **************************************************************
 * 
 * 
 *
 * WARNING :
 * For this example you'll need SimpleTimer library:
 *   https://github.com/jfturcot/SimpleTimer
 * Visit this page for more information:
 *   http://playground.arduino.cc/Code/SimpleTimer
 *
 * 
 *   DHT22 ----gpio12
 *
 **************************************************************/

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#define DHTPIN 12 //pin gpio 12 in sensor
#define DHTTYPE DHT11   // DHT 22 Change this if you have a DHT11
DHT dht(DHTPIN, DHTTYPE);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxx";  // Put your Auth Token here. (see Step 3 above)

SimpleTimer timer;

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
   Blynk.begin(auth, "SSID", "PWD"); //insert here your SSID and password
 
  // Setup a function to be called every second
  timer.setInterval(1000L, sendUptime);
}

void sendUptime()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
   //Read the Temp and Humidity from DHT
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  
  Blynk.virtualWrite(12, t); // virtual pin 
  Blynk.virtualWrite(13, h); // virtual pin
  ESP.deepSleep(300000000);
}

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

Many thanks for your help

@helpfinder I know you want to sleep as quickly as possible but some temperature sensors can take a long time to get their readings. Maybe change 1000L to 2000L and with trial and error add some delay() before the deepSleep call.

1 Like

Thanks for reply Costas. I can try also your suggestion with setting timer.setInterval to 2000 and delay().
What came to my mind is I am not using 10k Ohm resistor to connect Vcc and Data pin of DHT11 and I am not sure if this could be also the case or not. Maybe somebody has experience with this?

The DHT11 is notoriously slow… I would add it’s own timer and increase the reading cycle to 5-6 seconds, and give it that time to “calibrate” or whatever it needs, after awakening, before calling the sensor.

As for the resistor, it depends on if you are using the bare sensor, which may need it:

or the sensor mounted on a board, which already has it:

i have never used a resistor on a bare sensor. I can report many happy DHT22’s operating without issue on ESP8266-12’s, on upwards of 15m of Cat5E cable…

(but i only use BME280’s on my deep sleepers)

The DHT22 is a much better sensor then the DHT11

i was trying to help the OP regarding the resistor.

1 Like

I appreciate everyones help, suggestions. Thank you guys.
DHT22 are already ordered from eBay, in the meantime I just want to figure out where is the problem (in case it will occur also with DHT22).
Now I am thinking I will modify the code so temperature will be printed also to Serial monitor if I can see sometimes there is no proper value printed out. (Hopefully I am able to add this to the code, because I am more enthuziasist than coder :))
In case I am able to see these no values I can test with adding resistor if something is gonna change, and also afterwards play with delay() in the code before ESP goes to sleep

you definitely need longer time for the sensor (and it’s library) to read, create, and send the data…

Missing dht.begin(); in setup. You can try in sendUptime.

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
   Blynk.begin(auth, "SSID", "PWD"); //insert here your SSID and password
   dht.begin();
  // Setup a function to be called every second
  timer.setInterval(1000L, sendUptime);
}