DHT.cpp 3.1 KB

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