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.

177 lines
5.1 KiB

  1. // -----------------------------------------------------------------------------
  2. // DHTXX Sensor
  3. // Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #pragma once
  6. #include "Arduino.h"
  7. #include "BaseSensor.h"
  8. #define DHT_MAX_DATA 5
  9. #define DHT_MAX_ERRORS 5
  10. #define DHT_MIN_INTERVAL 2000
  11. #define DHT11 11
  12. #define DHT22 22
  13. #define DHT21 21
  14. #define AM2301 21
  15. class DHTSensor : public BaseSensor {
  16. public:
  17. DHTSensor(unsigned char gpio, unsigned char type, bool pull_up = false): BaseSensor() {
  18. _gpio = gpio;
  19. _type = type;
  20. if (pull_up) pinMode(_gpio, INPUT_PULLUP);
  21. _count = 2;
  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 = SENSOR_ERROR_OK;
  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 = SENSOR_ERROR_TIMEOUT;
  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 = SENSOR_ERROR_TIMEOUT;
  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 = SENSOR_ERROR_CRC;
  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 = SENSOR_ERROR_OK;
  99. }
  100. // Descriptive name of the sensor
  101. String name() {
  102. char buffer[20];
  103. snprintf(buffer, sizeof(buffer), "DHT%d @ GPIO%d", _type, _gpio);
  104. return String(buffer);
  105. }
  106. // Descriptive name of the slot # index
  107. String slot(unsigned char index) {
  108. return name();
  109. }
  110. // Type for slot # index
  111. magnitude_t type(unsigned char index) {
  112. _error = SENSOR_ERROR_OK;
  113. if (index == 0) return MAGNITUDE_TEMPERATURE;
  114. if (index == 1) return MAGNITUDE_HUMIDITY;
  115. _error = SENSOR_ERROR_OUT_OF_RANGE;
  116. return MAGNITUDE_NONE;
  117. }
  118. // Current value for slot # index
  119. double value(unsigned char index) {
  120. _error = SENSOR_ERROR_OK;
  121. if (index == 0) return _temperature;
  122. if (index == 1) return _humidity;
  123. _error = SENSOR_ERROR_OUT_OF_RANGE;
  124. return 0;
  125. }
  126. protected:
  127. unsigned long _signal(int usTimeOut, bool state) {
  128. unsigned long uSec = 1;
  129. while (digitalRead(_gpio) == state) {
  130. if (++uSec > usTimeOut) return 0;
  131. delayMicroseconds(1);
  132. }
  133. return uSec;
  134. }
  135. unsigned char _gpio;
  136. unsigned char _type;
  137. unsigned long _last_ok = 0;
  138. unsigned char _errors = 0;
  139. double _temperature;
  140. unsigned int _humidity;
  141. };