From 736f63e9aec975ca55e97f8cf9a26593628bff2b Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Thu, 18 Apr 2019 11:07:38 +0200 Subject: [PATCH] Fix Shelly 2.5 overtemp detection Fix Shelly 2.5 overtemp detection --- sonoff/support.ino | 10 ++++++++++ sonoff/xnrg_07_ade7953.ino | 2 +- sonoff/xsns_02_analog.ino | 28 +++++++++++++++++++++------- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/sonoff/support.ino b/sonoff/support.ino index 0ea3ead03..d8732fc85 100644 --- a/sonoff/support.ino +++ b/sonoff/support.ino @@ -584,6 +584,16 @@ float ConvertTemp(float c) return result; } +float ConvertTempToCelsius(float c) +{ + float result = c; + + if (!isnan(c) && Settings.flag.temperature_conversion) { + result = (c - 32) / 1.8; // Celsius + } + return result; +} + char TempUnit(void) { return (Settings.flag.temperature_conversion) ? 'F' : 'C'; diff --git a/sonoff/xnrg_07_ade7953.ino b/sonoff/xnrg_07_ade7953.ino index e7e2abd27..2adc01ff2 100644 --- a/sonoff/xnrg_07_ade7953.ino +++ b/sonoff/xnrg_07_ade7953.ino @@ -162,7 +162,7 @@ void Ade7953EnergyEverySecond() void Ade7953EverySecond() { - if (power && (global_temperature > ADE7953_OVERTEMP)) { // Device overtemp, turn off relays + if (power && (ConvertTempToCelsius(AdcTemperature()) > ADE7953_OVERTEMP)) { // Device overtemp, turn off relays SetAllPower(POWER_ALL_OFF, SRC_OVERTEMP); } } diff --git a/sonoff/xsns_02_analog.ino b/sonoff/xsns_02_analog.ino index 822235497..d0f0dfebf 100644 --- a/sonoff/xsns_02_analog.ino +++ b/sonoff/xsns_02_analog.ino @@ -36,6 +36,7 @@ #define ANALOG_B 3350.0 // Thermistor Beta Coefficient uint16_t adc_last_value = 0; +float adc_temp = 0; uint16_t AdcRead(uint8_t factor) { @@ -69,6 +70,22 @@ void AdcEvery250ms(void) } #endif // USE_RULES +void AdcEverySecond(void) +{ + if (my_module_flag.adc0_temp) { + int adc = AdcRead(2); + // Steinhart-Hart equation for thermistor as temperature sensor + double Rt = (adc * ANALOG_R21) / (1024.0 * ANALOG_V33 - (double)adc); + double T = ANALOG_B / (ANALOG_B/ANALOG_T0 + log(Rt/ANALOG_R0)); + adc_temp = ConvertTemp(TO_CELSIUS(T)); + } +} + +float AdcTemperature(void) +{ + return adc_temp; +} + void AdcShow(bool json) { if (my_module_flag.adc0) { @@ -83,14 +100,8 @@ void AdcShow(bool json) } } if (my_module_flag.adc0_temp) { - int adc = AdcRead(2); - // Steinhart-Hart equation for thermistor as temperature sensor - double Rt = (adc * ANALOG_R21) / (1024.0 * ANALOG_V33 - (double)adc); - double T = ANALOG_B / (ANALOG_B/ANALOG_T0 + log(Rt/ANALOG_R0)); - double temp = ConvertTemp(TO_CELSIUS(T)); - char temperature[33]; - dtostrfd(temp, Settings.flag2.temperature_resolution, temperature); + dtostrfd(adc_temp, Settings.flag2.temperature_resolution, temperature); if (json) { ResponseAppend_P(JSON_SNS_TEMP, "ANALOG", temperature); @@ -127,6 +138,9 @@ bool Xsns02(uint8_t function) AdcEvery250ms(); break; #endif // USE_RULES + case FUNC_EVERY_SECOND: + AdcEverySecond(); + break; case FUNC_JSON_APPEND: AdcShow(1); break;