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.

297 lines
8.5 KiB

  1. // -----------------------------------------------------------------------------
  2. // DHTXX Sensor
  3. // Copyright (C) 2017-2019 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. constexpr const double DHT_DUMMY_VALUE = -255;
  10. constexpr const size_t DHT_MAX_DATA = 5;
  11. constexpr const size_t DHT_MAX_ERRORS = 5;
  12. constexpr const uint32_t DHT_MIN_INTERVAL = 2000;
  13. enum class DHTChipType {
  14. DHT11,
  15. DHT12,
  16. DHT21,
  17. DHT22,
  18. AM2301,
  19. SI7021
  20. };
  21. // Note: backwards compatibility for configuration headers
  22. #define DHT_CHIP_DHT11 DHTChipType::DHT11
  23. #define DHT_CHIP_DHT12 DHTChipType::DHT12
  24. #define DHT_CHIP_DHT22 DHTChipType::DHT22
  25. #define DHT_CHIP_DHT21 DHTChipType::DHT21
  26. #define DHT_CHIP_AM2301 DHTChipType::AM2301
  27. #define DHT_CHIP_SI7021 DHTChipType::SI7021
  28. int dhtchip_to_number(DHTChipType chip) {
  29. switch (chip) {
  30. case DHTChipType::DHT11:
  31. return 11;
  32. case DHTChipType::DHT12:
  33. return 12;
  34. case DHTChipType::DHT21:
  35. case DHTChipType::AM2301:
  36. return 21;
  37. case DHTChipType::DHT22:
  38. case DHTChipType::SI7021:
  39. return 22;
  40. default:
  41. return -1;
  42. }
  43. }
  44. class DHTSensor : public BaseSensor {
  45. public:
  46. // ---------------------------------------------------------------------
  47. // Public
  48. // ---------------------------------------------------------------------
  49. DHTSensor(): BaseSensor() {
  50. _count = 2;
  51. _sensor_id = SENSOR_DHTXX_ID;
  52. }
  53. ~DHTSensor() {
  54. if (_previous != GPIO_NONE) gpioReleaseLock(_previous);
  55. }
  56. // ---------------------------------------------------------------------
  57. void setGPIO(unsigned char gpio) {
  58. _gpio = gpio;
  59. }
  60. void setType(DHTChipType type) {
  61. _type = type;
  62. }
  63. // ---------------------------------------------------------------------
  64. unsigned char getGPIO() {
  65. return _gpio;
  66. }
  67. int getType() {
  68. return dhtchip_to_number(_type);
  69. }
  70. DHTChipType getChipType() {
  71. return _type;
  72. }
  73. // ---------------------------------------------------------------------
  74. // Sensor API
  75. // ---------------------------------------------------------------------
  76. // Initialization method, must be idempotent
  77. void begin() {
  78. _count = 0;
  79. // Manage GPIO lock
  80. if (_previous != GPIO_NONE) gpioReleaseLock(_previous);
  81. _previous = GPIO_NONE;
  82. if (!gpioGetLock(_gpio)) {
  83. _error = SENSOR_ERROR_GPIO_USED;
  84. return;
  85. }
  86. _previous = _gpio;
  87. // Set now to fail the check in _read at least once
  88. _last_ok = millis();
  89. _count = 2;
  90. _ready = true;
  91. }
  92. // Pre-read hook (usually to populate registers with up-to-date data)
  93. void pre() {
  94. _error = SENSOR_ERROR_OK;
  95. _read();
  96. }
  97. // Descriptive name of the sensor
  98. String description() {
  99. char buffer[20];
  100. snprintf(buffer, sizeof(buffer), "DHT%d @ GPIO%d", dhtchip_to_number(_type), _gpio);
  101. return String(buffer);
  102. }
  103. // Descriptive name of the slot # index
  104. String slot(unsigned char index) {
  105. return description();
  106. };
  107. // Address of the sensor (it could be the GPIO or I2C address)
  108. String address(unsigned char index) {
  109. return String(_gpio);
  110. }
  111. // Type for slot # index
  112. unsigned char type(unsigned char index) {
  113. if (index == 0) return MAGNITUDE_TEMPERATURE;
  114. if (index == 1) return MAGNITUDE_HUMIDITY;
  115. return MAGNITUDE_NONE;
  116. }
  117. // Current value for slot # index
  118. double value(unsigned char index) {
  119. if (index == 0) return _temperature;
  120. if (index == 1) return _humidity;
  121. return 0;
  122. }
  123. protected:
  124. // ---------------------------------------------------------------------
  125. // Protected
  126. // ---------------------------------------------------------------------
  127. void _read() {
  128. if ((_last_ok > 0) && (millis() - _last_ok < DHT_MIN_INTERVAL)) {
  129. if ((_temperature == DHT_DUMMY_VALUE) && (_humidity == DHT_DUMMY_VALUE)) {
  130. _error = SENSOR_ERROR_WARM_UP;
  131. } else {
  132. _error = SENSOR_ERROR_OK;
  133. }
  134. return;
  135. }
  136. unsigned long low = 0;
  137. unsigned long high = 0;
  138. unsigned char dhtData[DHT_MAX_DATA] = {0};
  139. unsigned char byteInx = 0;
  140. unsigned char bitInx = 7;
  141. pinMode(_gpio, OUTPUT);
  142. // Send start signal to DHT sensor
  143. if (++_errors > DHT_MAX_ERRORS) {
  144. _errors = 0;
  145. digitalWrite(_gpio, HIGH);
  146. nice_delay(250);
  147. }
  148. noInterrupts();
  149. digitalWrite(_gpio, LOW);
  150. if ((_type == DHT_CHIP_DHT11) || (_type == DHT_CHIP_DHT12)) {
  151. nice_delay(20);
  152. } else if (_type == DHT_CHIP_SI7021) {
  153. delayMicroseconds(500);
  154. } else {
  155. delayMicroseconds(1100);
  156. }
  157. digitalWrite(_gpio, HIGH);
  158. delayMicroseconds(40);
  159. pinMode(_gpio, INPUT_PULLUP);
  160. delayMicroseconds(10);
  161. // No errors, read the 40 data bits
  162. for( int k = 0; k < 41; k++ ) {
  163. // Starts new data transmission with >50us low signal
  164. low = _signal(100, LOW);
  165. if (low == 0) {
  166. _error = SENSOR_ERROR_TIMEOUT;
  167. return;
  168. }
  169. // Check to see if after >70us rx data is a 0 or a 1
  170. high = _signal(100, HIGH);
  171. if (high == 0) {
  172. _error = SENSOR_ERROR_TIMEOUT;
  173. return;
  174. }
  175. // Skip the first bit
  176. if (k == 0) continue;
  177. // add the current read to the output data
  178. // since all dhtData array where set to 0 at the start,
  179. // only look for "1" (>28us us)
  180. if (high > low) dhtData[byteInx] |= (1 << bitInx);
  181. // index to next byte
  182. if (bitInx == 0) {
  183. bitInx = 7;
  184. ++byteInx;
  185. } else {
  186. --bitInx;
  187. }
  188. }
  189. interrupts();
  190. // Verify checksum
  191. if (dhtData[4] != ((dhtData[0] + dhtData[1] + dhtData[2] + dhtData[3]) & 0xFF)) {
  192. _error = SENSOR_ERROR_CRC;
  193. return;
  194. }
  195. // Get humidity from Data[0] and Data[1]
  196. if (_type == DHT_CHIP_DHT11) {
  197. _humidity = dhtData[0];
  198. } else if (_type == DHT_CHIP_DHT12) {
  199. _humidity = dhtData[0];
  200. _humidity += dhtData[1] * 0.1;
  201. } else {
  202. _humidity = dhtData[0] * 256 + dhtData[1];
  203. _humidity /= 10;
  204. }
  205. // Get temp from Data[2] and Data[3]
  206. if (_type == DHT_CHIP_DHT11) {
  207. _temperature = dhtData[2];
  208. } else if (_type == DHT_CHIP_DHT12) {
  209. _temperature = (dhtData[2] & 0x7F);
  210. _temperature += dhtData[3] * 0.1;
  211. if (dhtData[2] & 0x80) _temperature *= -1;
  212. } else {
  213. _temperature = (dhtData[2] & 0x7F) * 256 + dhtData[3];
  214. _temperature /= 10;
  215. if (dhtData[2] & 0x80) _temperature *= -1;
  216. }
  217. _last_ok = millis();
  218. _errors = 0;
  219. _error = SENSOR_ERROR_OK;
  220. }
  221. unsigned long _signal(unsigned long usTimeOut, bool state) {
  222. unsigned long uSec = 1;
  223. while (digitalRead(_gpio) == state) {
  224. if (++uSec > usTimeOut) return 0;
  225. delayMicroseconds(1);
  226. }
  227. return uSec;
  228. }
  229. DHTChipType _type = DHT_CHIP_DHT22;
  230. unsigned char _gpio = GPIO_NONE;
  231. unsigned char _previous = GPIO_NONE;
  232. unsigned long _last_ok = 0;
  233. unsigned char _errors = 0;
  234. double _temperature = DHT_DUMMY_VALUE;
  235. double _humidity = DHT_DUMMY_VALUE;
  236. };
  237. #endif // SENSOR_SUPPORT && DHT_SUPPORT