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.

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