xsns_05_ds18x20_legacy.ino 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. xsns_05_ds18x20_legacy.ino - DS18x20 temperature sensor support for Sonoff-Tasmota
  3. Copyright (C) 2018 Heiko Krupp and 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_DS18x20_LEGACY
  16. /*********************************************************************************************\
  17. * DS18B20 - Temperature
  18. \*********************************************************************************************/
  19. #define XSNS_05 5
  20. #define DS18S20_CHIPID 0x10
  21. #define DS18B20_CHIPID 0x28
  22. #define MAX31850_CHIPID 0x3B
  23. #define W1_SKIP_ROM 0xCC
  24. #define W1_CONVERT_TEMP 0x44
  25. #define W1_READ_SCRATCHPAD 0xBE
  26. #define DS18X20_MAX_SENSORS 8
  27. #include <OneWire.h>
  28. OneWire *ds = NULL;
  29. uint8_t ds18x20_address[DS18X20_MAX_SENSORS][8];
  30. uint8_t ds18x20_index[DS18X20_MAX_SENSORS];
  31. uint8_t ds18x20_sensors = 0;
  32. char ds18x20_types[9];
  33. void Ds18x20Init(void)
  34. {
  35. ds = new OneWire(pin[GPIO_DSB]);
  36. }
  37. void Ds18x20Search(void)
  38. {
  39. uint8_t num_sensors=0;
  40. uint8_t sensor = 0;
  41. ds->reset_search();
  42. for (num_sensors = 0; num_sensors < DS18X20_MAX_SENSORS; num_sensors) {
  43. if (!ds->search(ds18x20_address[num_sensors])) {
  44. ds->reset_search();
  45. break;
  46. }
  47. // If CRC Ok and Type DS18S20, DS18B20 or MAX31850
  48. if ((OneWire::crc8(ds18x20_address[num_sensors], 7) == ds18x20_address[num_sensors][7]) &&
  49. ((ds18x20_address[num_sensors][0]==DS18S20_CHIPID) || (ds18x20_address[num_sensors][0]==DS18B20_CHIPID) || (ds18x20_address[num_sensors][0]==MAX31850_CHIPID))) {
  50. num_sensors++;
  51. }
  52. }
  53. for (byte i = 0; i < num_sensors; i++) {
  54. ds18x20_index[i] = i;
  55. }
  56. for (byte i = 0; i < num_sensors; i++) {
  57. for (byte j = i + 1; j < num_sensors; j++) {
  58. if (uint32_t(ds18x20_address[ds18x20_index[i]]) > uint32_t(ds18x20_address[ds18x20_index[j]])) {
  59. std::swap(ds18x20_index[i], ds18x20_index[j]);
  60. }
  61. }
  62. }
  63. ds18x20_sensors = num_sensors;
  64. }
  65. uint8_t Ds18x20Sensors(void)
  66. {
  67. return ds18x20_sensors;
  68. }
  69. String Ds18x20Addresses(uint8_t sensor)
  70. {
  71. char address[20];
  72. for (byte i = 0; i < 8; i++) {
  73. sprintf(address+2*i, "%02X", ds18x20_address[ds18x20_index[sensor]][i]);
  74. }
  75. return String(address);
  76. }
  77. void Ds18x20Convert(void)
  78. {
  79. ds->reset();
  80. ds->write(W1_SKIP_ROM); // Address all Sensors on Bus
  81. ds->write(W1_CONVERT_TEMP); // start conversion, no parasite power on at the end
  82. // delay(750); // 750ms should be enough for 12bit conv
  83. }
  84. boolean Ds18x20Read(uint8_t sensor, float &t)
  85. {
  86. byte data[12];
  87. int8_t sign = 1;
  88. uint16_t temp12 = 0;
  89. int16_t temp14 = 0;
  90. float temp9 = 0.0;
  91. uint8_t present = 0;
  92. t = NAN;
  93. ds->reset();
  94. ds->select(ds18x20_address[ds18x20_index[sensor]]);
  95. ds->write(W1_READ_SCRATCHPAD); // Read Scratchpad
  96. for (byte i = 0; i < 9; i++) {
  97. data[i] = ds->read();
  98. }
  99. if (OneWire::crc8(data, 8) == data[8]) {
  100. switch(ds18x20_address[ds18x20_index[sensor]][0]) {
  101. case DS18S20_CHIPID:
  102. if (data[1] > 0x80) {
  103. data[0] = (~data[0]) +1;
  104. sign = -1; // App-Note fix possible sign error
  105. }
  106. if (data[0] & 1) {
  107. temp9 = ((data[0] >> 1) + 0.5) * sign;
  108. } else {
  109. temp9 = (data[0] >> 1) * sign;
  110. }
  111. t = ConvertTemp((temp9 - 0.25) + ((16.0 - data[6]) / 16.0));
  112. break;
  113. case DS18B20_CHIPID:
  114. temp12 = (data[1] << 8) + data[0];
  115. if (temp12 > 2047) {
  116. temp12 = (~temp12) +1;
  117. sign = -1;
  118. }
  119. t = ConvertTemp(sign * temp12 * 0.0625); // Divide by 16
  120. break;
  121. case MAX31850_CHIPID:
  122. temp14 = (data[1] << 8) + (data[0] & 0xFC);
  123. t = ConvertTemp(temp14 * 0.0625); // Divide by 16
  124. break;
  125. }
  126. }
  127. return (!isnan(t));
  128. }
  129. /********************************************************************************************/
  130. void Ds18x20Type(uint8_t sensor)
  131. {
  132. strcpy_P(ds18x20_types, PSTR("DS18x20"));
  133. switch(ds18x20_address[ds18x20_index[sensor]][0]) {
  134. case DS18S20_CHIPID:
  135. strcpy_P(ds18x20_types, PSTR("DS18S20"));
  136. break;
  137. case DS18B20_CHIPID:
  138. strcpy_P(ds18x20_types, PSTR("DS18B20"));
  139. break;
  140. case MAX31850_CHIPID:
  141. strcpy_P(ds18x20_types, PSTR("MAX31850"));
  142. break;
  143. }
  144. }
  145. void Ds18x20Show(boolean json)
  146. {
  147. char stemp[10];
  148. float t;
  149. byte dsxflg = 0;
  150. for (byte i = 0; i < Ds18x20Sensors(); i++) {
  151. if (Ds18x20Read(i, t)) { // Check if read failed
  152. Ds18x20Type(i);
  153. char temperature[33];
  154. dtostrfd(t, Settings.flag2.temperature_resolution, temperature);
  155. if (json) {
  156. if (!dsxflg) {
  157. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"DS18x20\":{"), mqtt_data);
  158. stemp[0] = '\0';
  159. }
  160. dsxflg++;
  161. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%s\"DS%d\":{\"" D_JSON_TYPE "\":\"%s\",\"" D_JSON_ADDRESS "\":\"%s\",\"" D_JSON_TEMPERATURE "\":%s}"),
  162. mqtt_data, stemp, i +1, ds18x20_types, Ds18x20Addresses(i).c_str(), temperature);
  163. strlcpy(stemp, ",", sizeof(stemp));
  164. #ifdef USE_DOMOTICZ
  165. if ((0 == tele_period) && (1 == dsxflg)) {
  166. DomoticzSensor(DZ_TEMP, temperature);
  167. }
  168. #endif // USE_DOMOTICZ
  169. #ifdef USE_KNX
  170. if ((0 == tele_period) && (1 == dsxflg)) {
  171. KnxSensor(KNX_TEMPERATURE, t);
  172. }
  173. #endif // USE_KNX
  174. #ifdef USE_WEBSERVER
  175. } else {
  176. snprintf_P(stemp, sizeof(stemp), PSTR("%s-%d"), ds18x20_types, i +1);
  177. snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TEMP, mqtt_data, stemp, temperature, TempUnit());
  178. #endif // USE_WEBSERVER
  179. }
  180. }
  181. }
  182. if (json) {
  183. if (dsxflg) {
  184. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}"), mqtt_data);
  185. }
  186. }
  187. Ds18x20Search(); // Check for changes in sensors number
  188. Ds18x20Convert(); // Start Conversion, takes up to one second
  189. }
  190. /*********************************************************************************************\
  191. * Interface
  192. \*********************************************************************************************/
  193. boolean Xsns05(byte function)
  194. {
  195. boolean result = false;
  196. if (pin[GPIO_DSB] < 99) {
  197. switch (function) {
  198. case FUNC_INIT:
  199. Ds18x20Init();
  200. break;
  201. case FUNC_PREP_BEFORE_TELEPERIOD:
  202. Ds18x20Search(); // Check for changes in sensors number
  203. Ds18x20Convert(); // Start Conversion, takes up to one second
  204. break;
  205. case FUNC_JSON_APPEND:
  206. Ds18x20Show(1);
  207. break;
  208. #ifdef USE_WEBSERVER
  209. case FUNC_WEB_APPEND:
  210. Ds18x20Show(0);
  211. break;
  212. #endif // USE_WEBSERVER
  213. }
  214. }
  215. return result;
  216. }
  217. #endif // USE_DS18x20_LEGACY