xsns_06_dht.ino 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. xsns_06_dht.ino - DHTxx, AM23xx and SI7021 temperature and humidity sensor support for Sonoff-Tasmota
  3. Copyright (C) 2018 Theo Arends
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifdef USE_DHT
  16. /*********************************************************************************************\
  17. * DHT11, AM2301 (DHT21, DHT22, AM2302, AM2321), SI7021 - Temperature and Humidy
  18. *
  19. * Reading temperature or humidity takes about 250 milliseconds!
  20. * Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  21. * Source: Adafruit Industries https://github.com/adafruit/DHT-sensor-library
  22. \*********************************************************************************************/
  23. #define XSNS_06 6
  24. #define DHT_MAX_SENSORS 3
  25. #define DHT_MAX_RETRY 8
  26. uint32_t dht_max_cycles;
  27. uint8_t dht_data[5];
  28. byte dht_sensors = 0;
  29. struct DHTSTRUCT {
  30. byte pin;
  31. byte type;
  32. char stype[12];
  33. uint32_t lastreadtime;
  34. uint8_t lastresult;
  35. float t = NAN;
  36. float h = NAN;
  37. } Dht[DHT_MAX_SENSORS];
  38. void DhtReadPrep(void)
  39. {
  40. for (byte i = 0; i < dht_sensors; i++) {
  41. digitalWrite(Dht[i].pin, HIGH);
  42. }
  43. }
  44. int32_t DhtExpectPulse(byte sensor, bool level)
  45. {
  46. int32_t count = 0;
  47. while (digitalRead(Dht[sensor].pin) == level) {
  48. if (count++ >= (int32_t)dht_max_cycles) {
  49. return -1; // Timeout
  50. }
  51. }
  52. return count;
  53. }
  54. boolean DhtRead(byte sensor)
  55. {
  56. int32_t cycles[80];
  57. uint8_t error = 0;
  58. dht_data[0] = dht_data[1] = dht_data[2] = dht_data[3] = dht_data[4] = 0;
  59. // digitalWrite(Dht[sensor].pin, HIGH);
  60. // delay(250);
  61. if (Dht[sensor].lastresult > DHT_MAX_RETRY) {
  62. Dht[sensor].lastresult = 0;
  63. digitalWrite(Dht[sensor].pin, HIGH); // Retry read prep
  64. delay(250);
  65. }
  66. pinMode(Dht[sensor].pin, OUTPUT);
  67. digitalWrite(Dht[sensor].pin, LOW);
  68. if (GPIO_SI7021 == Dht[sensor].type) {
  69. delayMicroseconds(500);
  70. } else {
  71. delay(20);
  72. }
  73. noInterrupts();
  74. digitalWrite(Dht[sensor].pin, HIGH);
  75. delayMicroseconds(40);
  76. pinMode(Dht[sensor].pin, INPUT_PULLUP);
  77. delayMicroseconds(10);
  78. if (-1 == DhtExpectPulse(sensor, LOW)) {
  79. AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " " D_START_SIGNAL_LOW " " D_PULSE));
  80. error = 1;
  81. }
  82. else if (-1 == DhtExpectPulse(sensor, HIGH)) {
  83. AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " " D_START_SIGNAL_HIGH " " D_PULSE));
  84. error = 1;
  85. }
  86. else {
  87. for (int i = 0; i < 80; i += 2) {
  88. cycles[i] = DhtExpectPulse(sensor, LOW);
  89. cycles[i+1] = DhtExpectPulse(sensor, HIGH);
  90. }
  91. }
  92. interrupts();
  93. if (error) { return false; }
  94. for (int i = 0; i < 40; ++i) {
  95. int32_t lowCycles = cycles[2*i];
  96. int32_t highCycles = cycles[2*i+1];
  97. if ((-1 == lowCycles) || (-1 == highCycles)) {
  98. AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " " D_PULSE));
  99. return false;
  100. }
  101. dht_data[i/8] <<= 1;
  102. if (highCycles > lowCycles) {
  103. dht_data[i / 8] |= 1;
  104. }
  105. }
  106. uint8_t checksum = (dht_data[0] + dht_data[1] + dht_data[2] + dht_data[3]) & 0xFF;
  107. if (dht_data[4] != checksum) {
  108. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DHT D_CHECKSUM_FAILURE " %02X, %02X, %02X, %02X, %02X =? %02X"),
  109. dht_data[0], dht_data[1], dht_data[2], dht_data[3], dht_data[4], checksum);
  110. AddLog(LOG_LEVEL_DEBUG);
  111. return false;
  112. }
  113. return true;
  114. }
  115. void DhtReadTempHum(byte sensor)
  116. {
  117. if ((NAN == Dht[sensor].h) || (Dht[sensor].lastresult > DHT_MAX_RETRY)) { // Reset after 8 misses
  118. Dht[sensor].t = NAN;
  119. Dht[sensor].h = NAN;
  120. }
  121. if (DhtRead(sensor)) {
  122. switch (Dht[sensor].type) {
  123. case GPIO_DHT11:
  124. Dht[sensor].h = dht_data[0];
  125. Dht[sensor].t = dht_data[2] + ((float)dht_data[3] * 0.1f); // Issue #3164
  126. break;
  127. case GPIO_DHT22:
  128. case GPIO_SI7021:
  129. Dht[sensor].h = ((dht_data[0] << 8) | dht_data[1]) * 0.1;
  130. Dht[sensor].t = (((dht_data[2] & 0x7F) << 8 ) | dht_data[3]) * 0.1;
  131. if (dht_data[2] & 0x80) {
  132. Dht[sensor].t *= -1;
  133. }
  134. break;
  135. }
  136. Dht[sensor].t = ConvertTemp(Dht[sensor].t);
  137. Dht[sensor].lastresult = 0;
  138. } else {
  139. Dht[sensor].lastresult++;
  140. }
  141. }
  142. boolean DhtSetup(byte pin, byte type)
  143. {
  144. boolean success = false;
  145. if (dht_sensors < DHT_MAX_SENSORS) {
  146. Dht[dht_sensors].pin = pin;
  147. Dht[dht_sensors].type = type;
  148. dht_sensors++;
  149. success = true;
  150. }
  151. return success;
  152. }
  153. /********************************************************************************************/
  154. void DhtInit(void)
  155. {
  156. dht_max_cycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for reading pulses from DHT sensor.
  157. for (byte i = 0; i < dht_sensors; i++) {
  158. pinMode(Dht[i].pin, INPUT_PULLUP);
  159. Dht[i].lastreadtime = 0;
  160. Dht[i].lastresult = 0;
  161. GetTextIndexed(Dht[i].stype, sizeof(Dht[i].stype), Dht[i].type, kSensorNames);
  162. if (dht_sensors > 1) {
  163. snprintf_P(Dht[i].stype, sizeof(Dht[i].stype), PSTR("%s-%02d"), Dht[i].stype, Dht[i].pin);
  164. }
  165. }
  166. }
  167. void DhtEverySecond(void)
  168. {
  169. if (uptime &1) {
  170. // <1mS
  171. DhtReadPrep();
  172. } else {
  173. for (byte i = 0; i < dht_sensors; i++) {
  174. // DHT11 and AM2301 25mS per sensor, SI7021 5mS per sensor
  175. DhtReadTempHum(i);
  176. }
  177. }
  178. }
  179. void DhtShow(boolean json)
  180. {
  181. for (byte i = 0; i < dht_sensors; i++) {
  182. char temperature[33];
  183. dtostrfd(Dht[i].t, Settings.flag2.temperature_resolution, temperature);
  184. char humidity[33];
  185. dtostrfd(Dht[i].h, Settings.flag2.humidity_resolution, humidity);
  186. if (json) {
  187. snprintf_P(mqtt_data, sizeof(mqtt_data), JSON_SNS_TEMPHUM, mqtt_data, Dht[i].stype, temperature, humidity);
  188. #ifdef USE_DOMOTICZ
  189. if ((0 == tele_period) && (0 == i)) {
  190. DomoticzTempHumSensor(temperature, humidity);
  191. }
  192. #endif // USE_DOMOTICZ
  193. #ifdef USE_KNX
  194. if ((0 == tele_period) && (0 == i)) {
  195. KnxSensor(KNX_TEMPERATURE, Dht[i].t);
  196. KnxSensor(KNX_HUMIDITY, Dht[i].h);
  197. }
  198. #endif // USE_KNX
  199. #ifdef USE_WEBSERVER
  200. } else {
  201. snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TEMP, mqtt_data, Dht[i].stype, temperature, TempUnit());
  202. snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_HUM, mqtt_data, Dht[i].stype, humidity);
  203. #endif // USE_WEBSERVER
  204. }
  205. }
  206. }
  207. /*********************************************************************************************\
  208. * Interface
  209. \*********************************************************************************************/
  210. boolean Xsns06(byte function)
  211. {
  212. boolean result = false;
  213. if (dht_flg) {
  214. switch (function) {
  215. case FUNC_INIT:
  216. DhtInit();
  217. break;
  218. case FUNC_EVERY_SECOND:
  219. DhtEverySecond();
  220. break;
  221. case FUNC_JSON_APPEND:
  222. DhtShow(1);
  223. break;
  224. #ifdef USE_WEBSERVER
  225. case FUNC_WEB_APPEND:
  226. DhtShow(0);
  227. break;
  228. #endif // USE_WEBSERVER
  229. }
  230. }
  231. return result;
  232. }
  233. #endif // USE_DHT