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.

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