DHT.cpp 3.2 KB

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