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.

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