From 3ce011c2cebd9c20228c1531e0b568ef39635083 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Mon, 20 Feb 2023 18:02:43 +0100 Subject: [PATCH] Attempt to stabilize Shelly Pro 4PM values --- .../xdrv_88_esp32_shelly_pro.ino | 6 ----- .../tasmota_xnrg_energy/xnrg_07_ade7953.ino | 26 ++++++++++++++----- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_88_esp32_shelly_pro.ino b/tasmota/tasmota_xdrv_driver/xdrv_88_esp32_shelly_pro.ino index c93c1a077..16dd8db3e 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_88_esp32_shelly_pro.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_88_esp32_shelly_pro.ino @@ -272,10 +272,6 @@ void ShellyProUpdateIsr(void) { /* The goal if this function is to minimize SPI and SetVirtualPinState calls */ - noInterrupts(); - detachInterrupt(SPro.pin_mcp23s17_int); - interrupts(); - uint32_t input_state = SP4Mcp23S17Read16(SP4_MCP23S17_INTCAPA); // Read intcap and clear interrupt input_state &= 0x806F; // Only test input bits @@ -302,8 +298,6 @@ void ShellyProUpdateIsr(void) { mask <<= 1; } SPro.input_state = input_state; - - attachInterrupt(SPro.pin_mcp23s17_int, ShellyProUpdateIsr, CHANGE); } bool ShellyProButton(void) { diff --git a/tasmota/tasmota_xnrg_energy/xnrg_07_ade7953.ino b/tasmota/tasmota_xnrg_energy/xnrg_07_ade7953.ino index 935a5a7f0..78fb0fcc0 100644 --- a/tasmota/tasmota_xnrg_energy/xnrg_07_ade7953.ino +++ b/tasmota/tasmota_xnrg_energy/xnrg_07_ade7953.ino @@ -231,6 +231,7 @@ typedef struct { } tAde7953Channel; struct Ade7953 { + uint32_t last_update; uint32_t voltage_rms[ADE7953_MAX_CHANNEL] = { 0, 0 }; uint32_t current_rms[ADE7953_MAX_CHANNEL] = { 0, 0 }; uint32_t active_power[ADE7953_MAX_CHANNEL] = { 0, 0 }; @@ -523,6 +524,19 @@ void Ade7953GetData(void) { } if (Energy->power_on) { // Powered on + + float correction = 1.0f; + if (Ade7953.use_spi) { // SPI + uint32_t time = millis(); + if (Ade7953.last_update) { + uint32_t difference = time - Ade7953.last_update; + correction = (float)difference / 1000; // Correction to 1 second + +// AddLog(LOG_LEVEL_DEBUG, PSTR("ADE: Correction %4_f"), &correction); + } + Ade7953.last_update = time; + } + float divider; for (uint32_t channel = 0; channel < Energy->phase_count; channel++) { Energy->data_valid[channel] = 0; @@ -557,6 +571,12 @@ void Ade7953GetData(void) { divider = (Ade7953.calib_data[channel][ADE7953_CAL_VAGAIN] != ADE7953_GAIN_DEFAULT) ? ADE7953_LSB_PER_WATTSECOND : power_calibration; Energy->apparent_power[channel] = (float)apparent_power[channel] / divider; + if (Ade7953.use_spi) { // SPI + Energy->active_power[channel] /= correction; + Energy->reactive_power[channel] /= correction; + Energy->apparent_power[channel] /= correction; + } + if (0 == Energy->active_power[channel]) { Energy->current[channel] = 0; } else { @@ -836,7 +856,6 @@ bool Xnrg07(uint32_t function) { bool result = false; switch (function) { -/* case FUNC_ENERGY_EVERY_SECOND: // Use energy interrupt timer (fails on SPI) if (!Ade7953.use_spi) { // No SPI Ade7953EnergyEverySecond(); @@ -847,11 +866,6 @@ bool Xnrg07(uint32_t function) { Ade7953EnergyEverySecond(); } break; -*/ - case FUNC_ENERGY_EVERY_SECOND: // Use energy interrupt timer (fails on SPI) - Ade7953EnergyEverySecond(); - break; - case FUNC_COMMAND: result = Ade7953Command(); break;