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.

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