Blynk with nodemcu DHT11 sensor

Hi, i try to test nodemcu with DHT11 sensor. I connect the sensor in pin 4 (that’s mean pin 2 for the arduino) and test the example only with DHT. Working perfect!!! After try the example from Blynk examples sites with nodemcua and DHT11 and everything working fine except the temperature & humidity results (Failed to read from DHT sensor!). What is wrong with this?



/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  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
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  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
 *************************************************************/

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


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

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

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

#define DHTPIN 2          // 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(V5, h);
  Blynk.virtualWrite(V6, t);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 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);
}

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

You should edit your post so that the code displays correctly…
Blynk%20-%20FTFC

Pete.

many thanks for the tip… about my issue any help??

Pin 2 or GPIO2 is on D4 on a Nodemcu.
Here is a link to the pinout

1 Like

Do you have the DHT sensor connected to the NodeMCU pin labelled D4 (it’s not 100% clear from your description).

The DHT11 is very slow (as well as being very inaccurate), so I’d try taking a reading every 5 seconds as opposed to every second, and see if that helps.

Pete.

I connect the DHT at the nodemcu pin D4 and as i write when i try the DHT example (without Blynk code) everything working perfect!!!

correct!!! i have the correct hardware interconnection because i try DHT example (without Blynk) and working perfect!!!

Sorry, but I didn’t understand from your first post that… anyway, try reuploading the working code (without blynk) to see if it still works or you maybe have a loose connection/wire…

i try reading every 5sec but nothing…

I ment to upload the code without blynk, because it should work. So maybe it’s a loose wire? Regarding the reading frequency it’s fine if you read it once every (1 Hz resolution, DHT22 has 0.5Hz resolution)

@morfeas Are you sure your NodeMCU is even connecting to the Blynk server? Does it show as active in the App? If so does anything show in the display widgets? And finally are they setup properly with the correct vPins and PUSH setting?

yes everything its ok. In the end i read values from the sensor but i lost same data. In widgets i see values for 5sec and after 0 value for 3sec and the same all the time.

timer.setInterval(30000L, sendSensor); // it’s enough, you don’t need to check temp every 5 sec
6 times an hour is more than enough :wink:

So… NOT “nothing” but just inconsistent readings?? Well the DHT11 is a very slow and limited accuracy sensor, so as already suggested, less frequent reading are sufficient.

with this SPECIFIC code from Blynk examples, nothing!!! after many test with other codes inconsistent readings…

Ok, thanks i check it!!!

1 Like

Hey @morfeas I had the same problem and found out that issue was in DHT11 library. It was fixed in 1.3.3 version so you should just update your library and it should work fine.