NodeMCU Arduino IDE with DHT11 sensor

Noob question: I want to read DHT11 temperature data on a NodeMCU module. I'm using Arduino IDE for programming the NodeMCU. From where do I download the DHT.h / DHT11.h file / libraries for reading the temperature data for this setting?

Some standard Arduino libraries work fine with ESP8266 (the microcontroller on your NodeMCU module). Others are written for a specific architecture and will not work on ESP8266. So I recommend trying the most popular DHT11 libraries, then if they don't work do a search for DHT11 ESP8266 and you will find libraries written specifically for ESP8266.

Ok. Thanks v.much for your advise. I will try to use the popular DHT11 libraries first, and post the results here if they are working. Thanks again.

The adafruit libs work with my ESP's 01 to MCU.

Also make sure you get all the prefs right for the node MCU.

Took me a couple of tries to work out they are not all the same. Use the flash config check sketch first is your best bet from the wifi examples for ESP's.

Thanks v.much Ballscrewbob for your advise and help.

The Adafruit DHT libraries are working nicely. I have a problem though. The humidity values are changing properly, but the temperature is showing constant 30.00 *C (not even a fluctuation of +/-0.01). Could this be because of a defective sensor? Also, I'm directly connecting the sensor output to the D5 pin of NodeMCU (without any pullup resistor). Could this be the reason for the constant value?

I have used them without on many occasions with no issues.

I did get one fail over winter somewhere between minus 40 to minus 45 degrees celsius but the MKR it was connected to survived very well.

You really should post your code so it can be checked properly but ensure you use code tags ( </> )

Also make sure you try just the DHT on its own before adding any other items.
First thing I do when I get a package of parts is to set up a sketch just to check each part on its own that way I can rule out the part as an issue and know it is almost always with either the way I wired it or my crappy code skills.

I have a couple of node MCU’s and other ESP boards here and a selection of DHT’s so it would not take much to check out your code.

Update: The temperature IS changing but barely 1 degree during the whole day, even though Google is showing 3-4 degrees temperature fluctuation for my city during the same period.

I have another question regarding the same. Here’s my code. I’m sending the temperature and humidity data every 30 mins via IFTTT to my Twitter DM.

#include <ESP8266WiFi.h>
#include <DHT.h>
#include "DataToMaker.h"

#define SSID_NAME "XXXXXXX"
#define SSID_PASSWORD "XXXXXXX"

#define IFTTT_EVENT "temperature_changed"
#define IFTTT_KEY "XXXXXXX"

DataToMaker event(IFTTT_KEY, IFTTT_EVENT);

#define DHTPIN D1
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  dht.begin();

  pinMode(BUILTIN_LED, OUTPUT);
  digitalWrite(BUILTIN_LED, HIGH);
}

void loop() {
  double currentTemperature = dht.readTemperature();
  double currentHumidity = dht.readHumidity();
  
    digitalWrite(BUILTIN_LED, LOW);

    WiFi.mode(WIFI_STA);
    WiFi.begin(SSID_NAME, SSID_PASSWORD);

    while (WiFi.status() != WL_CONNECTED) 
      delay(500);

    event.setValue(1, String(currentTemperature));
    event.setValue(2, String(currentHumidity));
    
    if(event.connect())
      event.post();
    
    WiFi.disconnect();
    WiFi.mode(WIFI_OFF);

    digitalWrite(BUILTIN_LED, HIGH);

    delay(30L*60L*1000L);
}

The DataToMaker.h file (not my code) can be found here.

Here is a screenshot of my Twitter DM:

As you can see, both temperature and humidity are being displayed as 30.00 *C and 80.00% etc (00 after the decimal point). Is this normal? Doesn’t the DTH11 sensor return fractional values?

I also have another simpler sketch (code given below) which prints the same thing to the Serial Monitor, 00 after decimal point. No fractional values.

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

#define DHTPIN D1
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  dht.begin();

  pinMode(BUILTIN_LED, OUTPUT);
  digitalWrite(BUILTIN_LED, HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:
    digitalWrite(BUILTIN_LED, LOW);

    Serial.print("Temperature: ");
    Serial.print(dht.readTemperature());
    Serial.print(" *C\t");

    Serial.print("Humidity: ");
    Serial.print(dht.readHumidity());
    Serial.println(" %");

    digitalWrite(BUILTIN_LED, HIGH);

    delay(3000);
}

Yes they can do fractional as can be seen HERE

It all depends on what maths is used in the sketch itself. Also the ability of the other end to receive decimals and handle them.

Wont get to this until a little later.

No problem. Thanks v.much for your help Ballscrewbob. I'll try the simpler sketch in the Serial Monitor again to see if I can get the fractional values. Thanks again.

I found this article on the net.

It says:

The 40 data bits are organized the following way:

  • 16 bits of humidity data, with MSB (Most Significant Bit) first. The first 8 bits are for the integral part, and the second 8 bits for the fractional part. For the DHT11 the fractional bits are always zero.
  • 16 bits of temperature data: as for the humidity, there are 8bit of integral and 8bit of fractional data, and the fractional data always zero for the DHT11.
  • 8 bits of checksum, which is the sum of the preceding four bytes.

Maybe that's the way for some DHT11 modules?

You may be correct as some people do seem to get decimal accuracy with the same sensor.

I double checked the one I linked to and it is a DHT11. It says so stamped on the side.

But as you say a lot of the data sheets refer to single degree.

I guess so. I must have got one which refers to a single degree. Doesn't matter. It was just for experimentation anyway. Thanks v. much again for your help on this Ballscrewbob.