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.

244 lines
6.9 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. // Private
  107. // -----------------------------------------------------------------------------
  108. void _dhtWebSocketOnSend(JsonObject& root) {
  109. root["dhtVisible"] = 1;
  110. root["dhtTmp"] = getDHTTemperature();
  111. root["dhtHum"] = getDHTHumidity();
  112. }
  113. // -----------------------------------------------------------------------------
  114. // Values
  115. // -----------------------------------------------------------------------------
  116. double getDHTTemperature(bool celsius) {
  117. double value = celsius ? _dhtTemperature : _dhtTemperature * 1.8 + 32;
  118. double correction = getSetting("tmpCorrection", TEMPERATURE_CORRECTION).toFloat();
  119. return roundTo(value + correction, DHT_TEMPERATURE_DECIMALS);
  120. }
  121. double getDHTTemperature() {
  122. return getDHTTemperature(true);
  123. }
  124. unsigned int getDHTHumidity() {
  125. return _dhtHumidity;
  126. }
  127. void dhtSetup() {
  128. #if WEB_SUPPORT
  129. // Websockets
  130. wsOnSendRegister(_dhtWebSocketOnSend);
  131. apiRegister(DHT_TEMPERATURE_TOPIC, DHT_TEMPERATURE_TOPIC, [](char * buffer, size_t len) {
  132. dtostrf(_dhtTemperature, 1-len, 1, buffer);
  133. });
  134. apiRegister(DHT_HUMIDITY_TOPIC, DHT_HUMIDITY_TOPIC, [](char * buffer, size_t len) {
  135. snprintf_P(buffer, len, PSTR("%d"), _dhtHumidity);
  136. });
  137. #endif
  138. }
  139. void dhtLoop() {
  140. // Check if we should read new data
  141. static unsigned long last_update = 0;
  142. if ((millis() - last_update > DHT_UPDATE_INTERVAL) || (last_update == 0)) {
  143. last_update = millis();
  144. // Read sensor data
  145. if (readDHT(DHT_PIN, DHT_TYPE) == DHT_OK) {
  146. unsigned char tmpUnits = getSetting("tmpUnits", TMP_UNITS).toInt();
  147. double t = getDHTTemperature(tmpUnits == TMP_CELSIUS);
  148. unsigned int h = getDHTHumidity();
  149. char temperature[6];
  150. char humidity[6];
  151. dtostrf(t, 1-sizeof(temperature), 1, temperature);
  152. itoa((unsigned int) h, humidity, 10);
  153. DEBUG_MSG_P(PSTR("[DHT] Temperature: %s%s\n"), temperature, (tmpUnits == TMP_CELSIUS) ? "ºC" : "ºF");
  154. DEBUG_MSG_P(PSTR("[DHT] Humidity: %s\n"), humidity);
  155. // Send MQTT messages
  156. mqttSend(getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), temperature);
  157. mqttSend(getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), humidity);
  158. // Send to Domoticz
  159. #if DOMOTICZ_SUPPORT
  160. {
  161. domoticzSend("dczTmpIdx", 0, temperature);
  162. int status;
  163. if (h > 70) {
  164. status = HUMIDITY_WET;
  165. } else if (h > 45) {
  166. status = HUMIDITY_COMFORTABLE;
  167. } else if (h > 30) {
  168. status = HUMIDITY_NORMAL;
  169. } else {
  170. status = HUMIDITY_DRY;
  171. }
  172. char buffer[2];
  173. snprintf_P(buffer, sizeof(buffer), PSTR("%d"), status);
  174. domoticzSend("dczHumIdx", humidity, buffer);
  175. }
  176. #endif
  177. #if INFLUXDB_SUPPORT
  178. idbSend(getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), temperature);
  179. idbSend(getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), humidity);
  180. #endif
  181. // Update websocket clients
  182. #if WEB_SUPPORT
  183. char buffer[100];
  184. snprintf_P(buffer, sizeof(buffer), PSTR("{\"dhtVisible\": 1, \"dhtTmp\": %s, \"dhtHum\": %s, \"tmpUnits\": %d}"), temperature, humidity, tmpUnits);
  185. wsSend(buffer);
  186. #endif
  187. }
  188. }
  189. }
  190. #endif