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.

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