From 15c347669833ef03bbb616124a3fc7d162998f20 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Wed, 26 Jan 2022 13:59:55 +0100 Subject: [PATCH 1/2] Add more error checking to SPM --- tasmota/xdrv_86_esp32_sonoff_spm.ino | 69 +++++++++++++++++++--------- 1 file changed, 47 insertions(+), 22 deletions(-) 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; From a0a38f638af840d8dc8e9ad6e762844459187a55 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Wed, 26 Jan 2022 16:20:48 +0100 Subject: [PATCH 2/2] Update touchscreen x/y only when touched --- tasmota/xdrv_55_touch.ino | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) 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