ajit_nayak87
Member level 5
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.
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.
SimpleDHTlibrary
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);
}
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);
}