123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- #include "DHT.h"
- #define MIN_INTERVAL 2000
- #define TIMEOUT \
- UINT32_MAX
- DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
- (void)count;
- _pin = pin;
- _type = type;
- #ifdef __AVR
- _bit = digitalPinToBitMask(pin);
- _port = digitalPinToPort(pin);
- #endif
- _maxcycles =
- microsecondsToClockCycles(1000);
-
-
-
- }
- void DHT::begin(uint8_t usec) {
-
- pinMode(_pin, INPUT_PULLUP);
-
-
-
- _lastreadtime = millis() - MIN_INTERVAL;
- DEBUG_PRINT("DHT max clock cycles: ");
- DEBUG_PRINTLN(_maxcycles, DEC);
- pullTime = usec;
- }
- float DHT::readTemperature(bool S, bool force) {
- float f = NAN;
- if (read(force)) {
- switch (_type) {
- case DHT11:
- f = data[2];
- if (data[3] & 0x80) {
- f = -1 - f;
- }
- f += (data[3] & 0x0f) * 0.1;
- if (S) {
- f = convertCtoF(f);
- }
- break;
- case DHT12:
- f = data[2];
- f += (data[3] & 0x0f) * 0.1;
- if (data[2] & 0x80) {
- f *= -1;
- }
- if (S) {
- f = convertCtoF(f);
- }
- break;
- case DHT22:
- case DHT21:
- f = ((word)(data[2] & 0x7F)) << 8 | data[3];
- f *= 0.1;
- if (data[2] & 0x80) {
- f *= -1;
- }
- if (S) {
- f = convertCtoF(f);
- }
- break;
- }
- }
- return f;
- }
- float DHT::convertCtoF(float c) { return c * 1.8 + 32; }
- float DHT::convertFtoC(float f) { return (f - 32) * 0.55555; }
- float DHT::readHumidity(bool force) {
- float f = NAN;
- if (read(force)) {
- switch (_type) {
- case DHT11:
- case DHT12:
- f = data[0] + data[1] * 0.1;
- break;
- case DHT22:
- case DHT21:
- f = ((word)data[0]) << 8 | data[1];
- f *= 0.1;
- break;
- }
- }
- return f;
- }
- float DHT::computeHeatIndex(bool isFahrenheit) {
- float hi = computeHeatIndex(readTemperature(isFahrenheit), readHumidity(),
- isFahrenheit);
- return hi;
- }
- float DHT::computeHeatIndex(float temperature, float percentHumidity,
- bool isFahrenheit) {
- float hi;
- if (!isFahrenheit)
- temperature = convertCtoF(temperature);
- hi = 0.5 * (temperature + 61.0 + ((temperature - 68.0) * 1.2) +
- (percentHumidity * 0.094));
- if (hi > 79) {
- hi = -42.379 + 2.04901523 * temperature + 10.14333127 * percentHumidity +
- -0.22475541 * temperature * percentHumidity +
- -0.00683783 * pow(temperature, 2) +
- -0.05481717 * pow(percentHumidity, 2) +
- 0.00122874 * pow(temperature, 2) * percentHumidity +
- 0.00085282 * temperature * pow(percentHumidity, 2) +
- -0.00000199 * pow(temperature, 2) * pow(percentHumidity, 2);
- if ((percentHumidity < 13) && (temperature >= 80.0) &&
- (temperature <= 112.0))
- hi -= ((13.0 - percentHumidity) * 0.25) *
- sqrt((17.0 - abs(temperature - 95.0)) * 0.05882);
- else if ((percentHumidity > 85.0) && (temperature >= 80.0) &&
- (temperature <= 87.0))
- hi += ((percentHumidity - 85.0) * 0.1) * ((87.0 - temperature) * 0.2);
- }
- return isFahrenheit ? hi : convertFtoC(hi);
- }
- bool DHT::read(bool force) {
-
-
- uint32_t currenttime = millis();
- if (!force && ((currenttime - _lastreadtime) < MIN_INTERVAL)) {
- return _lastresult;
- }
- _lastreadtime = currenttime;
-
- data[0] = data[1] = data[2] = data[3] = data[4] = 0;
- #if defined(ESP8266)
- yield();
- #endif
-
-
-
-
- pinMode(_pin, INPUT_PULLUP);
- delay(1);
-
- pinMode(_pin, OUTPUT);
- digitalWrite(_pin, LOW);
- switch (_type) {
- case DHT22:
- case DHT21:
- delayMicroseconds(1100);
- break;
- case DHT11:
- default:
- delay(20);
- break;
- }
- uint32_t cycles[80];
- {
-
- pinMode(_pin, INPUT_PULLUP);
-
- delayMicroseconds(pullTime);
-
-
-
- InterruptLock lock;
-
-
- if (expectPulse(LOW) == TIMEOUT) {
- DEBUG_PRINTLN(F("DHT timeout waiting for start signal low pulse."));
- _lastresult = false;
- return _lastresult;
- }
- if (expectPulse(HIGH) == TIMEOUT) {
- DEBUG_PRINTLN(F("DHT timeout waiting for start signal high pulse."));
- _lastresult = false;
- return _lastresult;
- }
-
-
-
-
-
-
-
-
- for (int i = 0; i < 80; i += 2) {
- cycles[i] = expectPulse(LOW);
- cycles[i + 1] = expectPulse(HIGH);
- }
- }
-
-
- for (int i = 0; i < 40; ++i) {
- uint32_t lowCycles = cycles[2 * i];
- uint32_t highCycles = cycles[2 * i + 1];
- if ((lowCycles == TIMEOUT) || (highCycles == TIMEOUT)) {
- DEBUG_PRINTLN(F("DHT timeout waiting for pulse."));
- _lastresult = false;
- return _lastresult;
- }
- data[i / 8] <<= 1;
-
- if (highCycles > lowCycles) {
-
- data[i / 8] |= 1;
- }
-
-
-
- }
- DEBUG_PRINTLN(F("Received from DHT:"));
- DEBUG_PRINT(data[0], HEX);
- DEBUG_PRINT(F(", "));
- DEBUG_PRINT(data[1], HEX);
- DEBUG_PRINT(F(", "));
- DEBUG_PRINT(data[2], HEX);
- DEBUG_PRINT(F(", "));
- DEBUG_PRINT(data[3], HEX);
- DEBUG_PRINT(F(", "));
- DEBUG_PRINT(data[4], HEX);
- DEBUG_PRINT(F(" =? "));
- DEBUG_PRINTLN((data[0] + data[1] + data[2] + data[3]) & 0xFF, HEX);
-
- if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
- _lastresult = true;
- return _lastresult;
- } else {
- DEBUG_PRINTLN(F("DHT checksum failure!"));
- _lastresult = false;
- return _lastresult;
- }
- }
- uint32_t DHT::expectPulse(bool level) {
- #if (F_CPU > 16000000L) || (F_CPU == 0L)
- uint32_t count = 0;
- #else
- uint16_t count = 0;
- #endif
- #ifdef __AVR
- uint8_t portState = level ? _bit : 0;
- while ((*portInputRegister(_port) & _bit) == portState) {
- if (count++ >= _maxcycles) {
- return TIMEOUT;
- }
- }
- #else
- while (digitalRead(_pin) == level) {
- if (count++ >= _maxcycles) {
- return TIMEOUT;
- }
- }
- #endif
- return count;
- }
|