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.

228 lines
6.5 KiB

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