diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index ae249c610..b4bff23ed 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -1,4 +1,8 @@ /*********************************************************************************************\ + * 6.6.0.12 20190910 + * Redesign command Tariff to now default to 0 (=disabled) and allowing to set both Standard Time (ST) and Daylight Savings Time (DST) start hour + * Commands Tariff1 23 = Tariff1 ST, Tariff2 7 = Tariff2 ST, Tarriff3 22 = Tarrif1 DST, Tariff4 6 = Tariff2 DST, Tariff9 0/1 = Weekend toggle + * * 6.6.0.11 20190907 * Change Settings crc calculation allowing short term backward compatibility * Add support for up to 4 INA226 Voltage and Current sensors by Steve Rogers (#6342) diff --git a/sonoff/settings.h b/sonoff/settings.h index eb58877cb..9fdfb1e34 100644 --- a/sonoff/settings.h +++ b/sonoff/settings.h @@ -227,9 +227,7 @@ struct SYSCFG { uint8_t weblog_level; // 1AC uint8_t mqtt_fingerprint[2][20]; // 1AD uint8_t adc_param_type; // 1D5 - - uint8_t free_1D6[18]; // 1D6 Free since 5.12.0e - + uint8_t register8[18]; // 1D6 - 18 x 8-bit registers indexed by enum SettingsRegister8 uint8_t sps30_inuse_hours; // 1E8 char mqtt_host[33]; // 1E9 - Keep together with below as being copied as one chunck with reset 6 uint16_t mqtt_port; // 20A - Keep together diff --git a/sonoff/settings.ino b/sonoff/settings.ino index 9534c0ab6..289c8392b 100644 --- a/sonoff/settings.ino +++ b/sonoff/settings.ino @@ -128,15 +128,6 @@ #ifndef TUYA_DIMMER_MAX #define TUYA_DIMMER_MAX 100 #endif -#ifndef ENERGY_TARIFF1_HOUR -#define ENERGY_TARIFF1_HOUR 23 // Start hour "nighttime" or "off-peak" tariff -#endif -#ifndef ENERGY_TARIFF2_HOUR -#define ENERGY_TARIFF2_HOUR 7 // Start hour "daytime" or "standard" tariff -#endif -#ifndef ENERGY_TARIFF_WEEKEND -#define ENERGY_TARIFF_WEEKEND 1 // 0 = No difference in weekend, 1 = off-peak during weekend -#endif enum WebColors { COL_TEXT, COL_BACKGROUND, COL_FORM, @@ -1097,9 +1088,6 @@ void SettingsDelta(void) } else { Settings.param[P_TUYA_DIMMER_MAX] = 255; } - Settings.param[P_ENERGY_TARIFF1] = ENERGY_TARIFF1_HOUR; - Settings.param[P_ENERGY_TARIFF2] = ENERGY_TARIFF2_HOUR; - Settings.flag3.energy_weekend = ENERGY_TARIFF_WEEKEND; } if (Settings.version < 0x06060009) { Settings.baudrate = Settings.ex_baudrate * 4; @@ -1139,8 +1127,11 @@ void SettingsDelta(void) Settings.tuya_fnid_map[tuyaindex].dpid = Settings.param[P_ex_TUYA_CURRENT_ID]; tuyaindex++; } - } + if (Settings.version < 0x0606000C) { + memset(&Settings.register8, 0x00, sizeof(Settings.register8)); + } + Settings.version = VERSION; SettingsSave(1); } diff --git a/sonoff/sonoff.h b/sonoff/sonoff.h index e773bf7a7..89e3c53f3 100644 --- a/sonoff/sonoff.h +++ b/sonoff/sonoff.h @@ -245,11 +245,18 @@ enum ButtonStates { PRESSED, NOT_PRESSED }; enum Shortcuts { SC_CLEAR, SC_DEFAULT, SC_USER }; -enum SettingsParamIndex {P_HOLD_TIME, P_MAX_POWER_RETRY, P_ex_TUYA_DIMMER_ID, P_MDNS_DELAYED_START, P_BOOT_LOOP_OFFSET, P_RGB_REMAP, P_IR_UNKNOW_THRESHOLD, // SetOption32 .. SetOption38 - P_CSE7766_INVALID_POWER, P_HOLD_IGNORE, P_ex_TUYA_RELAYS, P_OVER_TEMP, // SetOption39 .. SetOption42 - P_TUYA_DIMMER_MAX, P_ex_TUYA_VOLTAGE_ID, P_ex_TUYA_CURRENT_ID, P_ex_TUYA_POWER_ID, // SetOption43 .. SetOption46 - P_ENERGY_TARIFF1, P_ENERGY_TARIFF2, // SetOption47 .. SetOption48 - P_MAX_PARAM8}; // Max is PARAM8_SIZE (18) - SetOption32 until SetOption49 +enum SettingsParamIndex { P_HOLD_TIME, P_MAX_POWER_RETRY, P_ex_TUYA_DIMMER_ID, P_MDNS_DELAYED_START, P_BOOT_LOOP_OFFSET, P_RGB_REMAP, P_IR_UNKNOW_THRESHOLD, // SetOption32 .. SetOption38 + P_CSE7766_INVALID_POWER, P_HOLD_IGNORE, P_ex_TUYA_RELAYS, P_OVER_TEMP, // SetOption39 .. SetOption42 + P_TUYA_DIMMER_MAX, + P_ex_TUYA_VOLTAGE_ID, P_ex_TUYA_CURRENT_ID, P_ex_TUYA_POWER_ID, // SetOption43 .. SetOption46 + P_ex_ENERGY_TARIFF1, P_ex_ENERGY_TARIFF2, // SetOption47 .. SetOption48 + P_MAX_PARAM8 }; // Max is PARAM8_SIZE (18) - SetOption32 until SetOption49 + +enum SettingsRegister8 { R8_ENERGY_TARIFF1_ST, R8_ENERGY_TARIFF2_ST, R8_ENERGY_TARIFF1_DS, R8_ENERGY_TARIFF2_DS, + R8_SPARE04, R8_SPARE05, R8_SPARE06, R8_SPARE07, + R8_SPARE08, R8_SPARE09, R8_SPARE10, R8_SPARE11, + R8_SPARE12, R8_SPARE13, R8_SPARE14, R8_SPARE15, + R8_SPARE16, R8_SPARE17 }; // Max size is 18 (Settings.register8[]) enum DomoticzSensors {DZ_TEMP, DZ_TEMP_HUM, DZ_TEMP_HUM_BARO, DZ_POWER_ENERGY, DZ_ILLUMINANCE, DZ_COUNT, DZ_VOLTAGE, DZ_CURRENT, DZ_AIRQUALITY, DZ_P1_SMART_METER, DZ_MAX_SENSORS}; diff --git a/sonoff/sonoff_version.h b/sonoff/sonoff_version.h index b7eae8a8f..dc8f2f2e9 100644 --- a/sonoff/sonoff_version.h +++ b/sonoff/sonoff_version.h @@ -20,6 +20,6 @@ #ifndef _SONOFF_VERSION_H_ #define _SONOFF_VERSION_H_ -const uint32_t VERSION = 0x0606000B; +const uint32_t VERSION = 0x0606000C; #endif // _SONOFF_VERSION_H_ diff --git a/sonoff/support_rtc.ino b/sonoff/support_rtc.ino index e75046598..961869687 100644 --- a/sonoff/support_rtc.ino +++ b/sonoff/support_rtc.ino @@ -299,7 +299,7 @@ void BreakTime(uint32_t time_input, TIME_T &tm) strlcpy(tm.name_of_month, kMonthNames + (month *3), 4); tm.month = month + 1; // jan is month 1 tm.day_of_month = time + 1; // day of month - tm.valid = (time_input > 1451602800); // 2016-01-01 + tm.valid = (time_input > START_VALID_TIME); // 2016-01-01 } uint32_t MakeTime(TIME_T &tm) @@ -374,9 +374,9 @@ void RtcSecond(void) uint8_t offset = (uptime < 30) ? RtcTime.second : (((ESP.getChipId() & 0xF) * 3) + 3) ; // First try ASAP to sync. If fails try once every 60 seconds based on chip id if (!global_state.wifi_down && (((offset == RtcTime.second) && ((RtcTime.year < 2016) || (Rtc.ntp_sync_minute == RtcTime.minute))) || ntp_force_sync)) { Rtc.ntp_time = sntp_get_current_timestamp(); - if (Rtc.ntp_time > 1451602800) { // Fix NTP bug in core 2.4.1/SDK 2.2.1 (returns Thu Jan 01 08:00:10 1970 after power on) + if (Rtc.ntp_time > START_VALID_TIME) { // Fix NTP bug in core 2.4.1/SDK 2.2.1 (returns Thu Jan 01 08:00:10 1970 after power on) ntp_force_sync = false; - if (Rtc.utc_time > 1451602800) { Rtc.drift_time = Rtc.ntp_time - Rtc.utc_time; } + if (Rtc.utc_time > START_VALID_TIME) { Rtc.drift_time = Rtc.ntp_time - Rtc.utc_time; } Rtc.utc_time = Rtc.ntp_time; Rtc.ntp_sync_minute = 60; // Sync so block further requests if (Rtc.restart_time == 0) { @@ -391,7 +391,7 @@ void RtcSecond(void) // AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION "(" D_UTC_TIME ") %s, (" D_DST_TIME ") %s, (" D_STD_TIME ") %s"), GetTime(0).c_str(), GetTime(2).c_str(), GetTime(3).c_str()); ntp_synced_message = true; - if (Rtc.local_time < 1451602800) { // 2016-01-01 + if (Rtc.local_time < START_VALID_TIME) { // 2016-01-01 rules_flag.time_init = 1; } else { rules_flag.time_set = 1; @@ -403,7 +403,7 @@ void RtcSecond(void) } Rtc.utc_time++; Rtc.local_time = Rtc.utc_time; - if (Rtc.local_time > 1451602800) { // 2016-01-01 + if (Rtc.local_time > START_VALID_TIME) { // 2016-01-01 int16_t timezone_minutes = Settings.timezone_minutes; if (Settings.timezone < 0) { timezone_minutes *= -1; } Rtc.time_timezone = (Settings.timezone * SECS_PER_HOUR) + (timezone_minutes * SECS_PER_MIN); @@ -447,7 +447,7 @@ void RtcSecond(void) void RtcSetTime(uint32_t epoch) { - if (epoch < 1451602800) { // 2016-01-01 + if (epoch < START_VALID_TIME) { // 2016-01-01 Rtc.user_time_entry = false; ntp_force_sync = true; } else { diff --git a/sonoff/xdrv_03_energy.ino b/sonoff/xdrv_03_energy.ino index 007aa871f..b9d8d92b4 100644 --- a/sonoff/xdrv_03_energy.ino +++ b/sonoff/xdrv_03_energy.ino @@ -123,6 +123,25 @@ Ticker ticker_energy; /********************************************************************************************/ +bool EnergyTariff1Active() // Off-Peak hours +{ + uint8_t tariff1 = Settings.register8[R8_ENERGY_TARIFF1_ST]; + uint8_t tariff2 = Settings.register8[R8_ENERGY_TARIFF2_ST]; + if (IsDst() && (Settings.register8[R8_ENERGY_TARIFF1_DS] != Settings.register8[R8_ENERGY_TARIFF2_DS])) { + tariff1 = Settings.register8[R8_ENERGY_TARIFF1_DS]; + tariff2 = Settings.register8[R8_ENERGY_TARIFF2_DS]; + } + if (tariff1 != tariff2) { + return ((RtcTime.hour < tariff2) || // Tarrif1 = Off-Peak + (RtcTime.hour >= tariff1) || + (Settings.flag3.energy_weekend && ((RtcTime.day_of_week == 1) || + (RtcTime.day_of_week == 7))) + ); + } else { + return false; + } +} + void EnergyUpdateToday(void) { if (Energy.kWhtoday_delta > 1000) { @@ -143,11 +162,7 @@ void EnergyUpdateToday(void) Energy.daily = (float)(RtcSettings.energy_kWhtoday) / 100000; Energy.total = (float)(RtcSettings.energy_kWhtotal + RtcSettings.energy_kWhtoday) / 100000; - if ((RtcTime.hour < Settings.param[P_ENERGY_TARIFF2]) || // Tarrif1 = Off-Peak - (RtcTime.hour >= Settings.param[P_ENERGY_TARIFF1]) || - (Settings.flag3.energy_weekend && ((RtcTime.day_of_week == 1) || - (RtcTime.day_of_week == 7))) - ) { + if (EnergyTariff1Active()) { // Tarrif1 = Off-Peak RtcSettings.energy_usage.usage1_kWhtoday += energy_diff; RtcSettings.energy_usage.return1_kWhtotal += return_diff; Energy.total1 = (float)(RtcSettings.energy_usage.usage1_kWhtotal + RtcSettings.energy_usage.usage1_kWhtoday) / 100000; @@ -414,8 +429,8 @@ void EnergyOverTempCheck() if (!isnan(Energy.reactive_power)) { Energy.reactive_power = 0; } if (!isnan(Energy.frequency)) { Energy.frequency = 0; } if (!isnan(Energy.power_factor)) { Energy.power_factor = 0; } - Energy.start_energy = 0; if (!isnan(Energy.export_active)) { Energy.export_active = 0; } + Energy.start_energy = 0; XnrgCall(FUNC_ENERGY_RESET); } @@ -501,19 +516,24 @@ void CmndEnergyReset(void) void CmndTariff(void) { - // Tariff1 23 - // Tariff2 7 - // Tariff3 0/1 - if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= 2)) { + // Tariff1 23 - Standard Time Tariff1 start hour + // Tariff2 7 - Standard Time Tariff2 start hour + // Tariff3 22 - Daylight Savings Time Tariff1 start hour + // Tariff4 6 - Daylight Savings Time Tariff2 start hour + // Tariff9 0/1 + if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= 4)) { if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 24)) { - Settings.param[P_ENERGY_TARIFF1 + XdrvMailbox.index -1] = XdrvMailbox.payload; + Settings.register8[R8_ENERGY_TARIFF1_ST + XdrvMailbox.index -1] = XdrvMailbox.payload; } } - else if (XdrvMailbox.index == 3) { + else if (XdrvMailbox.index == 9) { Settings.flag3.energy_weekend = XdrvMailbox.payload & 1; } - Response_P(PSTR("{\"%s\":{\"Off-Peak\":%d,\"Standard\":%d,\"Weekend\":\"%s\"}}"), - XdrvMailbox.command, Settings.param[P_ENERGY_TARIFF1], Settings.param[P_ENERGY_TARIFF2], GetStateText(Settings.flag3.energy_weekend)); + Response_P(PSTR("{\"%s\":{\"Off-Peak\":[%d,%d],\"Standard\":[%d,%d],\"Weekend\":\"%s\"}}"), + XdrvMailbox.command, + Settings.register8[R8_ENERGY_TARIFF1_ST], Settings.register8[R8_ENERGY_TARIFF1_DS], + Settings.register8[R8_ENERGY_TARIFF2_ST], Settings.register8[R8_ENERGY_TARIFF2_DS], + GetStateText(Settings.flag3.energy_weekend)); } void CmndPowerCal(void) diff --git a/sonoff/xsns_33_ds3231.ino b/sonoff/xsns_33_ds3231.ino index f1243a3a4..ef2acfbd4 100644 --- a/sonoff/xsns_33_ds3231.ino +++ b/sonoff/xsns_33_ds3231.ino @@ -142,13 +142,13 @@ bool Xsns33(uint8_t function) break; case FUNC_EVERY_SECOND: TIME_T tmpTime; - if (!ds3231ReadStatus && DS3231chipDetected && Rtc.utc_time < 1451602800 ) { // We still did not sync with NTP (time not valid) , so, read time from DS3231 + if (!ds3231ReadStatus && DS3231chipDetected && Rtc.utc_time < START_VALID_TIME ) { // We still did not sync with NTP (time not valid) , so, read time from DS3231 ntp_force_sync = true; //force to sync with ntp Rtc.utc_time = ReadFromDS3231(); //we read UTC TIME from DS3231 // from this line, we just copy the function from "void RtcSecond()" at the support.ino ,line 2143 and above // We need it to set rules etc. BreakTime(Rtc.utc_time, tmpTime); - if (Rtc.utc_time < 1451602800 ) { + if (Rtc.utc_time < START_VALID_TIME ) { ds3231ReadStatus = true; //if time in DS3231 is valid, do not update again } RtcTime.year = tmpTime.year + 1970; @@ -156,13 +156,13 @@ bool Xsns33(uint8_t function) Rtc.standard_time = RuleToTime(Settings.tflag[0], RtcTime.year); AddLog_P2(LOG_LEVEL_INFO, PSTR("Set time from DS3231 to RTC (" D_UTC_TIME ") %s, (" D_DST_TIME ") %s, (" D_STD_TIME ") %s"), GetTime(0).c_str(), GetTime(2).c_str(), GetTime(3).c_str()); - if (Rtc.local_time < 1451602800) { // 2016-01-01 + if (Rtc.local_time < START_VALID_TIME) { // 2016-01-01 rules_flag.time_init = 1; } else { rules_flag.time_set = 1; } } - else if (!ds3231WriteStatus && DS3231chipDetected && Rtc.utc_time > 1451602800 && abs(Rtc.utc_time - ReadFromDS3231()) > 60) {//if time is valid and is drift from RTC in more that 60 second + else if (!ds3231WriteStatus && DS3231chipDetected && Rtc.utc_time > START_VALID_TIME && abs(Rtc.utc_time - ReadFromDS3231()) > 60) {//if time is valid and is drift from RTC in more that 60 second AddLog_P2(LOG_LEVEL_INFO, PSTR("Write Time TO DS3231 from NTP (" D_UTC_TIME ") %s, (" D_DST_TIME ") %s, (" D_STD_TIME ") %s"), GetTime(0).c_str(), GetTime(2).c_str(), GetTime(3).c_str()); SetDS3231Time (Rtc.utc_time); //update the DS3231 time