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.

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