DHT11 and DeepSleep not working

Hi friends. I build the sketch to showing temp and humidity in deep sleep mode but values can not be show in blynk widget.

the sketch:

#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 4 //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[] = "98a6b5fcc0bc4ddaac40679db7d5492e";  // Put your Auth Token here. (see Step 3 above)
char ssid[] = "MikroTik Home";
char pass[] = "12345678";
char server[] = "10.5.51.3";

SimpleTimer timer;

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass, server, 8080); //insert here your SSID and password
 
  // Setup a function to be called every second
  timer.setInterval(2000L, 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(10, t); // virtual pin 
  Blynk.virtualWrite(11, h); // virtual pin
  ESP.deepSleep(10000000);
  delay(100);
}
void loop()
{
  Blynk.run();
  timer.run();
}

I guess it should be Vpin

Blynk.virtualWrite(V10, t); 
Blynk.virtualWrite(V11, h);

At least I always use Vpin, not only the pin.

1 Like

Maybe it’s not going into deep sleep but just taking a nap :wink:
I’d run the code without the deep_sleep function and following psoro recommendation to see if it’s working and then, make the changes.
This maybe of interest to you.

Hi, what type of ESP8266 you are using? Keep in mind that esp01 need a little hack to make deep sleep to work. Search this forum for a post talking about deep sleep an a dht during almost 6 months posted by my to know more about that. I’m now at cell and I can’t post the correct link easily…

1 Like

I using nodemcu and shorting rst pin to gpio 16 (D0). The sleep is working good but the value cannot be display on blynk app. when I remove below code from my sketch the values showing correctly

ESP.deepSleep(10000000);
  delay(100);

I solved the problem with below sketch:

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
 #include <ESP8266WiFi.h>
 #include <BlynkSimpleEsp8266.h>
 #include <DHT.h>
 #define DHTPIN 4 //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[] = "98a6b5fcc0bc4ddaac40679db7d5492e";  // Put your Auth Token here. (see Step 3 above)
 char ssid[] = "MikroTik Home";
 char pass[] = "12345678";
 char server[] = "10.5.51.3";
 
 
 void setup()
 {
   Serial.begin(115200); // See the connection status in Serial Monitor
   Blynk.begin(auth, ssid, pass, server, 8080);
   dht.begin();
 }




 void loop()
 {
   Blynk.run();
   Send();
 }
 
 void Send(){
   while (Blynk.connect()==false)
   {}
   float h = dht.readHumidity();
   float t = dht.readTemperature();
   Blynk.virtualWrite(11, t); // virtual pin 
   Blynk.virtualWrite(12, h); // virtual pin
   delay(1000);
   ESP.deepSleep(20000000);
 }

The code was correct there… because…
V0=0
V1=1
V127=127

2 Likes

Well you found the problem and fix it… it is perfect…
Maybe you didn’t know that use ESP.deepSleep stop all of the esp (including program execution) except the internal clock, and after the timeout it will reset the esp without execute any other line… if you put something after ESP.deepSleep… it never will be executed. (self experience with try and error)

I rewrite a little your code to ensure the dht have read values correctly for avoid to send a NAN value to Blynk server…

I used tasker for check the sensor every 500ms until get correct values… if esp is connected to blynk server it will send the values and then, it will go to sleep. if no connection to the server exists it will go sleep anyway.

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <Tasker.h> //https://github.com/joysfera/arduino-tasker

 #define DHTPIN 4 //pin gpio 12 in sensor
 #define DHTTYPE DHT11   // DHT 22 Change this if you have a DHT11
 DHT dht(DHTPIN, DHTTYPE);
 Tasker Task(true);

 // You should get Auth Token in the Blynk App.
 // Go to the Project Settings (nut icon).
 char auth[] = "98a6b5fcc0bc4ddaac40679db7d5492e";  // Put your Auth Token here. (see Step 3 above)
 char ssid[] = "MikroTik Home";
 char pass[] = "12345678";
 char server[] = "10.5.51.3";

void readDHT()
{
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  if (isnan(t) || isnan(h)) return;
  if (Blynk.connected())
  {
    Blynk.virtualWrite(V2, h); // virtual pin
    Blynk.virtualWrite(V0, t); // virtual pin 
    Serial.println(t);
    Serial.println(h);
  }
  ESP.deepSleep(20000000);
}
void setup()
{
  dht.begin();
  Serial.begin(115200); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass, server, 8080);
  Task.setInterval(readDHT,500);
}
void loop()
{
  Blynk.run();
  Task.loop();
}
1 Like

Hello ! I’m facing an issue with your code, it seems that esp8266 doesn’t wake up from sleep because i can’t see it connecting to server and printing value after the first loop. Maybe you can help ?

  ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on ESP8266

[4468] Connecting to blynk-cloud.com:80
[5597] Ready (ping: 365ms).
21.50
41.00
rll⸮⸮|⸮l⸮|⸮l⸮b|⸮⸮⸮⸮r⸮bl⸮b⸮⸮no⸮lon⸮⸮⸮cp⸮⸮lrlrl⸮s⸮o⸮b⸮obp⸮

Here is what i’ve edit the server part :

Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);

Do you have a connection between GPIO16 (pin D0) and GND?

Of not then your NodeMCU will never wake up from deep sleep.

Pete.

1 Like