DHT.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* DHT library
  2. MIT license
  3. written by Adafruit Industries
  4. */
  5. #include <math.h>
  6. #include "DHT.h"
  7. //#define NAN 0
  8. #ifdef DEBUG
  9. #define DEBUG_PRINT(...) Serial.println(__VA_ARGS__)
  10. #else
  11. #define DEBUG_PRINT(...)
  12. #endif
  13. DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
  14. _pin = pin;
  15. _type = type;
  16. _count = count;
  17. firstreading = true;
  18. }
  19. void DHT::begin(void) {
  20. // set up the pins!
  21. pinMode(_pin, INPUT);
  22. digitalWrite(_pin, HIGH);
  23. _lastreadtime = 0;
  24. }
  25. //boolean S == Scale. True == Farenheit; False == Celcius
  26. float DHT::readTemperature(bool S) {
  27. float f;
  28. if (read()) {
  29. switch (_type) {
  30. case DHT11:
  31. f = data[2];
  32. if(S)
  33. f = convertCtoF(f);
  34. return f;
  35. case DHT22:
  36. case DHT21:
  37. f = data[2] & 0x7F;
  38. f *= 256;
  39. f += data[3];
  40. f /= 10;
  41. if (data[2] & 0x80)
  42. f *= -1;
  43. if(S)
  44. f = convertCtoF(f);
  45. return f;
  46. }
  47. }
  48. DEBUG_PRINT("Read fail");
  49. return NAN;
  50. }
  51. float DHT::convertCtoF(float c) {
  52. return c * 9 / 5 + 32;
  53. }
  54. float DHT::readHumidity(void) {
  55. float f;
  56. if (read()) {
  57. switch (_type) {
  58. case DHT11:
  59. f = data[0];
  60. return f;
  61. case DHT22:
  62. case DHT21:
  63. f = data[0];
  64. f *= 256;
  65. f += data[1];
  66. f /= 10;
  67. return f;
  68. }
  69. }
  70. DEBUG_PRINT("Read fail");
  71. return NAN;
  72. }
  73. boolean DHT::read(void) {
  74. uint8_t laststate = HIGH;
  75. uint8_t counter = 0;
  76. uint8_t j = 0, i;
  77. unsigned long currenttime;
  78. // pull the pin high and wait 250 milliseconds
  79. digitalWrite(_pin, HIGH);
  80. delay(250);
  81. currenttime = millis();
  82. if (currenttime < _lastreadtime) {
  83. // ie there was a rollover
  84. _lastreadtime = 0;
  85. }
  86. if (!firstreading && ((currenttime - _lastreadtime) < 2000)) {
  87. return true; // return last correct measurement
  88. //delay(2000 - (currenttime - _lastreadtime));
  89. }
  90. firstreading = false;
  91. /*
  92. DEBUG_PRINT("Currtime: "); DEBUG_PRINT(currenttime);
  93. DEBUG_PRINT(" Lasttime: "); DEBUG_PRINT(_lastreadtime);
  94. */
  95. _lastreadtime = millis();
  96. data[0] = data[1] = data[2] = data[3] = data[4] = 0;
  97. // now pull it low for ~20 milliseconds
  98. pinMode(_pin, OUTPUT);
  99. digitalWrite(_pin, LOW);
  100. delay(20);
  101. //cli();
  102. digitalWrite(_pin, HIGH);
  103. delayMicroseconds(40);
  104. pinMode(_pin, INPUT);
  105. // read in timings
  106. for ( i=0; i< MAXTIMINGS; i++) {
  107. counter = 0;
  108. while (digitalRead(_pin) == laststate) {
  109. counter++;
  110. delayMicroseconds(1);
  111. if (counter == 255) {
  112. break;
  113. }
  114. }
  115. laststate = digitalRead(_pin);
  116. if (counter == 255) break;
  117. // ignore first 3 transitions
  118. if ((i >= 4) && (i%2 == 0)) {
  119. // shove each bit into the storage bytes
  120. data[j/8] <<= 1;
  121. if (counter > _count)
  122. data[j/8] |= 1;
  123. j++;
  124. }
  125. }
  126. //sei();
  127. /*
  128. DEBUG_PRINTln(j, DEC);
  129. DEBUG_PRINT(data[0], HEX); DEBUG_PRINT(", ");
  130. DEBUG_PRINT(data[1], HEX); DEBUG_PRINT(", ");
  131. DEBUG_PRINT(data[2], HEX); DEBUG_PRINT(", ");
  132. DEBUG_PRINT(data[3], HEX); DEBUG_PRINT(", ");
  133. DEBUG_PRINT(data[4], HEX); DEBUG_PRINT(" =? ");
  134. DEBUG_PRINTln(data[0] + data[1] + data[2] + data[3], HEX);
  135. */
  136. // check we read 40 bits and that the checksum matches
  137. if ((j >= 40) &&
  138. (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) {
  139. return true;
  140. }
  141. return false;
  142. }