From f7a8b1f9234ee628596b0b00d3736753016cae3b Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Fri, 21 Sep 2018 16:02:56 +0200 Subject: [PATCH] Shelly2 clean up Shelly2 clean up --- sonoff/i18n.h | 2 +- sonoff/user_config.h | 2 +- sonoff/xdrv_03_energy.ino | 2 +- sonoff/xnrg_04_mcp39f501.ino | 36 ++++++++++-------------------------- 4 files changed, 13 insertions(+), 29 deletions(-) diff --git a/sonoff/i18n.h b/sonoff/i18n.h index 4ea5ab3fe..b0a7ec071 100644 --- a/sonoff/i18n.h +++ b/sonoff/i18n.h @@ -442,7 +442,7 @@ const char kUnitNames[] PROGMEM = D_UNIT_VOLT "|" D_UNIT_WATT "|" D_UNIT_WATTHOUR "|" - "d" D_UNIT_HERTZ ; + D_UNIT_HERTZ ; const char S_JSON_COMMAND_NVALUE_SPACE_UNIT[] PROGMEM = "{\"%s\":\"%d %s\"}"; const char S_JSON_COMMAND_LVALUE_SPACE_UNIT[] PROGMEM = "{\"%s\":\"%lu %s\"}"; diff --git a/sonoff/user_config.h b/sonoff/user_config.h index ef3e66fc2..438983a66 100644 --- a/sonoff/user_config.h +++ b/sonoff/user_config.h @@ -348,7 +348,7 @@ // Power monitoring sensors ----------------------- #define USE_PZEM004T // Add support for PZEM004T Energy monitor (+2k code) #define USE_PZEM2 // Add support for PZEM003,014,016,017 Energy monitor (+1k1 code) -#define USE_MCP39F501 // Add support for MCP39F501 Energy monitor as used in Shelly 2 (+3k2 code) +#define USE_MCP39F501 // Add support for MCP39F501 Energy monitor as used in Shelly 2 (+3k1 code) // -- Low level interface devices ----------------- #define USE_IR_REMOTE // Send IR remote commands using library IRremoteESP8266 and ArduinoJson (+4k code, 0k3 mem, 48 iram) diff --git a/sonoff/xdrv_03_energy.ino b/sonoff/xdrv_03_energy.ino index 068261971..860183f49 100644 --- a/sonoff/xdrv_03_energy.ino +++ b/sonoff/xdrv_03_energy.ino @@ -451,7 +451,7 @@ boolean EnergyCommand() nvalue = Settings.energy_current_calibration; unit = UNIT_MICROSECOND; } - else if ((CMND_FREQUENCYSET == command_code) && XnrgCall(FUNC_COMMAND)) { // dHz + else if ((CMND_FREQUENCYSET == command_code) && XnrgCall(FUNC_COMMAND)) { // Hz nvalue = Settings.energy_frequency_calibration; unit = UNIT_HERTZ; } diff --git a/sonoff/xnrg_04_mcp39f501.ino b/sonoff/xnrg_04_mcp39f501.ino index a9620ce50..c34846edf 100644 --- a/sonoff/xnrg_04_mcp39f501.ino +++ b/sonoff/xnrg_04_mcp39f501.ino @@ -56,12 +56,6 @@ #define MCP_FREQUENCY_GAIN_BASE 0x00AE #define MCP_FREQUENCY_LEN 4 -#define EMTR_OUT_BASE 0x0004 -#define EMTR_OUT_LEN 28 - -#define MCP_FLASH_READ 0x42 -#define MCP_FLASH_WRITE 0x50 - typedef struct mcp_calibration_registers_type { uint16_t gain_current_rms; uint16_t gain_voltage_rms; @@ -161,16 +155,6 @@ void McpSetInt(unsigned long value, uint8_t *data, uint8_t offset, size_t size) } } -void AddLogSerialSend(byte loglevel, uint8_t *buffer, int count) -{ - snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SERIAL "Send")); - for (int i = 0; i < count; i++) { - snprintf_P(log_data, sizeof(log_data), PSTR("%s %02X"), log_data, *(buffer++)); - } - AddLog(loglevel); -} - - void McpSend(uint8_t *data) { if (mcp_timeout) { return; } @@ -179,7 +163,7 @@ void McpSend(uint8_t *data) data[0] = MCP_START_FRAME; data[data[1] -1] = McpChecksum(data); -// AddLogSerialSend(LOG_LEVEL_DEBUG_MORE, data, data[1]); +// AddLogSerial(LOG_LEVEL_DEBUG_MORE, data, data[1]); for (byte i = 0; i < data[1]; i++) { Serial.write(data[i]); @@ -252,7 +236,7 @@ calc: void McpCalibrationReactivePower() { - mcp_calibration_registers.gain_reactive_power = mcp_calibration_registers.gain_reactive_power * mcp_calibration_setpoint.calibration_reactive_power / mcp_output_registers.reactive_power; + mcp_calibration_registers.gain_reactive_power = mcp_calibration_registers.gain_reactive_power * mcp_calibration_registers.calibration_reactive_power / mcp_output_registers.reactive_power; } void McpCalibrationLineFreqency() @@ -605,6 +589,11 @@ void McpEverySecond() { uint8_t get_state[] = { 0xA5, 0x08, 0x41, 0x00, 0x04, 0x4E, 0x16, 0x00 }; + if (mcp_output_registers.active_power) { + energy_kWhtoday_delta += ((mcp_output_registers.active_power * 10) / 36); + EnergyUpdateToday(); + } + if (mcp_timeout) { mcp_timeout--; } @@ -621,11 +610,6 @@ void McpEverySecond() else if (!mcp_single_wire_active) { McpSend(get_state); } - - if (mcp_output_registers.active_power) { - energy_kWhtoday_delta += ((mcp_output_registers.active_power * 10) / 36); - EnergyUpdateToday(); - } } void McpSnsInit() @@ -656,9 +640,9 @@ boolean McpCommand() if ((CMND_POWERCAL == energy_command_code) || (CMND_VOLTAGECAL == energy_command_code) || (CMND_CURRENTCAL == energy_command_code)) { // MCP Debug commands - PowerCal - if (1 == XdrvMailbox.payload) { McpSingleWireStart(); } - if (2 == XdrvMailbox.payload) { McpSingleWireStop(0); } - if (3 == XdrvMailbox.payload) { McpGetAddress(); } +// if (1 == XdrvMailbox.payload) { McpSingleWireStart(); } +// if (2 == XdrvMailbox.payload) { McpSingleWireStop(0); } +// if (3 == XdrvMailbox.payload) { McpGetAddress(); } serviced = false; }