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.

274 lines
7.6 KiB

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