diff --git a/tasmota/xdrv_29_deepsleep.ino b/tasmota/xdrv_29_deepsleep.ino index ac0478ee5..f93506410 100644 --- a/tasmota/xdrv_29_deepsleep.ino +++ b/tasmota/xdrv_29_deepsleep.ino @@ -95,7 +95,7 @@ void DeepSleepPrepare(void) if ((RtcSettings.nextwakeup == 0) || (RtcSettings.deepsleep_slip < 9000) || (RtcSettings.deepsleep_slip > 11000) || - (RtcSettings.nextwakeup > (UtcTime() + Settings->deepsleep))) { + (RtcSettings.nextwakeup > (LocalTime() + Settings->deepsleep))) { AddLog(LOG_LEVEL_ERROR, PSTR("DSL: Reset wrong settings wakeup: %ld, slip %ld"), RtcSettings.nextwakeup, RtcSettings.deepsleep_slip ); RtcSettings.nextwakeup = 0; RtcSettings.deepsleep_slip = 10000; @@ -103,13 +103,13 @@ void DeepSleepPrepare(void) // Timeslip in 0.1 seconds between the real wakeup and the calculated wakeup // Because deepsleep is in second and timeslip in 0.1 sec the compare always check if the slip is in the 10% range - int16_t timeslip = (int16_t)(RtcSettings.nextwakeup + millis() / 1000 - UtcTime()) * 10; + int16_t timeslip = (int16_t)(RtcSettings.nextwakeup + millis() / 1000 - LocalTime()) * 10; // Allow 10% of deepsleep error to count as valid deepsleep; expecting 3-4% // if more then 10% timeslip = 0 == non valid wakeup; maybe manual timeslip = (timeslip < -(int32_t)Settings->deepsleep) ? 0 : (timeslip > (int32_t)Settings->deepsleep) ? 0 : 1; if (timeslip) { - RtcSettings.deepsleep_slip = (Settings->deepsleep + RtcSettings.nextwakeup - UtcTime()) * RtcSettings.deepsleep_slip / tmax((Settings->deepsleep - (millis() / 1000)),5); + RtcSettings.deepsleep_slip = (Settings->deepsleep + RtcSettings.nextwakeup - LocalTime()) * RtcSettings.deepsleep_slip / tmax((Settings->deepsleep - (millis() / 1000)),5); // Avoid crazy numbers. Again maximum 10% deviation. RtcSettings.deepsleep_slip = tmin(tmax(RtcSettings.deepsleep_slip, 9000), 11000); RtcSettings.nextwakeup += Settings->deepsleep; @@ -117,15 +117,15 @@ void DeepSleepPrepare(void) // It may happen that wakeup in just <5 seconds in future // In this case also add deepsleep to nextwakeup - if (RtcSettings.nextwakeup <= (UtcTime() - DEEPSLEEP_MIN_TIME)) { + if (RtcSettings.nextwakeup <= (LocalTime() - DEEPSLEEP_MIN_TIME)) { // ensure nextwakeup is at least in the future - RtcSettings.nextwakeup += (((UtcTime() + DEEPSLEEP_MIN_TIME - RtcSettings.nextwakeup) / Settings->deepsleep) + 1) * Settings->deepsleep; + RtcSettings.nextwakeup += (((LocalTime() + DEEPSLEEP_MIN_TIME - RtcSettings.nextwakeup) / Settings->deepsleep) + 1) * Settings->deepsleep; } - String dt = GetDT(RtcSettings.nextwakeup + LocalTime() - UtcTime()); // 2017-03-07T11:08:02 + String dt = GetDT(RtcSettings.nextwakeup); // 2017-03-07T11:08:02 // Limit sleeptime to DEEPSLEEP_MAX_CYCLE - // uint32_t deepsleep_sleeptime = DEEPSLEEP_MAX_CYCLE < (RtcSettings.nextwakeup - UtcTime()) ? (uint32_t)DEEPSLEEP_MAX_CYCLE : RtcSettings.nextwakeup - UtcTime(); - deepsleep_sleeptime = tmin((uint32_t)DEEPSLEEP_MAX_CYCLE ,RtcSettings.nextwakeup - UtcTime()); + // uint32_t deepsleep_sleeptime = DEEPSLEEP_MAX_CYCLE < (RtcSettings.nextwakeup - LocalTime()) ? (uint32_t)DEEPSLEEP_MAX_CYCLE : RtcSettings.nextwakeup - LocalTime(); + deepsleep_sleeptime = tmin((uint32_t)DEEPSLEEP_MAX_CYCLE ,RtcSettings.nextwakeup - LocalTime()); // stat/tasmota/DEEPSLEEP = {"DeepSleep":{"Time":"2019-11-12T21:33:45","Epoch":1573590825}} Response_P(PSTR("{\"" D_PRFX_DEEPSLEEP "\":{\"" D_JSON_TIME "\":\"%s\",\"Epoch\":%d}}"), (char*)dt.c_str(), RtcSettings.nextwakeup); @@ -140,7 +140,7 @@ void DeepSleepStart(void) AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_APPLICATION "Sleeping")); // Won't show in GUI WifiShutdown(); - RtcSettings.ultradeepsleep = RtcSettings.nextwakeup - UtcTime(); + RtcSettings.ultradeepsleep = RtcSettings.nextwakeup - LocalTime(); RtcSettingsSave(); RtcRebootReset(); #ifdef ESP8266 diff --git a/tasmota/xdrv_55_touch.ino b/tasmota/xdrv_55_touch.ino index 02d415e4a..d553f419c 100644 --- a/tasmota/xdrv_55_touch.ino +++ b/tasmota/xdrv_55_touch.ino @@ -133,17 +133,21 @@ void Touch_Check(void(*rotconvert)(int16_t *x, int16_t *y)) { #ifdef USE_FT5206 if (FT5206_found) { - touch_xp = FT5206_x(); - touch_yp = FT5206_y(); touched = FT5206_touched(); + if (touched) { + touch_xp = FT5206_x(); + touch_yp = FT5206_y(); + } } #endif // USE_FT5206 #ifdef USE_XPT2046 if (XPT2046_found) { - touch_xp = XPT2046_x(); - touch_yp = XPT2046_y(); touched = XPT2046_touched(); + if (touched) { + touch_xp = XPT2046_x(); + touch_yp = XPT2046_y(); + } } #endif // USE_XPT2046 diff --git a/tasmota/xdrv_86_esp32_sonoff_spm.ino b/tasmota/xdrv_86_esp32_sonoff_spm.ino index ef04efeba..225db6ccc 100644 --- a/tasmota/xdrv_86_esp32_sonoff_spm.ino +++ b/tasmota/xdrv_86_esp32_sonoff_spm.ino @@ -177,7 +177,7 @@ typedef struct { uint32_t crc32; // To detect file changes uint32_t version; // To detect driver function changes uint16_t module_map[SSPM_TOTAL_MODULES]; // Max possible SPM relay modules - float energy_total[SSPM_TOTAL_MODULES][4]; // Total energy in hecto kWh (30.12 kwH is stored as 3012) + float energy_total[SSPM_TOTAL_MODULES][4]; // Total energy in kWh - float allows up to 262143.99 kWh } tSspmSettings; typedef struct { @@ -211,6 +211,7 @@ typedef struct { uint8_t mstate; uint8_t last_button; uint8_t error_led_blinks; + uint8_t yesterday; uint8_t history_relay; uint8_t log_relay; bool map_change; @@ -723,12 +724,18 @@ void SSPMSendGetLog(uint32_t relay, uint32_t entries) { /*********************************************************************************************/ void SSPMHandleReceivedData(void) { - uint8_t command = SspmBuffer[16]; - bool ack = (0x80 == SspmBuffer[15]); - uint8_t command_sequence = SspmBuffer[19 + Sspm->expected_bytes]; + /* + 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 + AA 55 01 8b 34 32 37 39 37 34 13 4b 35 36 37 80 04 00 02 00 00 06 98 06 + Marker |Module id |Ac|Cm|Size | |Ix|Chksm| + */ + bool ack = (0x80 == SspmBuffer[15]); // Ac + uint32_t command = SspmBuffer[16]; // Cm + uint32_t expected_bytes = (SspmBuffer[17] << 8) + SspmBuffer[18]; // Size + uint32_t command_sequence = SspmBuffer[19 + expected_bytes]; // Ix // AddLog(LOG_LEVEL_DEBUG, PSTR("SPM: Rcvd ack %d, cmnd %d, seq %d, size %d"), -// ack, command, command_sequence, Sspm->expected_bytes); +// ack, command, command_sequence, expected_bytes); if (ack) { // Responses from ARM (Acked) @@ -739,7 +746,7 @@ void SSPMHandleReceivedData(void) { AA 55 01 00 00 00 00 00 00 00 00 00 00 00 00 80 00 00 01 00 00 fc 73 |Er| |St| */ - if ((1 == Sspm->expected_bytes) && (0 == SspmBuffer[19])) { + if ((1 == expected_bytes) && (0 == SspmBuffer[19])) { Sspm->mstate++; // Cycle to } break; @@ -755,7 +762,7 @@ void SSPMHandleReceivedData(void) { Marker |Module id |Ac|Cm|Size | |Ch|Ra|Max P |Min P |Max U |Min U |Max I |De|Ix|Chksm| | | | 4400W| 0.1W| 240V| 0.1V| 20A| | */ - if (0x02 == Sspm->expected_bytes) { + if (0x02 == expected_bytes) { } @@ -772,7 +779,7 @@ void SSPMHandleReceivedData(void) { AA 55 01 8b 34 32 37 39 37 34 13 4b 35 36 37 80 09 00 06 00 0f 01 01 01 01 05 fe 35 |OS|4RelayMasks| */ - if (0x06 == Sspm->expected_bytes) { + if (0x06 == expected_bytes) { // SspmBuffer[20] & 0x0F // Relays operational power_t current_state = SspmBuffer[20] >> 4; // Relays state power_t mask = 0x0000000F; @@ -797,7 +804,7 @@ void SSPMHandleReceivedData(void) { AA 55 01 6b 7e 32 37 39 37 34 13 4b 35 36 37 80 0b 00 02 00 00 09 bb c7 |?? ??| */ - if (0x02 == Sspm->expected_bytes) { + if (0x02 == expected_bytes) { } Sspm->module_selected++; @@ -852,8 +859,10 @@ void SSPMHandleReceivedData(void) { 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 42 67 46 */ - { - uint32_t entries = (Sspm->expected_bytes - 22) / 2; + if (expected_bytes < 24) { + AddLog(LOG_LEVEL_DEBUG, PSTR("SPM: History error%d"), SspmBuffer[19]); + } else { + uint32_t entries = (expected_bytes - 22) / 2; // Find last valid (= non-zero) entry in 6 month fifo buffer uint16_t energy = 0; while (!energy && --entries) { @@ -863,7 +872,11 @@ void SSPMHandleReceivedData(void) { uint32_t channel = SspmBuffer[32]; uint32_t module = SSPMGetModuleNumberFromMap(SspmBuffer[20] << 8 | SspmBuffer[21]); - +/* + if (!module && !channel) { + AddLog(LOG_LEVEL_DEBUG, PSTR("DBG: SSPMHistory1 '%50_H'"), SspmBuffer); + } +*/ if (Sspm->history_relay < 255) { uint32_t history_module = Sspm->history_relay >> 2; uint32_t history_channel = Sspm->history_relay & 0x03; // Channel relays are NOT bit masked this time @@ -891,12 +904,23 @@ void SSPMHandleReceivedData(void) { energy_total += energy; } } - if (0 == Sspm->Settings.energy_total[module][channel]) { // Initial save - Sspm->Settings.energy_total[module][channel] = energy_total; + if (0 == Sspm->Settings.energy_total[module][channel]) { + Sspm->Settings.energy_total[module][channel] = energy_total; // Initial setting } - if (Sspm->energy_today[module][channel] < last_energy_today) { // Daily incremental save - Sspm->Settings.energy_total[module][channel] += last_energy_today; + + // If received daily energy is below previous daily energy then update total energy + // This happens around midnight in normal situations + if (Sspm->energy_today[module][channel] < last_energy_today) { + Sspm->Settings.energy_total[module][channel] += last_energy_today; // Daily incremental save } +/* + if (255 == Sspm->yesterday) { Sspm->yesterday = SspmBuffer[36]; } // Initial setting + // If the day has changed then update total energy + if (Sspm->yesterday != SspmBuffer[36]) { // Next day + Sspm->Settings.energy_total[module][channel] += last_energy_today; // Daily incremental save + } + Sspm->yesterday = SspmBuffer[36]; +*/ Sspm->energy_total[module][channel] = Sspm->Settings.energy_total[module][channel] + Sspm->energy_today[module][channel]; if (Sspm->history_relay < 255) { @@ -939,7 +963,7 @@ void SSPMHandleReceivedData(void) { uint32_t module = Sspm->log_relay >> 2; uint32_t channel = Sspm->log_relay & 0x03; // Channel relays are NOT bit masked this time Response_P(PSTR("{\"SSPMLog%d\":"), Sspm->log_relay +1); - if (Sspm->expected_bytes < 15) { + if (expected_bytes < 15) { ResponseAppend_P(PSTR("\"Error%d\"}"), SspmBuffer[19]); } else if (module != SSPMGetModuleNumberFromMap(SspmBuffer[20] << 8 | SspmBuffer[21])) { ResponseAppend_P(PSTR("\"Wrong module\"}")); @@ -1065,7 +1089,7 @@ void SSPMHandleReceivedData(void) { |130| 1.0.0| 20A| 0.1A| 240V| 0.1V| 4400W| 0.1W| Ty = Type of sub-device. 130: Four-channel sub-device */ - if ((0x24 == Sspm->expected_bytes) && (Sspm->module_max < SSPM_MAX_MODULES)) { + if ((0x24 == expected_bytes) && (Sspm->module_max < SSPM_MAX_MODULES)) { memcpy(Sspm->module[Sspm->module_max], SspmBuffer + 19, SSPM_MODULE_NAME_SIZE); uint32_t module_id = SspmBuffer[19] << 8 | SspmBuffer[20]; if (0 == Sspm->Settings.module_map[Sspm->module_max]) { @@ -1169,10 +1193,10 @@ void SSPMSerialInput(void) { } if (Sspm->serial_in_byte_counter < SSPM_SERIAL_BUFFER_SIZE -1) { SspmBuffer[Sspm->serial_in_byte_counter++] = serial_in_byte; - if (19 == Sspm->serial_in_byte_counter) { - Sspm->expected_bytes = (SspmBuffer[Sspm->serial_in_byte_counter -2] << 8) + SspmBuffer[Sspm->serial_in_byte_counter -1]; + if ((0xAA == SspmBuffer[0]) && (0x55 == SspmBuffer[1]) && (19 == Sspm->serial_in_byte_counter)) { + Sspm->expected_bytes = 22 + (SspmBuffer[17] << 8) + SspmBuffer[18]; } - if (Sspm->serial_in_byte_counter == (22 + Sspm->expected_bytes)) { + if (Sspm->serial_in_byte_counter == Sspm->expected_bytes) { AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("SPM: ARM %*_H"), Sspm->serial_in_byte_counter, SspmBuffer); @@ -1237,6 +1261,7 @@ void SSPMInit(void) { #endif #endif + Sspm->yesterday = 255; // Not initialized Sspm->history_relay = 255; // Disable display energy history Sspm->log_relay = 255; // Disable display logging Sspm->old_power = TasmotaGlobal.power; @@ -1537,7 +1562,7 @@ void (* const SSPMCommand[])(void) PROGMEM = { &CmndSSPMDisplay, &CmndSSPMReset, &CmndSSPMMap, &CmndSpmEnergyTotal }; void CmndSpmEnergyTotal(void) { - // Reset Energy Total + // Reset Energy Total // SspmEnergyTotal 0 - Set total energy from midnight with sum of last month history // SspmEnergyTotal 4.23 - Set total energy from midnight (without today's energy) uint32_t relay = XdrvMailbox.index -1;