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.

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