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.

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