DHT issue with NodeMCU

I am running into what looks like a common problem getting a DHT sensor to read with a NodeMCU. It appears to have something to do with how DHT is being named in the code but i have tried a couple different solutions i saw online and none seem to be working. Here is the error:

Arduino: 1.8.6 (Windows 10), TD: 1.45, Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Disabled, 4M (no SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

sketch_mar15b:8:5: error: 'dht' does not name a type

     dht DHT(DHT11_PIN, DHTTYPE);

     ^

C:\Users\ryan\Documents\Arduino\sketch_mar15b\sketch_mar15b.ino: In function 'void loop()':

sketch_mar15b:41:22: error: expected primary-expression before '.' token

         int chk = DHT.read11(DHT11_PIN);

                      ^

sketch_mar15b:43:25: error: expected primary-expression before '.' token

       Serial.println(DHT.temperature);

                         ^

sketch_mar15b:45:25: error: expected primary-expression before '.' token

       Serial.println(DHT.humidity);

                         ^

sketch_mar15b:56:7: error: 'else' without a previous 'if'

       else

       ^

exit status 1
'dht' does not name a type

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Here is the full code:

// Henry's Bench
//Checking to ensure you can connect ESP-12E to a router
     
    #include <ESP8266WiFi.h>
    #include <DHT.h>
    #define DHT11_PIN 2
    #define DHTTYPE DHT11
    dht DHT(DHT11_PIN, DHTTYPE);
    float humidity, temp_f; // Values read from
    
    const char* ssid     = "Living Room G";
    const char* password = "apassword";     

    int wifiStatus;
     
    void setup() {
      
      Serial.begin(115200);\
      delay(200);
      
     
     
      // We start by connecting to a WiFi network
     
      Serial.println();
      Serial.println();
      Serial.print("Your are connecting to;");
      Serial.println(ssid);
      
      WiFi.begin(ssid, password);
      
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
     
 }   
     
void loop() {

        int chk = DHT.read11(DHT11_PIN);
      Serial.print("Temperature = ");
      Serial.println(DHT.temperature);
      Serial.print("Humidity = ");
      Serial.println(DHT.humidity);
      delay(1000);
  
      wifiStatus = WiFi.status();

      if(wifiStatus == WL_CONNECTED)
         Serial.println("");
         Serial.println("Your ESP is connected!");  
         Serial.println("Your IP address is: ");
         Serial.println(WiFi.localIP());  
      
      else
        Serial.println("");
        Serial.println("WiFi not connected");
      
      delay(1000); // check for connection every once a second

}

NVM! Figured it out.

Please share, might help someone else in the future.

The code seems fine, curious to know what caused the issue ?

jackthom41:
The code seems fine, curious to know what caused the issue ?

Maybe forgot to add the DHT library, or didn't add it properly .... the .h and .cpp files.

I think I may know the answer to why it didn't work because I may have run into the same problem a couple weeks back. I also tried to make a DHT sensor setup with a nodemcu. I included the library and all....everything seemed to look fine based on the tutorial I used as a guide. Problem was the include file. I uninstalled the latest adafruit dht library and reinstalled a earlier version. BOOM...worked on first upload after doing this dialing back the file.

I'm calling for ban on all members who dont come back and share their answers/solutions!!! LOL

:slight_smile:

Crap! I apologize guys for flaking on this thread. I’ve been completely wrapped up in building all of this out once I figured out this issues I was on to the next thing! As someone else previously mentioned it was an issue with the naming conventions of the DHT.h file. I too had to revert back to an older version of the library to make it work.

My original attempts at following some guides online ended up being backwards in the way they showed DHT.h being declared in the code and it took me a couple attempts to finally sort out the root of the issue. Hopefully that will help someone else who might run into this!