diff --git a/sonoff/_releasenotes.ino b/sonoff/_releasenotes.ino index 65bde0745..f18c25b6b 100644 --- a/sonoff/_releasenotes.ino +++ b/sonoff/_releasenotes.ino @@ -1,5 +1,6 @@ /* 6.1.0a - * Change DS18B20 driver to provide instant results + * Change DS18x20 driver to provide better instant results (#3169) + * Change DS18B20 driver to provide better instant results * Remove TSL2561 debug message and update library (#2415) * Change SHT1x sensor initialization from pre-teleperiod to once during restart to fix I2C interference * Add wifi and mqtt status led blinkyblinky to be disabled by SetOption31 1. Does not work when LedPower is On (deliberate) (#871, #2230, #3114, #3155) diff --git a/sonoff/xsns_05_ds18x20.ino b/sonoff/xsns_05_ds18x20.ino index 292191b18..96c6ef335 100644 --- a/sonoff/xsns_05_ds18x20.ino +++ b/sonoff/xsns_05_ds18x20.ino @@ -33,15 +33,24 @@ #define W1_WRITE_SCRATCHPAD 0x4E #define W1_READ_SCRATCHPAD 0xBE +#define DS18X20_MAX_MISS 5 #define DS18X20_MAX_SENSORS 8 +typedef struct DS18X20SENSORS { + uint8_t address[8]; + uint8_t index; + uint8_t result; + float temperature; +} Ds18x20Sensor; + const char kDs18x20Types[] PROGMEM = "DS18x20|DS18S20|DS1822|DS18B20|MAX31850"; uint8_t ds18x20_chipids[] = { 0, DS18S20_CHIPID, DS1822_CHIPID, DS18B20_CHIPID, MAX31850_CHIPID }; -uint8_t ds18x20_address[DS18X20_MAX_SENSORS][8]; -uint8_t ds18x20_index[DS18X20_MAX_SENSORS] = { 0 }; + +Ds18x20Sensor ds18x20_sensor[DS18X20_MAX_SENSORS]; uint8_t ds18x20_sensors = 0; uint8_t ds18x20_pin = 0; +uint8_t ds18x20_second = 0; char ds18x20_types[9]; /*********************************************************************************************\ @@ -248,26 +257,26 @@ void Ds18x20Init() ds18x20_pin = pin[GPIO_DSB]; OneWireResetSearch(); for (ds18x20_sensors = 0; ds18x20_sensors < DS18X20_MAX_SENSORS; ds18x20_sensors) { - if (!OneWireSearch(ds18x20_address[ds18x20_sensors])) { + if (!OneWireSearch(ds18x20_sensor[ds18x20_sensors].address)) { break; } - if (OneWireCrc8(ds18x20_address[ds18x20_sensors]) && - ((ds18x20_address[ds18x20_sensors][0] == DS18S20_CHIPID) || - (ds18x20_address[ds18x20_sensors][0] == DS1822_CHIPID) || - (ds18x20_address[ds18x20_sensors][0] == DS18B20_CHIPID) || - (ds18x20_address[ds18x20_sensors][0] == MAX31850_CHIPID))) { - ds18x20_index[ds18x20_sensors] = ds18x20_sensors; - ids[ds18x20_sensors] = ds18x20_address[ds18x20_sensors][0]; // Chip id + if (OneWireCrc8(ds18x20_sensor[ds18x20_sensors].address) && + ((ds18x20_sensor[ds18x20_sensors].address[0] == DS18S20_CHIPID) || + (ds18x20_sensor[ds18x20_sensors].address[0] == DS1822_CHIPID) || + (ds18x20_sensor[ds18x20_sensors].address[0] == DS18B20_CHIPID) || + (ds18x20_sensor[ds18x20_sensors].address[0] == MAX31850_CHIPID))) { + ds18x20_sensor[ds18x20_sensors].index = ds18x20_sensors; + ids[ds18x20_sensors] = ds18x20_sensor[ds18x20_sensors].address[0]; // Chip id for (uint8_t j = 6; j > 0; j--) { - ids[ds18x20_sensors] = ids[ds18x20_sensors] << 8 | ds18x20_address[ds18x20_sensors][j]; + ids[ds18x20_sensors] = ids[ds18x20_sensors] << 8 | ds18x20_sensor[ds18x20_sensors].address[j]; } ds18x20_sensors++; } } for (uint8_t i = 0; i < ds18x20_sensors; i++) { for (uint8_t j = i + 1; j < ds18x20_sensors; j++) { - if (ids[ds18x20_index[i]] > ids[ds18x20_index[j]]) { // Sort ascending - std::swap(ds18x20_index[i], ds18x20_index[j]); + if (ids[ds18x20_sensor[i].index] > ids[ds18x20_sensor[j].index]) { // Sort ascending + std::swap(ds18x20_sensor[i].index, ds18x20_sensor[j].index); } } } @@ -283,7 +292,7 @@ void Ds18x20Convert() // delay(750); // 750ms should be enough for 12bit conv } -boolean Ds18x20Read(uint8_t sensor, float &t) +void Ds18x20Read(uint8_t sensor) { uint8_t data[9]; int8_t sign = 1; @@ -291,17 +300,17 @@ boolean Ds18x20Read(uint8_t sensor, float &t) int16_t temp14 = 0; float temp9 = 0.0; - t = NAN; - + uint8_t index = ds18x20_sensor[sensor].index; + if (ds18x20_sensor[index].result) { ds18x20_sensor[index].result--; } for (uint8_t retry = 0; retry < 3; retry++) { OneWireReset(); - OneWireSelect(ds18x20_address[ds18x20_index[sensor]]); + OneWireSelect(ds18x20_sensor[index].address); OneWireWrite(W1_READ_SCRATCHPAD); for (uint8_t i = 0; i < 9; i++) { data[i] = OneWireRead(); } if (OneWireCrc8(data)) { - switch(ds18x20_address[ds18x20_index[sensor]][0]) { + switch(ds18x20_sensor[index].address[0]) { case DS18S20_CHIPID: if (data[1] > 0x80) { data[0] = (~data[0]) +1; @@ -312,19 +321,20 @@ boolean Ds18x20Read(uint8_t sensor, float &t) } else { temp9 = (data[0] >> 1) * sign; } - t = ConvertTemp((temp9 - 0.25) + ((16.0 - data[6]) / 16.0)); - break; + ds18x20_sensor[index].temperature = ConvertTemp((temp9 - 0.25) + ((16.0 - data[6]) / 16.0)); + ds18x20_sensor[index].result = DS18X20_MAX_MISS; + return; case DS1822_CHIPID: case DS18B20_CHIPID: if (data[4] != 0x7F) { data[4] = 0x7F; // Set resolution to 12-bit OneWireReset(); - OneWireSelect(ds18x20_address[ds18x20_index[sensor]]); + OneWireSelect(ds18x20_sensor[index].address); OneWireWrite(W1_WRITE_SCRATCHPAD); OneWireWrite(data[2]); // Th Register OneWireWrite(data[3]); // Tl Register OneWireWrite(data[4]); // Configuration Register - OneWireSelect(ds18x20_address[ds18x20_index[sensor]]); + OneWireSelect(ds18x20_sensor[index].address); OneWireWrite(W1_WRITE_EEPROM); // Save scratchpad to EEPROM } temp12 = (data[1] << 8) + data[0]; @@ -332,41 +342,54 @@ boolean Ds18x20Read(uint8_t sensor, float &t) temp12 = (~temp12) +1; sign = -1; } - t = ConvertTemp(sign * temp12 * 0.0625); // Divide by 16 - break; + ds18x20_sensor[index].temperature = ConvertTemp(sign * temp12 * 0.0625); // Divide by 16 + ds18x20_sensor[index].result = DS18X20_MAX_MISS; + return; case MAX31850_CHIPID: temp14 = (data[1] << 8) + (data[0] & 0xFC); - t = ConvertTemp(temp14 * 0.0625); // Divide by 16 - break; + ds18x20_sensor[index].temperature = ConvertTemp(temp14 * 0.0625); // Divide by 16 + ds18x20_sensor[index].result = DS18X20_MAX_MISS; + return; } } - if (!isnan(t)) { - return true; - } } AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DSB D_SENSOR_CRC_ERROR)); - return false; +} + +/********************************************************************************************/ + +void Ds18x20EverySecond() +{ + ds18x20_second++; + if (ds18x20_second &1) { + Ds18x20Convert(); // Start conversion, takes up to one second + } else { + for (uint8_t i = 0; i < ds18x20_sensors; i++) { + Ds18x20Read(i); // Read temperature + } + } } void Ds18x20Show(boolean json) { char temperature[10]; char stemp[12]; - float t; bool domoticz_flag = true; for (uint8_t i = 0; i < ds18x20_sensors; i++) { - if (Ds18x20Read(i, t)) { // Check if read failed - dtostrfd(t, Settings.flag2.temperature_resolution, temperature); + uint8_t index = ds18x20_sensor[i].index; - uint8_t index = sizeof(ds18x20_chipids); - while (index) { - if (ds18x20_address[ds18x20_index[i]][0] == ds18x20_chipids[index]) { + if (ds18x20_sensor[index].result) { // Check for valid temperature + dtostrfd(ds18x20_sensor[index].temperature, Settings.flag2.temperature_resolution, temperature); + + uint8_t idx = sizeof(ds18x20_chipids); + while (idx) { + if (ds18x20_sensor[ds18x20_sensor[idx].index].address[0] == ds18x20_chipids[idx]) { break; } - index--; + idx--; } - GetTextIndexed(ds18x20_types, sizeof(ds18x20_types), index, kDs18x20Types); + GetTextIndexed(ds18x20_types, sizeof(ds18x20_types), idx, kDs18x20Types); snprintf_P(stemp, sizeof(stemp), PSTR("%s-%d"), ds18x20_types, i +1); if (json) { @@ -375,7 +398,7 @@ void Ds18x20Show(boolean json) } else { char address[17]; for (byte j = 0; j < 6; j++) { - sprintf(address+2*j, "%02X", ds18x20_address[ds18x20_index[i]][6-j]); // Skip sensor type and crc + sprintf(address+2*j, "%02X", ds18x20_sensor[index].address[6-j]); // Skip sensor type and crc } snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"%s\":{\"" D_JSON_ID "\":\"%s\",\"" D_JSON_TEMPERATURE "\":%s}"), mqtt_data, stemp, address, temperature); } @@ -387,7 +410,7 @@ void Ds18x20Show(boolean json) #endif // USE_DOMOTICZ #ifdef USE_KNX if ((0 == tele_period) && (0 == i)) { - KnxSensor(KNX_TEMPERATURE, t); + KnxSensor(KNX_TEMPERATURE, ds18x20_sensor[index].temperature); } #endif // USE_KNX #ifdef USE_WEBSERVER @@ -397,7 +420,6 @@ void Ds18x20Show(boolean json) } } } - Ds18x20Convert(); // Start conversion, takes up to one second } /*********************************************************************************************\ @@ -415,8 +437,8 @@ boolean Xsns05(byte function) case FUNC_INIT: Ds18x20Init(); break; - case FUNC_PREP_BEFORE_TELEPERIOD: - Ds18x20Convert(); // Start conversion, takes up to one second + case FUNC_EVERY_SECOND: + Ds18x20EverySecond(); break; case FUNC_JSON_APPEND: Ds18x20Show(1);