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.

210 lines
6.0 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_DHT_ID;
  23. }
  24. // ---------------------------------------------------------------------
  25. void setGPIO(unsigned char gpio) {
  26. _gpio = gpio;
  27. }
  28. void setType(unsigned char type) {
  29. _type = type;
  30. }
  31. // ---------------------------------------------------------------------
  32. unsigned char getGPIO() {
  33. return _gpio;
  34. }
  35. unsigned char getType() {
  36. return _type;
  37. }
  38. // ---------------------------------------------------------------------
  39. // Sensor API
  40. // ---------------------------------------------------------------------
  41. // Pre-read hook (usually to populate registers with up-to-date data)
  42. void pre() {
  43. _read();
  44. }
  45. // Descriptive name of the sensor
  46. String description() {
  47. char buffer[20];
  48. snprintf(buffer, sizeof(buffer), "DHT%d @ GPIO%d", _type, _gpio);
  49. return String(buffer);
  50. }
  51. // Descriptive name of the slot # index
  52. String slot(unsigned char index) {
  53. return description();
  54. }
  55. // Type for slot # index
  56. magnitude_t type(unsigned char index) {
  57. _error = SENSOR_ERROR_OK;
  58. if (index == 0) return MAGNITUDE_TEMPERATURE;
  59. if (index == 1) return MAGNITUDE_HUMIDITY;
  60. _error = SENSOR_ERROR_OUT_OF_RANGE;
  61. return MAGNITUDE_NONE;
  62. }
  63. // Current value for slot # index
  64. double value(unsigned char index) {
  65. _error = SENSOR_ERROR_OK;
  66. if (index == 0) return _temperature;
  67. if (index == 1) return _humidity;
  68. _error = SENSOR_ERROR_OUT_OF_RANGE;
  69. return 0;
  70. }
  71. protected:
  72. // ---------------------------------------------------------------------
  73. // Protected
  74. // ---------------------------------------------------------------------
  75. void _read() {
  76. if ((_last_ok > 0) && (millis() - _last_ok < DHT_MIN_INTERVAL)) {
  77. _error = SENSOR_ERROR_OK;
  78. return;
  79. }
  80. unsigned long low = 0;
  81. unsigned long high = 0;
  82. unsigned char dhtData[DHT_MAX_DATA] = {0};
  83. unsigned char byteInx = 0;
  84. unsigned char bitInx = 7;
  85. // Send start signal to DHT sensor
  86. if (++_errors > DHT_MAX_ERRORS) {
  87. _errors = 0;
  88. digitalWrite(_gpio, HIGH);
  89. delay(250);
  90. }
  91. pinMode(_gpio, OUTPUT);
  92. noInterrupts();
  93. digitalWrite(_gpio, LOW);
  94. delayMicroseconds(500);
  95. digitalWrite(_gpio, HIGH);
  96. delayMicroseconds(40);
  97. pinMode(_gpio, INPUT_PULLUP);
  98. // No errors, read the 40 data bits
  99. for( int k = 0; k < 41; k++ ) {
  100. // Starts new data transmission with >50us low signal
  101. low = _signal(56, LOW);
  102. if (low == 0) {
  103. _error = SENSOR_ERROR_TIMEOUT;
  104. return;
  105. }
  106. // Check to see if after >70us rx data is a 0 or a 1
  107. high = _signal(75, HIGH);
  108. if (high == 0) {
  109. _error = SENSOR_ERROR_TIMEOUT;
  110. return;
  111. }
  112. // Skip the first bit
  113. if (k == 0) continue;
  114. // add the current read to the output data
  115. // since all dhtData array where set to 0 at the start,
  116. // only look for "1" (>28us us)
  117. if (high > low) dhtData[byteInx] |= (1 << bitInx);
  118. // index to next byte
  119. if (bitInx == 0) {
  120. bitInx = 7;
  121. ++byteInx;
  122. } else {
  123. --bitInx;
  124. }
  125. }
  126. interrupts();
  127. // Verify checksum
  128. if (dhtData[4] != ((dhtData[0] + dhtData[1] + dhtData[2] + dhtData[3]) & 0xFF)) {
  129. _error = SENSOR_ERROR_CRC;
  130. return;
  131. }
  132. // Get humidity from Data[0] and Data[1]
  133. if (_type == DHT_CHIP_DHT11) {
  134. _humidity = dhtData[0];
  135. } else {
  136. _humidity = dhtData[0] * 256 + dhtData[1];
  137. _humidity /= 10;
  138. }
  139. // Get temp from Data[2] and Data[3]
  140. if (_type == DHT_CHIP_DHT11) {
  141. _temperature = dhtData[2];
  142. } else {
  143. _temperature = (dhtData[2] & 0x7F) * 256 + dhtData[3];
  144. _temperature /= 10;
  145. if (dhtData[2] & 0x80) _temperature *= -1;
  146. }
  147. _last_ok = millis();
  148. _errors = 0;
  149. _error = SENSOR_ERROR_OK;
  150. }
  151. unsigned long _signal(int usTimeOut, bool state) {
  152. unsigned long uSec = 1;
  153. while (digitalRead(_gpio) == state) {
  154. if (++uSec > usTimeOut) return 0;
  155. delayMicroseconds(1);
  156. }
  157. return uSec;
  158. }
  159. unsigned char _gpio;
  160. unsigned char _type;
  161. unsigned long _last_ok = 0;
  162. unsigned char _errors = 0;
  163. double _temperature;
  164. unsigned int _humidity;
  165. };