Skip to content

Commit

Permalink
fix negative temperature (from DHTNew) (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart authored Nov 6, 2021
1 parent b58ee17 commit 6c72318
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
46 changes: 28 additions & 18 deletions DHTStable.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//
// FILE: DHTStable.cpp
// AUTHOR: Rob Tillaart
// VERSION: 1.0.1
// VERSION: 1.1.0
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
// URL: https://github.com/RobTillaart/DHTstable
//
// HISTORY:

// 1.1.0 2021-11-06 fix DHTNew-#67 negative temperature
// 1.0.1 2021-06-01 change architecture to fix incompatible flag.
// 1.0.0 2021-05-26 rename files and class to DHTStable to fix incompatible flag.
// changed temperature and humidity to private
Expand Down Expand Up @@ -82,16 +83,16 @@ int DHTStable::read11(uint8_t pin)
}

// CONVERT AND STORE
_humidity = bits[0] + bits[1] * 0.1;
_temperature = (bits[2] & 0x7F) + bits[3] * 0.1;
if (bits[2] & 0x80) // negative temperature
_humidity = _bits[0] + _bits[1] * 0.1;
_temperature = (_bits[2] & 0x7F) + _bits[3] * 0.1;
if (_bits[2] & 0x80) // negative temperature
{
_temperature = -_temperature;
}

// TEST CHECKSUM
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
if (bits[4] != sum)
uint8_t sum = _bits[0] + _bits[1] + _bits[2] + _bits[3];
if (_bits[4] != sum)
{
return DHTLIB_ERROR_CHECKSUM;
}
Expand All @@ -111,19 +112,28 @@ int DHTStable::read(uint8_t pin)
if (_disableIRQ) interrupts();
if (rv != DHTLIB_OK)
{
_humidity = DHTLIB_INVALID_VALUE; // NaN prefered?
_temperature = DHTLIB_INVALID_VALUE; // NaN prefered?
_humidity = DHTLIB_INVALID_VALUE; // NaN preferred?
_temperature = DHTLIB_INVALID_VALUE; // NaN preferred?
return rv; // propagate error value
}

// CONVERT AND STORE
_humidity = word(bits[0], bits[1]) * 0.1;
int16_t t = bits[2] * 256 + bits[3];
_temperature = t * 0.1;

_humidity = word(_bits[0], _bits[1]) * 0.1;
int16_t t = ((_bits[2] & 0x7F) * 256 + _bits[3]);
if (t == 0)
{
_temperature = 0.0; // prevent -0.0;
}
else
{
_temperature = t * 0.1;
if((_bits[2] & 0x80) == 0x80 )
{
_temperature = -_temperature;
}
}
// TEST CHECKSUM
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
if (bits[4] != sum)
uint8_t sum = _bits[0] + _bits[1] + _bits[2] + _bits[3];
if (_bits[4] != sum)
{
return DHTLIB_ERROR_CHECKSUM;
}
Expand All @@ -146,7 +156,7 @@ int DHTStable::_readSensor(uint8_t pin, uint8_t wakeupDelay)
uint8_t idx = 0;

// EMPTY BUFFER
for (uint8_t i = 0; i < 5; i++) bits[i] = 0;
for (uint8_t i = 0; i < 5; i++) _bits[i] = 0;

// REQUEST SAMPLE
pinMode(pin, OUTPUT);
Expand All @@ -168,7 +178,7 @@ int DHTStable::_readSensor(uint8_t pin, uint8_t wakeupDelay)
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
}

// READ THE OUTPUT - 40 BITS => 5 BYTES
// READ THE OUTPUT - 40 _bits => 5 BYTES
for (uint8_t i = 40; i != 0; i--)
{
loopCnt = DHTLIB_TIMEOUT;
Expand All @@ -187,7 +197,7 @@ int DHTStable::_readSensor(uint8_t pin, uint8_t wakeupDelay)

if ((micros() - t) > 40)
{
bits[idx] |= mask;
_bits[idx] |= mask;
}
mask >>= 1;
if (mask == 0) // next byte?
Expand Down
12 changes: 6 additions & 6 deletions DHTStable.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: DHTStable.h
// AUTHOR: Rob Tillaart
// VERSION: 1.0.1
// VERSION: 1.1.0
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
// URL: https://github.com/RobTillaart/DHTstable
//
Expand All @@ -13,7 +13,7 @@

#include "Arduino.h"

#define DHTSTABLE_LIB_VERSION (F("1.0.1 - DHTStable"))
#define DHTSTABLE_LIB_VERSION (F("1.1.0 - DHTStable"))


const int DHTLIB_OK = 0;
Expand All @@ -26,8 +26,8 @@ const int DHTLIB_DHT_WAKEUP = 1;


// max timeout is 100usec.
// For a 16Mhz proc that is max 1600 clock cycles
// loops using TIMEOUT use at least 4 clock cycli
// For a 16Mhz processor that is max 1600 clock cycles
// loops using TIMEOUT use at least 4 clock cycles
// so 100 us takes max 400 loops
// so by dividing F_CPU by 40000 we "fail" as fast as possible
const int DHTLIB_TIMEOUT = (F_CPU/40000);
Expand All @@ -54,7 +54,7 @@ class DHTStable
inline int read44(uint8_t pin) { return read(pin); }; // ok
inline int read2301(uint8_t pin) { return read(pin); }; // ok
inline int read2302(uint8_t pin) { return read(pin); }; // ok
inline int read2320(uint8_t pin) { return read(pin); }; //.ok
inline int read2320(uint8_t pin) { return read(pin); }; // ok
inline int read2322(uint8_t pin) { return read(pin); }; // ok

// read values from cache, call read() to refresh!
Expand All @@ -65,7 +65,7 @@ class DHTStable
void setDisableIRQ(bool b ) { _disableIRQ = b; };

private:
uint8_t bits[5]; // buffer to receive data
uint8_t _bits[5]; // buffer to receive data
int _readSensor(uint8_t pin, uint8_t wakeupDelay);
bool _disableIRQ = false;
float _humidity;
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/DHTstable.git"
},
"version": "1.0.1",
"version": "1.1.0",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*"
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=DHTStable
version=1.0.1
version=1.1.0
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Stable version of library for DHT Temperature & Humidity Sensor
Expand Down

0 comments on commit 6c72318

Please sign in to comment.