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.

245 lines
6.9 KiB

  1. // -----------------------------------------------------------------------------
  2. // DHTXX Sensor
  3. // Copyright (C) 2017-2018 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 != GPIO_NONE) 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 != GPIO_NONE) gpioReleaseLock(_previous);
  50. _previous = GPIO_NONE;
  51. if (!gpioGetLock(_gpio)) {
  52. _error = SENSOR_ERROR_GPIO_USED;
  53. return;
  54. }
  55. _previous = _gpio;
  56. _count = 2;
  57. _ready = true;
  58. }
  59. // Pre-read hook (usually to populate registers with up-to-date data)
  60. void pre() {
  61. _error = SENSOR_ERROR_OK;
  62. _read();
  63. }
  64. // Descriptive name of the sensor
  65. String description() {
  66. char buffer[20];
  67. snprintf(buffer, sizeof(buffer), "DHT%d @ GPIO%d", _type, _gpio);
  68. return String(buffer);
  69. }
  70. // Descriptive name of the slot # index
  71. String slot(unsigned char index) {
  72. return description();
  73. };
  74. // Address of the sensor (it could be the GPIO or I2C address)
  75. String address(unsigned char index) {
  76. return String(_gpio);
  77. }
  78. // Type for slot # index
  79. unsigned char type(unsigned char index) {
  80. if (index == 0) return MAGNITUDE_TEMPERATURE;
  81. if (index == 1) return MAGNITUDE_HUMIDITY;
  82. return MAGNITUDE_NONE;
  83. }
  84. // Current value for slot # index
  85. double value(unsigned char index) {
  86. if (index == 0) return _temperature;
  87. if (index == 1) return _humidity;
  88. return 0;
  89. }
  90. protected:
  91. // ---------------------------------------------------------------------
  92. // Protected
  93. // ---------------------------------------------------------------------
  94. void _read() {
  95. if ((_last_ok > 0) && (millis() - _last_ok < DHT_MIN_INTERVAL)) {
  96. _error = SENSOR_ERROR_OK;
  97. return;
  98. }
  99. unsigned long low = 0;
  100. unsigned long high = 0;
  101. unsigned char dhtData[DHT_MAX_DATA] = {0};
  102. unsigned char byteInx = 0;
  103. unsigned char bitInx = 7;
  104. // Send start signal to DHT sensor
  105. if (++_errors > DHT_MAX_ERRORS) {
  106. _errors = 0;
  107. digitalWrite(_gpio, HIGH);
  108. delay(250);
  109. }
  110. pinMode(_gpio, OUTPUT);
  111. noInterrupts();
  112. digitalWrite(_gpio, LOW);
  113. if (_type == DHT_CHIP_DHT11) {
  114. delay(20);
  115. } else {
  116. delayMicroseconds(500);
  117. }
  118. digitalWrite(_gpio, HIGH);
  119. delayMicroseconds(40);
  120. pinMode(_gpio, INPUT_PULLUP);
  121. delayMicroseconds(10);
  122. // No errors, read the 40 data bits
  123. for( int k = 0; k < 41; k++ ) {
  124. // Starts new data transmission with >50us low signal
  125. low = _signal(100, LOW);
  126. if (low == 0) {
  127. _error = SENSOR_ERROR_TIMEOUT;
  128. return;
  129. }
  130. // Check to see if after >70us rx data is a 0 or a 1
  131. high = _signal(100, HIGH);
  132. if (high == 0) {
  133. _error = SENSOR_ERROR_TIMEOUT;
  134. return;
  135. }
  136. // Skip the first bit
  137. if (k == 0) continue;
  138. // add the current read to the output data
  139. // since all dhtData array where set to 0 at the start,
  140. // only look for "1" (>28us us)
  141. if (high > low) dhtData[byteInx] |= (1 << bitInx);
  142. // index to next byte
  143. if (bitInx == 0) {
  144. bitInx = 7;
  145. ++byteInx;
  146. } else {
  147. --bitInx;
  148. }
  149. }
  150. interrupts();
  151. // Verify checksum
  152. if (dhtData[4] != ((dhtData[0] + dhtData[1] + dhtData[2] + dhtData[3]) & 0xFF)) {
  153. _error = SENSOR_ERROR_CRC;
  154. return;
  155. }
  156. // Get humidity from Data[0] and Data[1]
  157. if (_type == DHT_CHIP_DHT11) {
  158. _humidity = dhtData[0];
  159. } else {
  160. _humidity = dhtData[0] * 256 + dhtData[1];
  161. _humidity /= 10;
  162. }
  163. // Get temp from Data[2] and Data[3]
  164. if (_type == DHT_CHIP_DHT11) {
  165. _temperature = dhtData[2];
  166. } else {
  167. _temperature = (dhtData[2] & 0x7F) * 256 + dhtData[3];
  168. _temperature /= 10;
  169. if (dhtData[2] & 0x80) _temperature *= -1;
  170. }
  171. _last_ok = millis();
  172. _errors = 0;
  173. _error = SENSOR_ERROR_OK;
  174. }
  175. unsigned long _signal(int usTimeOut, bool state) {
  176. unsigned long uSec = 1;
  177. while (digitalRead(_gpio) == state) {
  178. if (++uSec > usTimeOut) return 0;
  179. delayMicroseconds(1);
  180. }
  181. return uSec;
  182. }
  183. unsigned char _gpio = GPIO_NONE;
  184. unsigned char _previous = GPIO_NONE;
  185. unsigned char _type = DHT_CHIP_DHT22;
  186. unsigned long _last_ok = 0;
  187. unsigned char _errors = 0;
  188. double _temperature = 0;
  189. unsigned int _humidity = 0;
  190. };
  191. #endif // SENSOR_SUPPORT && DHT_SUPPORT