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.

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