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.

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