From fb41466cb7b6341930b7bbeac375a09f4308430d Mon Sep 17 00:00:00 2001 From: Theo Arends Date: Mon, 9 Jul 2018 14:18:26 +0200 Subject: [PATCH] Change DS18B20 driver Change DS18B20 driver to provide better instant results --- sonoff/_releasenotes.ino | 1 + sonoff/xsns_05_ds18b20.ino | 73 ++++++++++++++++---------------------- 2 files changed, 32 insertions(+), 42 deletions(-) diff --git a/sonoff/_releasenotes.ino b/sonoff/_releasenotes.ino index 6fd301922..65bde0745 100644 --- a/sonoff/_releasenotes.ino +++ b/sonoff/_releasenotes.ino @@ -1,4 +1,5 @@ /* 6.1.0a + * Change DS18B20 driver to provide 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_ds18b20.ino b/sonoff/xsns_05_ds18b20.ino index dabaf25d4..12ebe3be2 100644 --- a/sonoff/xsns_05_ds18b20.ino +++ b/sonoff/xsns_05_ds18b20.ino @@ -26,9 +26,12 @@ #define W1_CONVERT_TEMP 0x44 #define W1_READ_SCRATCHPAD 0xBE -float ds18b20_last_temperature = 0; +#define DS18B20_MAX_MISS 5 + +float ds18b20_temperature = 0; uint16_t ds18b20_last_result = 0; uint8_t ds18x20_pin = 0; +uint8_t ds18b20_second = 0; /*********************************************************************************************\ * Embedded stripped and tuned OneWire library @@ -126,12 +129,7 @@ boolean OneWireCrc8(uint8_t *addr) /********************************************************************************************/ -void Ds18x20Init() -{ - ds18x20_pin = pin[GPIO_DSB]; -} - -void Ds18x20Convert() +void Ds18b20Convert() { OneWireReset(); OneWireWrite(W1_SKIP_ROM); // Address all Sensors on Bus @@ -139,25 +137,16 @@ void Ds18x20Convert() // delay(750); // 750ms should be enough for 12bit conv } -boolean Ds18b20Read(float &t) +void Ds18b20Read() { uint8_t data[9]; int8_t sign = 1; - if (!ds18b20_last_temperature) { - t = NAN; - } else { - ds18b20_last_result++; - if (ds18b20_last_result > 4) { // Reset after 4 misses - ds18b20_last_temperature = NAN; - } - t = ds18b20_last_temperature; - } - + if (ds18b20_last_result) { ds18b20_last_result--; } /* - if (!OneWireReadBit()) { //check measurement end + if (!OneWireReadBit()) { // Check end of measurement AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DSB D_SENSOR_BUSY)); - return !isnan(t); + return; } */ for (uint8_t retry = 0; retry < 3; retry++) { @@ -173,36 +162,40 @@ boolean Ds18b20Read(float &t) temp12 = (~temp12) +1; sign = -1; } - t = ConvertTemp(sign * temp12 * 0.0625); - ds18b20_last_result = 0; - } - if (!isnan(t)) { - ds18b20_last_temperature = t; - return true; + ds18b20_temperature = ConvertTemp(sign * temp12 * 0.0625); + ds18b20_last_result = DS18B20_MAX_MISS; + return; } } AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DSB D_SENSOR_CRC_ERROR)); - return !isnan(t); +} + +/********************************************************************************************/ + +void Ds18b20EverySecond() +{ + ds18x20_pin = pin[GPIO_DSB]; + ds18b20_second++; + if (ds18b20_second &1) { + Ds18b20Convert(); // Start conversion, takes up to one second + } else { + Ds18b20Read(); // Read temperature + } } void Ds18b20Show(boolean json) { - float t; - - if (Ds18b20Read(t)) { // Check if read failed + if (ds18b20_last_result) { // Check for valid temperature char temperature[10]; - dtostrfd(t, Settings.flag2.temperature_resolution, temperature); - + dtostrfd(ds18b20_temperature, Settings.flag2.temperature_resolution, temperature); if(json) { snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"DS18B20\":{\"" D_JSON_TEMPERATURE "\":%s}"), mqtt_data, temperature); #ifdef USE_DOMOTICZ - if (0 == tele_period) DomoticzSensor(DZ_TEMP, temperature); + if (0 == tele_period) { DomoticzSensor(DZ_TEMP, temperature); } #endif // USE_DOMOTICZ #ifdef USE_KNX - if (0 == tele_period) { - KnxSensor(KNX_TEMPERATURE, t); - } + if (0 == tele_period) { KnxSensor(KNX_TEMPERATURE, ds18b20_temperature); } #endif // USE_KNX #ifdef USE_WEBSERVER } else { @@ -210,7 +203,6 @@ void Ds18b20Show(boolean json) #endif // USE_WEBSERVER } } - Ds18x20Convert(); // Start conversion, takes up to one second } /*********************************************************************************************\ @@ -225,11 +217,8 @@ boolean Xsns05(byte function) if (pin[GPIO_DSB] < 99) { switch (function) { - case FUNC_INIT: - Ds18x20Init(); - break; - case FUNC_PREP_BEFORE_TELEPERIOD: - Ds18x20Convert(); // Start conversion, takes up to one second + case FUNC_EVERY_SECOND: + Ds18b20EverySecond(); break; case FUNC_JSON_APPEND: Ds18b20Show(1);