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.

237 lines
6.7 KiB

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