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.

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