diff --git a/lib/default/TasmotaSerial-3.3.0/src/TasmotaSerial.cpp b/lib/default/TasmotaSerial-3.3.0/src/TasmotaSerial.cpp index 253c3094b..921ebff30 100644 --- a/lib/default/TasmotaSerial-3.3.0/src/TasmotaSerial.cpp +++ b/lib/default/TasmotaSerial-3.3.0/src/TasmotaSerial.cpp @@ -68,6 +68,7 @@ TasmotaSerial::TasmotaSerial(int receive_pin, int transmit_pin, int hardware_fal m_hardswap = true; } else { + if ((m_rx_pin < 0) && (m_tx_pin < 0)) { return; } if (m_rx_pin > -1) { m_buffer = (uint8_t*)malloc(serial_buffer_size); if (m_buffer == NULL) return; @@ -110,6 +111,7 @@ bool TasmotaSerial::isValidGPIOpin(int pin) { } bool TasmotaSerial::begin(uint32_t speed, uint32_t config) { + if (!m_valid) { return false; } if (config > 2) { // Legacy support where software serial fakes two stop bits if either stop bits is 2 or parity is not None m_stop_bits = ((config &0x30) >> 5) +1; diff --git a/tasmota/xdrv_03_energy.ino b/tasmota/xdrv_03_energy.ino index 71638bc15..1e9a50fc7 100644 --- a/tasmota/xdrv_03_energy.ino +++ b/tasmota/xdrv_03_energy.ino @@ -510,30 +510,32 @@ void EnergyEverySecond(void) } // Invalid data reset - uint32_t data_valid = Energy.phase_count; - for (uint32_t i = 0; i < Energy.phase_count; i++) { - if (Energy.data_valid[i] <= ENERGY_WATCHDOG) { - Energy.data_valid[i]++; - if (Energy.data_valid[i] > ENERGY_WATCHDOG) { - // Reset energy registers - Energy.voltage[i] = 0; - Energy.current[i] = 0; - Energy.active_power[i] = 0; - if (!isnan(Energy.apparent_power[i])) { Energy.apparent_power[i] = 0; } - if (!isnan(Energy.reactive_power[i])) { Energy.reactive_power[i] = 0; } - if (!isnan(Energy.frequency[i])) { Energy.frequency[i] = 0; } - if (!isnan(Energy.power_factor[i])) { Energy.power_factor[i] = 0; } - if (!isnan(Energy.export_active[i])) { Energy.export_active[i] = 0; } + if (TasmotaGlobal.uptime > 3) { + uint32_t data_valid = Energy.phase_count; + for (uint32_t i = 0; i < Energy.phase_count; i++) { + if (Energy.data_valid[i] <= ENERGY_WATCHDOG) { + Energy.data_valid[i]++; + if (Energy.data_valid[i] > ENERGY_WATCHDOG) { + // Reset energy registers + Energy.voltage[i] = 0; + Energy.current[i] = 0; + Energy.active_power[i] = 0; + if (!isnan(Energy.apparent_power[i])) { Energy.apparent_power[i] = 0; } + if (!isnan(Energy.reactive_power[i])) { Energy.reactive_power[i] = 0; } + if (!isnan(Energy.frequency[i])) { Energy.frequency[i] = 0; } + if (!isnan(Energy.power_factor[i])) { Energy.power_factor[i] = 0; } + if (!isnan(Energy.export_active[i])) { Energy.export_active[i] = 0; } - data_valid--; + data_valid--; + } } } - } - if (!data_valid) { - //Energy.start_energy = 0; - AddLog(LOG_LEVEL_DEBUG, PSTR("NRG: Energy reset by " STR(ENERGY_WATCHDOG) " seconds invalid data")); + if (!data_valid) { + //Energy.start_energy = 0; + AddLog(LOG_LEVEL_DEBUG, PSTR("NRG: Energy reset by " STR(ENERGY_WATCHDOG) " seconds invalid data")); - XnrgCall(FUNC_ENERGY_RESET); + XnrgCall(FUNC_ENERGY_RESET); + } } #ifdef USE_ENERGY_MARGIN_DETECTION diff --git a/tasmota/xnrg_19_cse7761.ino b/tasmota/xnrg_19_cse7761.ino index e8601b46f..3d29ca858 100644 --- a/tasmota/xnrg_19_cse7761.ino +++ b/tasmota/xnrg_19_cse7761.ino @@ -21,18 +21,16 @@ #ifdef USE_CSE7761 /*********************************************************************************************\ * CSE7761 - Energy (Sonoff Dual R3 Pow) + * {"NAME":"Sonoff Dual R3","GPIO":[0,0,1,0,0,0,3232,3200,0,0,225,0,0,0,0,0,0,0,0,0,1,7296,7328,224,0,0,0,0,160,161,0,0,0,0,0,0],"FLAG":0,"BASE":1} * - * Based on datasheet from ChipSea + * Based on datasheet from ChipSea and analysing serial data + * See https://github.com/arendst/Tasmota/discussions/10793 \*********************************************************************************************/ #define XNRG_19 19 //#define CSE7761_SIMULATE -#define CSE7761_DUAL_K1 1 // Current channel sampling resistance in milli Ohm -#define CSE7761_DUAL_K2 1 // Voltage divider resistance in 1k/1M -#define CSE7761_DUAL_CLK1 3579545 // System clock (3.579545MHz) used in frequency calculation - #define CSE7761_UREF 10000 // Gain 1 * 10000 in V #define CSE7761_IREF 160000 // Gain 16 * 10000 in A #define CSE7761_PREF 50000 // in W @@ -83,6 +81,7 @@ struct { uint32_t current_rms[2] = { 0 }; uint32_t energy[2] = { 0 }; uint32_t active_power[2] = { 0 }; + uint8_t energy_update = 0; uint8_t init = 4; uint8_t ready = 0; } CSE7761Data; @@ -157,30 +156,23 @@ uint32_t Cse7761Read(uint32_t reg) { } bool Cse7761ChipInit(void) { - uint16_t coefficient[8] = { 0 }; uint16_t calc_chksum = 0xFFFF; for (uint32_t i = 0; i < 8; i++) { - coefficient[i] = Cse7761Read(CSE7761_REG_RMSIAC + i); - calc_chksum += coefficient[i]; + calc_chksum = Cse7761Read(CSE7761_REG_RMSIAC + i); } calc_chksum = ~calc_chksum; uint16_t dummy = Cse7761Read(CSE7761_REG_COEFFOFFSET); uint16_t coeff_chksum = Cse7761Read(CSE7761_REG_COEFFCHKSUM); if (calc_chksum != coeff_chksum) { - AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Default coefficients")); - coefficient[RmsIAC] = 0xCC11; - coefficient[RmsUC] = 0xA643; - coefficient[PowerPAC] = 0xADE1; - } - if (HLW_PREF_PULSE == Settings.energy_power_calibration) { -// Settings.energy_frequency_calibration = 2750; - Settings.energy_voltage_calibration = CSE7761_UREF; - Settings.energy_current_calibration = CSE7761_IREF; - Settings.energy_power_calibration = CSE7761_PREF; + AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Not factory calibrated")); } Cse7761Write(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_ENABLE_WRITE); - delay(8); + +// delay(8); // Exception on ESP8266 + uint32_t timeout = millis() + 8; + while (!TimeReached(timeout)) { } + uint8_t sys_status = Cse7761Read(CSE7761_REG_SYSSTATUS); #ifdef CSE7761_SIMULATE sys_status = 0x11; @@ -322,7 +314,8 @@ void Cse7761GetData(void) { // The active power parameter PowerA/B is in two’s complement format, 32-bit data, the highest bit is Sign bit. uint32_t value = Cse7761Read(CSE7761_REG_RMSU); #ifdef CSE7761_SIMULATE - value = 2342160; +// value = 2342160; // 234.2V + value = 2000000; // 200V #endif CSE7761Data.voltage_rms = (value >= 0x800000) ? 0 : value; @@ -339,12 +332,14 @@ void Cse7761GetData(void) { value = Cse7761Read(CSE7761_REG_RMSIB); #ifdef CSE7761_SIMULATE - value = 29760; +// value = 29760; // 0.186A + value = 800000; // 5A #endif CSE7761Data.current_rms[1] = ((value >= 0x800000) || (value < 1600)) ? 0 : value; // No load threshold of 10mA value = Cse7761Read(CSE7761_REG_POWERPB); #ifdef CSE7761_SIMULATE - value = 2126641; +// value = 2126641; // 42.5W + value = 50000000; // 1000W #endif CSE7761Data.active_power[1] = (0 == CSE7761Data.current_rms[1]) ? 0 : (value & 0x80000000) ? (~value) + 1 : value; @@ -354,8 +349,8 @@ void Cse7761GetData(void) { CSE7761Data.active_power[0], CSE7761Data.active_power[1]); if (Energy.power_on) { // Powered on - Energy.voltage[0] = ((float)CSE7761Data.voltage_rms / Settings.energy_voltage_calibration); // V // Energy.frequency[0] = (float)Settings.energy_frequency_calibration / ((float)CSE7761Data.frequency + 1); // Hz + Energy.voltage[0] = ((float)CSE7761Data.voltage_rms / Settings.energy_voltage_calibration); // V for (uint32_t channel = 0; channel < 2; channel++) { Energy.data_valid[channel] = 0; @@ -365,6 +360,7 @@ void Cse7761GetData(void) { } else { Energy.current[channel] = (float)CSE7761Data.current_rms[channel] / Settings.energy_current_calibration; // A CSE7761Data.energy[channel] += Energy.active_power[channel]; + CSE7761Data.energy_update++; } } } else { // Powered off @@ -406,13 +402,16 @@ void Cse7761EverySecond(void) { } else { if (2 == CSE7761Data.ready) { - uint32_t energy_sum = (CSE7761Data.energy[0] + CSE7761Data.energy[1]) * 1000; - if (energy_sum) { - Energy.kWhtoday_delta += energy_sum / 36; - EnergyUpdateToday(); - CSE7761Data.energy[0] = 0; - CSE7761Data.energy[1] = 0; + if (CSE7761Data.energy_update) { + uint32_t energy_sum = ((CSE7761Data.energy[0] + CSE7761Data.energy[1]) * 1000) / CSE7761Data.energy_update; + if (energy_sum) { + Energy.kWhtoday_delta += energy_sum / 36; + EnergyUpdateToday(); + } } + CSE7761Data.energy[0] = 0; + CSE7761Data.energy[1] = 0; + CSE7761Data.energy_update = 0; } } } @@ -425,6 +424,12 @@ void Cse7761SnsInit(void) { SetSerial(38400, TS_SERIAL_8E1); ClaimSerial(); } + if (HLW_PREF_PULSE == Settings.energy_power_calibration) { +// Settings.energy_frequency_calibration = 2750; + Settings.energy_voltage_calibration = CSE7761_UREF; + Settings.energy_current_calibration = CSE7761_IREF; + Settings.energy_power_calibration = CSE7761_PREF; + } } else { TasmotaGlobal.energy_driver = ENERGY_NONE; } @@ -462,7 +467,7 @@ bool Cse7761Command(void) { else if (CMND_POWERSET == Energy.command_code) { if (XdrvMailbox.data_len && CSE7761Data.active_power[channel]) { if ((value > 100) && (value < 200000)) { // Between 1W and 2000W - Settings.energy_power_calibration = (CSE7761Data.active_power[channel] * 100) / value; + Settings.energy_power_calibration = ((CSE7761Data.active_power[channel]) / value) * 100; } } } diff --git a/tools/Esptool/Odroid_go_and_core2/Core2_flash.bat b/tools/Esptool/Odroid_go_and_core2/Core2_flash.bat index b9ba35654..85c0d6412 100644 --- a/tools/Esptool/Odroid_go_and_core2/Core2_flash.bat +++ b/tools/Esptool/Odroid_go_and_core2/Core2_flash.bat @@ -1 +1 @@ -esptool.py --chip esp32 --baud 2000000 --before default_reset --after hard_reset write_flash -z --flash_mode qio --flash_freq 80m --flash_size detect 0x1000 bootloader_qio_80m.bin 0x8000 partitions_spiffs_12M.bin 0xe000 boot_app0.bin 0x10000 tasmota32-core2.bin \ No newline at end of file +esptool.py --chip esp32 --baud 2000000 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 80m --flash_size detect 0x1000 bootloader_qio_80m.bin 0x8000 partitions_spiffs_12M.bin 0xe000 boot_app0.bin 0x10000 tasmota32-core2.bin diff --git a/tools/Esptool/Odroid_go_and_core2/Odroid_flash.bat b/tools/Esptool/Odroid_go_and_core2/Odroid_flash.bat index 001850a1f..ba16e32f9 100644 --- a/tools/Esptool/Odroid_go_and_core2/Odroid_flash.bat +++ b/tools/Esptool/Odroid_go_and_core2/Odroid_flash.bat @@ -1 +1 @@ -esptool.py --chip esp32 --baud 2000000 --before default_reset --after hard_reset write_flash -z --flash_mode qio --flash_freq 80m --flash_size detect 0x1000 bootloader_qio_80m.bin 0x8000 partitions_spiffs_12M.bin 0xe000 boot_app0.bin 0x10000 tasmota32-odroid.bin \ No newline at end of file +esptool.py --chip esp32 --baud 2000000 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 80m --flash_size detect 0x1000 bootloader_qio_80m.bin 0x8000 partitions_spiffs_12M.bin 0xe000 boot_app0.bin 0x10000 tasmota32-odroid.bin