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.

233 lines
6.6 KiB

  1. // -----------------------------------------------------------------------------
  2. // DHTXX Sensor
  3. // Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && DHT_SUPPORT
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "BaseSensor.h"
  9. #define DHT_MAX_DATA 5
  10. #define DHT_MAX_ERRORS 5
  11. #define DHT_MIN_INTERVAL 2000
  12. #define DHT_CHIP_DHT11 11
  13. #define DHT_CHIP_DHT22 22
  14. #define DHT_CHIP_DHT21 21
  15. #define DHT_CHIP_AM2301 21
  16. class DHTSensor : public BaseSensor {
  17. public:
  18. // ---------------------------------------------------------------------
  19. // Public
  20. // ---------------------------------------------------------------------
  21. DHTSensor(): BaseSensor() {
  22. _count = 2;
  23. _sensor_id = SENSOR_DHTXX_ID;
  24. }
  25. ~DHTSensor() {
  26. if (_previous != 0xFF) gpioReleaseLock(_previous);
  27. }
  28. // ---------------------------------------------------------------------
  29. void setGPIO(unsigned char gpio) {
  30. _gpio = gpio;
  31. }
  32. void setType(unsigned char type) {
  33. _type = type;
  34. }
  35. // ---------------------------------------------------------------------
  36. unsigned char getGPIO() {
  37. return _gpio;
  38. }
  39. unsigned char getType() {
  40. return _type;
  41. }
  42. // ---------------------------------------------------------------------
  43. // Sensor API
  44. // ---------------------------------------------------------------------
  45. // Initialization method, must be idempotent
  46. void begin() {
  47. _count = 0;
  48. // Manage GPIO lock
  49. if (_previous != 0xFF) gpioReleaseLock(_previous);
  50. _previous = 0xFF;
  51. if (!gpioGetLock(_gpio)) {
  52. _error = SENSOR_ERROR_GPIO_USED;
  53. return;
  54. }
  55. _previous = _gpio;
  56. _count = 2;
  57. }
  58. // Pre-read hook (usually to populate registers with up-to-date data)
  59. void pre() {
  60. _read();
  61. }
  62. // Descriptive name of the sensor
  63. String description() {
  64. char buffer[20];
  65. snprintf(buffer, sizeof(buffer), "DHT%d @ GPIO%d", _type, _gpio);
  66. return String(buffer);
  67. }
  68. // Type for slot # index
  69. unsigned char type(unsigned char index) {
  70. _error = SENSOR_ERROR_OK;
  71. if (index == 0) return MAGNITUDE_TEMPERATURE;
  72. if (index == 1) return MAGNITUDE_HUMIDITY;
  73. _error = SENSOR_ERROR_OUT_OF_RANGE;
  74. return MAGNITUDE_NONE;
  75. }
  76. // Current value for slot # index
  77. double value(unsigned char index) {
  78. _error = SENSOR_ERROR_OK;
  79. if (index == 0) return _temperature;
  80. if (index == 1) return _humidity;
  81. _error = SENSOR_ERROR_OUT_OF_RANGE;
  82. return 0;
  83. }
  84. protected:
  85. // ---------------------------------------------------------------------
  86. // Protected
  87. // ---------------------------------------------------------------------
  88. void _read() {
  89. if ((_last_ok > 0) && (millis() - _last_ok < DHT_MIN_INTERVAL)) {
  90. _error = SENSOR_ERROR_OK;
  91. return;
  92. }
  93. unsigned long low = 0;
  94. unsigned long high = 0;
  95. unsigned char dhtData[DHT_MAX_DATA] = {0};
  96. unsigned char byteInx = 0;
  97. unsigned char bitInx = 7;
  98. // Send start signal to DHT sensor
  99. if (++_errors > DHT_MAX_ERRORS) {
  100. _errors = 0;
  101. digitalWrite(_gpio, HIGH);
  102. delay(250);
  103. }
  104. pinMode(_gpio, OUTPUT);
  105. noInterrupts();
  106. digitalWrite(_gpio, LOW);
  107. delayMicroseconds(_type == DHT_CHIP_DHT11 ? 20000 : 500);
  108. digitalWrite(_gpio, HIGH);
  109. delayMicroseconds(40);
  110. pinMode(_gpio, INPUT_PULLUP);
  111. delayMicroseconds(10);
  112. // No errors, read the 40 data bits
  113. for( int k = 0; k < 41; k++ ) {
  114. // Starts new data transmission with >50us low signal
  115. low = _signal(100, LOW);
  116. if (low == 0) {
  117. _error = SENSOR_ERROR_TIMEOUT;
  118. return;
  119. }
  120. // Check to see if after >70us rx data is a 0 or a 1
  121. high = _signal(100, HIGH);
  122. if (high == 0) {
  123. _error = SENSOR_ERROR_TIMEOUT;
  124. return;
  125. }
  126. // Skip the first bit
  127. if (k == 0) continue;
  128. // add the current read to the output data
  129. // since all dhtData array where set to 0 at the start,
  130. // only look for "1" (>28us us)
  131. if (high > low) dhtData[byteInx] |= (1 << bitInx);
  132. // index to next byte
  133. if (bitInx == 0) {
  134. bitInx = 7;
  135. ++byteInx;
  136. } else {
  137. --bitInx;
  138. }
  139. }
  140. interrupts();
  141. // Verify checksum
  142. if (dhtData[4] != ((dhtData[0] + dhtData[1] + dhtData[2] + dhtData[3]) & 0xFF)) {
  143. _error = SENSOR_ERROR_CRC;
  144. return;
  145. }
  146. // Get humidity from Data[0] and Data[1]
  147. if (_type == DHT_CHIP_DHT11) {
  148. _humidity = dhtData[0];
  149. } else {
  150. _humidity = dhtData[0] * 256 + dhtData[1];
  151. _humidity /= 10;
  152. }
  153. // Get temp from Data[2] and Data[3]
  154. if (_type == DHT_CHIP_DHT11) {
  155. _temperature = dhtData[2];
  156. } else {
  157. _temperature = (dhtData[2] & 0x7F) * 256 + dhtData[3];
  158. _temperature /= 10;
  159. if (dhtData[2] & 0x80) _temperature *= -1;
  160. }
  161. _last_ok = millis();
  162. _errors = 0;
  163. _error = SENSOR_ERROR_OK;
  164. }
  165. unsigned long _signal(int usTimeOut, bool state) {
  166. unsigned long uSec = 1;
  167. while (digitalRead(_gpio) == state) {
  168. if (++uSec > usTimeOut) return 0;
  169. delayMicroseconds(1);
  170. }
  171. return uSec;
  172. }
  173. unsigned char _gpio;
  174. unsigned char _previous = 0xFF;
  175. unsigned char _type = DHT_CHIP_DHT22;
  176. unsigned long _last_ok = 0;
  177. unsigned char _errors = 0;
  178. double _temperature;
  179. unsigned int _humidity;
  180. };
  181. #endif // SENSOR_SUPPORT && DHT_SUPPORT