Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
5.3 KiB

  1. // -----------------------------------------------------------------------------
  2. // DHT Sensor
  3. // -----------------------------------------------------------------------------
  4. #pragma once
  5. #include "Arduino.h"
  6. #include "BaseSensor.h"
  7. #define DHT_MAX_DATA 5
  8. #define DHT_MAX_ERRORS 5
  9. #define DHT_MIN_INTERVAL 2000
  10. #define DHT_OK 0
  11. #define DHT_CHECKSUM_ERROR -1
  12. #define DHT_TIMEOUT_ERROR -2
  13. #define DHT11 11
  14. #define DHT22 22
  15. #define DHT21 21
  16. #define AM2301 21
  17. class DHTSensor : public BaseSensor {
  18. public:
  19. DHTSensor(unsigned char gpio, unsigned char type): BaseSensor() {
  20. _gpio = gpio;
  21. _type = type;
  22. }
  23. // Pre-read hook (usually to populate registers with up-to-date data)
  24. void pre() {
  25. if ((_last_ok > 0) && (millis() - _last_ok < DHT_MIN_INTERVAL)) {
  26. _error = 0;
  27. return;
  28. }
  29. unsigned long low = 0;
  30. unsigned long high = 0;
  31. unsigned char dhtData[DHT_MAX_DATA] = {0};
  32. unsigned char byteInx = 0;
  33. unsigned char bitInx = 7;
  34. // Send start signal to DHT sensor
  35. if (++_errors > DHT_MAX_ERRORS) {
  36. _errors = 0;
  37. digitalWrite(_gpio, HIGH);
  38. delay(250);
  39. }
  40. pinMode(_gpio, OUTPUT);
  41. noInterrupts();
  42. digitalWrite(_gpio, LOW);
  43. delayMicroseconds(500);
  44. digitalWrite(_gpio, HIGH);
  45. delayMicroseconds(40);
  46. pinMode(_gpio, INPUT_PULLUP);
  47. // No errors, read the 40 data bits
  48. for( int k = 0; k < 41; k++ ) {
  49. // Starts new data transmission with >50us low signal
  50. low = _signal(56, LOW);
  51. if (low == 0) {
  52. _error = DHT_TIMEOUT_ERROR;
  53. return;
  54. }
  55. // Check to see if after >70us rx data is a 0 or a 1
  56. high = _signal(75, HIGH);
  57. if (high == 0) {
  58. _error = DHT_TIMEOUT_ERROR;
  59. return;
  60. }
  61. // Skip the first bit
  62. if (k == 0) continue;
  63. // add the current read to the output data
  64. // since all dhtData array where set to 0 at the start,
  65. // only look for "1" (>28us us)
  66. if (high > low) dhtData[byteInx] |= (1 << bitInx);
  67. // index to next byte
  68. if (bitInx == 0) {
  69. bitInx = 7;
  70. ++byteInx;
  71. } else {
  72. --bitInx;
  73. }
  74. }
  75. interrupts();
  76. // Verify checksum
  77. if (dhtData[4] != ((dhtData[0] + dhtData[1] + dhtData[2] + dhtData[3]) & 0xFF)) {
  78. _error = DHT_CHECKSUM_ERROR;
  79. return;
  80. }
  81. // Get humidity from Data[0] and Data[1]
  82. if (_type == DHT11) {
  83. _humidity = dhtData[0];
  84. } else {
  85. _humidity = dhtData[0] * 256 + dhtData[1];
  86. _humidity /= 10;
  87. }
  88. // Get temp from Data[2] and Data[3]
  89. if (_type == DHT11) {
  90. _temperature = dhtData[2];
  91. } else {
  92. _temperature = (dhtData[2] & 0x7F) * 256 + dhtData[3];
  93. _temperature /= 10;
  94. if (dhtData[2] & 0x80) _temperature *= -1;
  95. }
  96. _last_ok = millis();
  97. _errors = 0;
  98. _error = 0;
  99. }
  100. // Post-read hook (usually to reset things)
  101. void post() {
  102. }
  103. // Return sensor status (true for ready)
  104. bool status() {
  105. return (_last_ok > 0) & (_error == 0);
  106. }
  107. // Return sensor last internal error
  108. int error() {
  109. return _error;
  110. }
  111. // Number of available slots
  112. unsigned char count() {
  113. return 2;
  114. }
  115. // Descriptive name of the sensor
  116. String name() {
  117. char buffer[64];
  118. snprintf(buffer, sizeof(buffer), "DHT%d @ GPIO%d", _type, _gpio);
  119. return String(buffer);
  120. }
  121. // Descriptive name of the slot # index
  122. String slot(unsigned char index) {
  123. return name();
  124. }
  125. // Type for slot # index
  126. magnitude_t type(unsigned char index) {
  127. if (index == 0) return MAGNITUDE_TEMPERATURE;
  128. if (index == 1) return MAGNITUDE_HUMIDITY;
  129. return MAGNITUDE_NONE;
  130. }
  131. // Current value for slot # index
  132. double value(unsigned char index) {
  133. if (index == 0) return _temperature;
  134. if (index == 1) return _humidity;
  135. return 0;
  136. }
  137. private:
  138. unsigned long _signal(int usTimeOut, bool state) {
  139. unsigned long uSec = 1;
  140. while (digitalRead(_gpio) == state) {
  141. if (++uSec > usTimeOut) return 0;
  142. delayMicroseconds(1);
  143. }
  144. return uSec;
  145. }
  146. unsigned char _gpio;
  147. unsigned char _type;
  148. int _error;
  149. unsigned long _last_ok = 0;
  150. unsigned char _errors = 0;
  151. double _temperature;
  152. unsigned int _humidity;
  153. };