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.

228 lines
6.5 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. /*
  2. DHT MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if DHT_SUPPORT
  6. double _dhtTemperature = 0;
  7. unsigned int _dhtHumidity = 0;
  8. // -----------------------------------------------------------------------------
  9. // HAL
  10. // https://github.com/gosouth/DHT22/blob/master/main/DHT22.c
  11. // -----------------------------------------------------------------------------
  12. #define DHT_MAX_DATA 5
  13. #define DHT_MAX_ERRORS 5
  14. #define DHT_MIN_INTERVAL 2000
  15. #define DHT_OK 0
  16. #define DHT_CHECKSUM_ERROR -1
  17. #define DHT_TIMEOUT_ERROR -2
  18. #define DHT11 11
  19. #define DHT22 22
  20. #define DHT21 21
  21. #define AM2301 21
  22. unsigned long _getSignalLevel(unsigned char gpio, int usTimeOut, bool state) {
  23. unsigned long uSec = 1;
  24. while (digitalRead(gpio) == state) {
  25. if (++uSec > usTimeOut) return 0;
  26. delayMicroseconds(1);
  27. }
  28. return uSec;
  29. }
  30. int readDHT(unsigned char gpio, unsigned char type) {
  31. static unsigned long last_ok = 0;
  32. if (millis() - last_ok < DHT_MIN_INTERVAL) return DHT_OK;
  33. unsigned long low = 0;
  34. unsigned long high = 0;
  35. static unsigned char errors = 0;
  36. uint8_t dhtData[DHT_MAX_DATA] = {0};
  37. uint8_t byteInx = 0;
  38. uint8_t bitInx = 7;
  39. // Send start signal to DHT sensor
  40. if (++errors > DHT_MAX_ERRORS) {
  41. errors = 0;
  42. digitalWrite(gpio, HIGH);
  43. delay(250);
  44. }
  45. pinMode(gpio, OUTPUT);
  46. digitalWrite(gpio, LOW);
  47. delay(20);
  48. noInterrupts();
  49. digitalWrite(gpio, HIGH);
  50. delayMicroseconds(40);
  51. pinMode(gpio, INPUT_PULLUP);
  52. delayMicroseconds(10);
  53. // DHT will keep the line low for 80 us and then high for 80us
  54. low = _getSignalLevel(gpio, 85, LOW);
  55. if (low==0) return DHT_TIMEOUT_ERROR;
  56. high = _getSignalLevel(gpio, 85, HIGH);
  57. if (high==0) return DHT_TIMEOUT_ERROR;
  58. // No errors, read the 40 data bits
  59. for( int k = 0; k < 40; k++ ) {
  60. // Starts new data transmission with >50us low signal
  61. low = _getSignalLevel(gpio, 56, LOW);
  62. if (low==0) return DHT_TIMEOUT_ERROR;
  63. // Check to see if after >70us rx data is a 0 or a 1
  64. high = _getSignalLevel(gpio, 75, HIGH);
  65. if (high==0) return DHT_TIMEOUT_ERROR;
  66. // add the current read to the output data
  67. // since all dhtData array where set to 0 at the start,
  68. // only look for "1" (>28us us)
  69. if (high > low) dhtData[byteInx] |= (1 << bitInx);
  70. // index to next byte
  71. if (bitInx == 0) {
  72. bitInx = 7;
  73. ++byteInx;
  74. } else {
  75. --bitInx;
  76. }
  77. }
  78. interrupts();
  79. // Verify checksum
  80. if (dhtData[4] != ((dhtData[0] + dhtData[1] + dhtData[2] + dhtData[3]) & 0xFF)) {
  81. return DHT_CHECKSUM_ERROR;
  82. }
  83. // Get humidity from Data[0] and Data[1]
  84. if (type == DHT11) {
  85. _dhtHumidity = dhtData[0];
  86. } else {
  87. _dhtHumidity = dhtData[0] * 256 + dhtData[1];
  88. _dhtHumidity /= 10;
  89. }
  90. // Get temp from Data[2] and Data[3]
  91. if (type == DHT11) {
  92. _dhtTemperature = dhtData[2];
  93. } else {
  94. _dhtTemperature = (dhtData[2] & 0x7F) * 256 + dhtData[3];
  95. _dhtTemperature /= 10;
  96. if (dhtData[2] & 0x80) _dhtTemperature *= -1;
  97. }
  98. last_ok = millis();
  99. errors = 0;
  100. return DHT_OK;
  101. }
  102. int readDHT() {
  103. return readDHT(DHT_PIN, DHT_TYPE);
  104. }
  105. // -----------------------------------------------------------------------------
  106. // Values
  107. // -----------------------------------------------------------------------------
  108. double getDHTTemperature(bool celsius) {
  109. return celsius ? _dhtTemperature : _dhtTemperature * 1.8 + 32;
  110. }
  111. double getDHTTemperature() {
  112. return getDHTTemperature(true);
  113. }
  114. unsigned int getDHTHumidity() {
  115. return _dhtHumidity;
  116. }
  117. void dhtSetup() {
  118. #if WEB_SUPPORT
  119. apiRegister(DHT_TEMPERATURE_TOPIC, DHT_TEMPERATURE_TOPIC, [](char * buffer, size_t len) {
  120. dtostrf(_dhtTemperature, 1-len, 1, buffer);
  121. });
  122. apiRegister(DHT_HUMIDITY_TOPIC, DHT_HUMIDITY_TOPIC, [](char * buffer, size_t len) {
  123. snprintf_P(buffer, len, PSTR("%d"), _dhtHumidity);
  124. });
  125. #endif
  126. }
  127. void dhtLoop() {
  128. // Check if we should read new data
  129. static unsigned long last_update = 0;
  130. if ((millis() - last_update > DHT_UPDATE_INTERVAL) || (last_update == 0)) {
  131. last_update = millis();
  132. // Read sensor data
  133. if (readDHT(DHT_PIN, DHT_TYPE) == DHT_OK) {
  134. unsigned char tmpUnits = getSetting("tmpUnits", TMP_UNITS).toInt();
  135. double tmpCorrection = getSetting("tmpCorrection", TMP_CORRECTION).toFloat();
  136. double t = getDHTTemperature(tmpUnits == TMP_CELSIUS) + tmpCorrection;
  137. unsigned int h = getDHTHumidity();
  138. char temperature[6];
  139. char humidity[6];
  140. dtostrf(t, 1-sizeof(temperature), 1, temperature);
  141. itoa((unsigned int) h, humidity, 10);
  142. DEBUG_MSG_P(PSTR("[DHT] Temperature: %s%s\n"), temperature, (tmpUnits == TMP_CELSIUS) ? "ºC" : "ºF");
  143. DEBUG_MSG_P(PSTR("[DHT] Humidity: %s\n"), humidity);
  144. // Send MQTT messages
  145. mqttSend(getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), temperature);
  146. mqttSend(getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), humidity);
  147. // Send to Domoticz
  148. #if DOMOTICZ_SUPPORT
  149. {
  150. domoticzSend("dczTmpIdx", 0, temperature);
  151. int status;
  152. if (h > 70) {
  153. status = HUMIDITY_WET;
  154. } else if (h > 45) {
  155. status = HUMIDITY_COMFORTABLE;
  156. } else if (h > 30) {
  157. status = HUMIDITY_NORMAL;
  158. } else {
  159. status = HUMIDITY_DRY;
  160. }
  161. char buffer[2];
  162. snprintf_P(buffer, sizeof(buffer), PSTR("%d"), status);
  163. domoticzSend("dczHumIdx", humidity, buffer);
  164. }
  165. #endif
  166. #if INFLUXDB_SUPPORT
  167. influxDBSend(getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), temperature);
  168. influxDBSend(getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), humidity);
  169. #endif
  170. // Update websocket clients
  171. #if WEB_SUPPORT
  172. char buffer[100];
  173. snprintf_P(buffer, sizeof(buffer), PSTR("{\"dhtVisible\": 1, \"dhtTmp\": %s, \"dhtHum\": %s, \"tmpUnits\": %d}"), temperature, humidity, tmpUnits);
  174. wsSend(buffer);
  175. #endif
  176. }
  177. }
  178. }
  179. #endif