+ Post New Thread
Results 1 to 4 of 4
  1. #1
    Member level 2
    Points: 408, Level: 4

    Join Date
    Oct 2017
    Posts
    51
    Helped
    0 / 0
    Points
    408
    Level
    4

    NodeMCu Interface with DHT11

    I am testing NodeMCu Interface with DHT11 unit. I have used arduino uno board where i am getting proper reading for temp & humidity.
    I have attached output Serial for reference.
    Code:
    #include "dht.h"
    
    dht DHT;
    
    #define DHT11_PIN 7
    
    void setup(){
      Serial.begin(115200);
    }
    
    void loop()
    {
      int chk = DHT.read11(DHT11_PIN);
      Serial.print("Temperature = ");
      Serial.println(DHT.temperature);
      Serial.print("Humidity = ");
      Serial.println(DHT.humidity);
      delay(1000);
    }
    Now I am testing same code using NodeMCu with SimpleDHT11 library. But Here i wont get Reading from DHT sensor. I tried Power 3.3v with seprate regulator with common ground. But result are same

    Code without library . I get an timeout error.

    Code:
    
    // DHT11 and DHT22 sensor reading demo
    // by 'jurs' for Arduino Forum
    
    // DHT functions enumerated
    enum {DHT11_SAMPLE, DHT22_SAMPLE, DHT_TEMPERATURE, DHT_HUMIDITY, DHT_DATAPTR};
    
    // Begin of user configuration area
    #define DHT_SAMPLE DHT11_SAMPLE // must be DHT11_SAMPLE or DHT22_SAMPLE
    #define DHT_PIN 2
    // End of user configuration area
    
    // DHT error codes enumerated
    enum {DHT_OK=0, DHT_ERROR_TIMEOUT=-1, DHT_ERROR_CRC=-2, DHT_ERROR_UNKNOWN=-3};
    
    void setup()
    {
      Serial.begin(115200);
      Serial.println("DHT Temperature and Huminity Measurement");
    }
    
    // DHT sensor pinout from left to right looking at the gridded side
    // 1-VCC  2-DATA  3-NC  4-GND
    
    int dhtCall(byte pin, byte function)
    // input parameters are the data pin and one of the DHT functions
    // return value is DHT error code with function DHT11_SAMPLE or DHT22_SAMPLE
    // alsways do sampling with DHT_OK result before calling other functions
    // return value is temperature with function DHT_TEMPERATURE
    // return value is humidity with function DHT_HUMIDITY
    // return value is pointer to byte array containing raw data with function DHT_DATAPTR
    {
      static int temperature=-999;
      static int humidity=-999;
      static byte data[5]; // 5 bytes to receive 40 data bits
      unsigned int loopCnt; // loop counter
      byte sum;  // checksum
      #define DHT_LOOPS 1800
      int triggerTime;
      switch (function)
      {
        case DHT11_SAMPLE: // REQUEST DHT11 SAMPLE
        case DHT22_SAMPLE: // REQUEST DHT22 SAMPLE
          if (function==DHT11_SAMPLE) triggerTime=20000; // 20000µs trigger time for DHT11
          else triggerTime=1000; // 1000µs trigger time for DHT22
          pinMode(pin, OUTPUT); 
          digitalWrite(pin, LOW);
          delayMicroseconds(triggerTime); 
          pinMode(pin,INPUT_PULLUP);
          loopCnt = DHT_LOOPS;
          while(digitalRead(pin) == HIGH) if (loopCnt-- == 0) return DHT_ERROR_TIMEOUT;
          loopCnt = DHT_LOOPS;
          while(digitalRead(pin) == LOW) if (loopCnt-- == 0) return DHT_ERROR_TIMEOUT;
          loopCnt = DHT_LOOPS;
          while(digitalRead(pin) == HIGH) if (loopCnt-- == 0) return DHT_ERROR_TIMEOUT;
          for (byte bitNum=0;bitNum<40;bitNum++) // try reading 40 bits
          {
            loopCnt = DHT_LOOPS;
            while(digitalRead(pin) == LOW) if (loopCnt-- == 0) return DHT_ERROR_TIMEOUT;
            delayMicroseconds(32);
            boolean dhtBit=digitalRead(pin);
            bitWrite(data[bitNum/8],7-bitNum%8,dhtBit);
            loopCnt = DHT_LOOPS;
            while(digitalRead(pin) == HIGH) if (loopCnt-- == 0) return DHT_ERROR_TIMEOUT;
          }
          sum = data[0] + data[1] + data[2] + data[3];  
          if (data[4] != sum) return DHT_ERROR_CRC;
          if (function==DHT11_SAMPLE)
          {
            humidity=data[0];
            temperature=data[2];
          }
          else
          {
            humidity=data[0]*256+data[1];
            temperature= (data[2] & 0x7F) * 256 + data[3];
            if (data[2] & 0x80) temperature= -temperature;
          }
          return DHT_OK;
        case DHT_TEMPERATURE:
          return temperature;
        case DHT_HUMIDITY:  
          return humidity;
        case DHT_DATAPTR:
          return (int)data;
        default:
          return DHT_ERROR_UNKNOWN;
      }  
    }
    
    void loop()
    {
      byte* b; // byte pointer for showing raw data
      switch (dhtCall(DHT_PIN, DHT_SAMPLE)) // always request a sample first
      {
        case DHT_OK: // only if DHT_OK is true, get temperature, humidity and possibly raw data
          Serial.print("Humidity: ");
          Serial.print(dhtCall(DHT_PIN, DHT_HUMIDITY));
          Serial.print("   Temp: ");
          Serial.print(dhtCall(DHT_PIN, DHT_TEMPERATURE));
          Serial.print("     raw data:  ");
          b=(byte*)dhtCall(DHT_PIN, DHT_DATAPTR);
          for (int i=0;i<5;i++)
          {
            Serial.print(b[i]);Serial.print('\t');
          }
          Serial.println();
          break;
        case DHT_ERROR_TIMEOUT:
          Serial.println("Timeout Error");
          break;
        case DHT_ERROR_CRC:
          Serial.println("CRC Error");
          break;
        default:
          Serial.println("Unknown Error");
      }
      delay(2000); // minimum time between two DHT samples is two seconds
    }
    SimpleDHTlibrary

    Code:
    #include "SimpleDHT.h"
    
    // for DHT11, 
    //      VCC: 5V or 3V
    //      GND: GND
    //      DATA: 2
    int pinDHT11 = 2;
    SimpleDHT11 dht11;
    
    void setup() {
      Serial.begin(115200);
    }
    
    void loop() {
      // start working...
      Serial.println("=================================");
      Serial.println("Sample DHT11...");
      
      // read without samples.
      byte temperature = 0;
      byte humidity = 0;
      int err = SimpleDHTErrSuccess;
      if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
        Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
        return;
      }
      
      Serial.print("Sample OK: ");
      Serial.print((int)temperature); Serial.print(" *C, "); 
      Serial.print((int)humidity); Serial.println(" H");
      
      // DHT11 sampling rate is 1HZ.
      delay(1500);
    }

    •   AltAdvertisment

        
       

  2. #2
    Super Moderator
    Points: 73,300, Level: 66
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    12,007
    Helped
    3972 / 3972
    Points
    73,300
    Level
    66

    Re: NodeMCu Interface with DHT11

    Are you following the guidelines in the DHT11 data sheet, in particular the delay needed before initializing it and the need for a pull-up resistor and decoupling capacitor if the cable is long?

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



    •   AltAdvertisment

        
       

  3. #3
    Member level 2
    Points: 408, Level: 4

    Join Date
    Oct 2017
    Posts
    51
    Helped
    0 / 0
    Points
    408
    Level
    4

    Re: NodeMCu Interface with DHT11

    Quote Originally Posted by betwixt View Post
    Are you following the guidelines in the DHT11 data sheet, in particular the delay needed before initializing it and the need for a pull-up resistor and decoupling capacitor if the cable is long?

    Brian.
    I am testing same like wise used for arduino uno. It's working well with arduino uno. Only thing i changes is nodemcu v1. 0 used with simpledht11 library. I am getting time out error. Yes length is short. I have powered using external 3.3v with common ground. If I program without library also I get time out error.



    •   AltAdvertisment

        
       

  4. #4
    Super Moderator
    Points: 27,662, Level: 40
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,254
    Helped
    1045 / 1045
    Points
    27,662
    Level
    40
    Blog Entries
    5

    Re: NodeMCu Interface with DHT11

    Quote Originally Posted by ajit_nayak87 View Post
    It's working well with arduino uno. Only thing i changes is nodemcu v1. 0 used with simpledht11 library
    You have defined the sensor pinout with the specific value 7:

    Code:
    #define DHT11_PIN 7
    However, from the values defined for ESP8266 at the file pins_arduino.h this number is not assigned to any pin:

    Code:
    static const uint8_t D0   = 16;
    static const uint8_t D1   = 5;
    static const uint8_t D2   = 4;
    static const uint8_t D3   = 0;
    static const uint8_t D4   = 2;
    static const uint8_t D5   = 14;
    static const uint8_t D6   = 12;
    static const uint8_t D7   = 13;
    static const uint8_t D8   = 15;
    static const uint8_t D9   = 3;
    static const uint8_t D10  = 1;
    So, instead of using a numeral parameter, prefer using mnemonics Dn which turns the code more easy to debug, as well as to match with the pin numbering printed on the board:

    Click image for larger version. 

Name:	N_MCU.png 
Views:	1 
Size:	223.7 KB 
ID:	147852

    BTW, for the NodeMCU board, you should be aware that the following pins have already built-in resistors that assure initial state to these pins during start-up, therefore unless you are plenty sure on how to share them with other signals w/o conflicting, select any others but these ones:

    // PIN-27 // D3 : GPIO0 ; // PULL-UP
    // PIN-26 // D4 : GPIO2 ; // PULL-UP
    // PIN-20 // D8 : GPIO15 ; // PULL-DOWN
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



--[[ ]]--