Skip to content

return; bug fixed #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions DHT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ void DHT::setup(uint8_t pin, DHT_MODEL_t model)
if ( model == AUTO_DETECT) {
DHT::model = DHT22;
readSensor();
if ( error == ERROR_TIMEOUT ) {
if ( error == ERROR_TIMEOUT )
DHT::model = DHT11;
// Warning: in case we auto detect a DHT11, you should wait at least 1000 msec
// before your first read request. Otherwise you will get a time out error.
}
}
}

Expand Down Expand Up @@ -108,15 +107,15 @@ const char *DHT::getStatusString() {

#endif

void DHT::readSensor()
bool DHT::readSensor()
{
// Make sure we don't poll the sensor too often
// - Max sample rate DHT11 is 1 Hz (duty cicle 1000 ms)
// - Max sample rate DHT22 is 0.5 Hz (duty cicle 2000 ms)
unsigned long startTime = millis();
if ( (unsigned long)(startTime - lastReadTime) < (model == DHT11 ? 999L : 1999L) ) {
return;
}
if ( (unsigned long)(startTime - lastReadTime) < (model == DHT11 ? 999L : 1999L) )
return false;

lastReadTime = startTime;

temperature = NAN;
Expand All @@ -126,13 +125,11 @@ void DHT::readSensor()

digitalWrite(pin, LOW); // Send start signal
pinMode(pin, OUTPUT);
if ( model == DHT11 ) {
if ( model == DHT11 )
delay(18);
}
else {
else
// This will fail for a DHT11 - that's how we can detect such a device
delayMicroseconds(800);
}

pinMode(pin, INPUT);
digitalWrite(pin, HIGH); // Switch bus to receive data
Expand All @@ -154,7 +151,7 @@ void DHT::readSensor()
age = (unsigned long)(micros() - startTime);
if ( age > 90 ) {
error = ERROR_TIMEOUT;
return;
return false;
}
}
while ( digitalRead(pin) == (i & 1) ? HIGH : LOW );
Expand All @@ -164,9 +161,8 @@ void DHT::readSensor()
data <<= 1;

// A zero max 30 usecs, a one at least 68 usecs.
if ( age > 30 ) {
if ( age > 30 )
data |= 1; // we got a one
}
}

switch ( i ) {
Expand All @@ -184,7 +180,7 @@ void DHT::readSensor()

if ( (byte)(((byte)rawHumidity) + (rawHumidity >> 8) + ((byte)rawTemperature) + (rawTemperature >> 8)) != data ) {
error = ERROR_CHECKSUM;
return;
return false;
}

// Store readings
Expand All @@ -196,11 +192,11 @@ void DHT::readSensor()
else {
humidity = rawHumidity * 0.1;

if ( rawTemperature & 0x8000 ) {
if ( rawTemperature & 0x8000 )
rawTemperature = -(int16_t)(rawTemperature & 0x7FFF);
}
temperature = ((int16_t)rawTemperature) * 0.1;
}

error = ERROR_NONE;
return true;
}
2 changes: 1 addition & 1 deletion DHT.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class DHT
static float toCelsius(float fromFahrenheit) { return (fromFahrenheit - 32.0) / 1.8; };

protected:
void readSensor();
bool readSensor();

float temperature;
float humidity;
Expand Down