Skip to content

Commit

Permalink
Merge pull request #5 from arendst/development
Browse files Browse the repository at this point in the history
update fork
  • Loading branch information
ascillato authored Mar 17, 2018
2 parents 0964a1a + 3e7142f commit ed2b3b6
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 39 deletions.
2 changes: 2 additions & 0 deletions sonoff/_releasenotes.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Add additional time offset to Wifi Retry based on device mac address (#2089)
* Add command Color6 RRGGBB for Clock hour marker color and command Rotation pixels for Clock rotation (#2092)
* Add HTML language header in local language (#2123)
* Add command PowerDelta 0..100 (percentage) to Energy monitoring devices to report on active power load change (#2157)
* Add Restart Reason to Status 1 report (#2161)
* Fix MAX31850 higher temperatures (#1269)
*
* 5.12.0d
Expand Down
3 changes: 3 additions & 0 deletions sonoff/i18n.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@
// Commands xdrv_03_energy.ino
#define D_CMND_POWERLOW "PowerLow"
#define D_CMND_POWERHIGH "PowerHigh"
#define D_CMND_POWERDELTA "PowerDelta"
#define D_CMND_VOLTAGELOW "VoltageLow"
#define D_CMND_VOLTAGEHIGH "VoltageHigh"
#define D_CMND_CURRENTLOW "CurrentLow"
Expand Down Expand Up @@ -359,6 +360,7 @@ enum UnitNames {
UNIT_MILLISECOND,
UNIT_MINUTE,
UNIT_PPM,
UNIT_PERCENTAGE,
UNIT_PRESSURE,
UNIT_SECOND,
UNIT_SECTORS,
Expand All @@ -376,6 +378,7 @@ const char kUnitNames[] PROGMEM =
D_UNIT_MILLISECOND "|"
D_UNIT_MINUTE "|"
D_UNIT_PARTS_PER_MILLION "|"
"%|"
D_UNIT_PRESSURE "|"
D_UNIT_SECOND "|"
D_UNIT_SECTORS "|"
Expand Down
8 changes: 4 additions & 4 deletions sonoff/language/hu-HU.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
#define D_BOOT_COUNT "Bootolások száma"
#define D_BRIGHTLIGHT "Max. fényerő"
#define D_BUTTON "Gomb"
#define D_BY "által" // Written by me
#define D_BY "tőle:" // Written by me
#define D_BYTES "Byte-ok"
#define D_CELSIUS "Celsius"
#define D_CO2 "Szén-dioxid"
Expand All @@ -71,7 +71,7 @@
#define D_COUNTER "Számláló"
#define D_CURRENT "Áram" // As in Voltage and Current
#define D_DATA "Adat"
#define D_DARKLIGHT "Min. févnyerő"
#define D_DARKLIGHT "Min. fényerő"
#define D_DEBUG "Debug"
#define D_DISABLED "Letiltva"
#define D_DNS_SERVER "DNS Szerver"
Expand Down Expand Up @@ -210,7 +210,7 @@
#define D_INFORMATION "Információ"
#define D_FIRMWARE_UPGRADE "Firmware Frissítés"
#define D_CONSOLE "Konzol"
#define D_CONFIRM_RESTART "Újraindítés megerősítése"
#define D_CONFIRM_RESTART "Újraindítás megerősítése"

#define D_CONFIGURE_MODULE "Eszköz konfiguráció"
#define D_CONFIGURE_WIFI "WiFi konfiguráció"
Expand Down Expand Up @@ -459,4 +459,4 @@
#define D_LOG_UPNP "UPP: " // UPnP
#define D_LOG_WIFI "WIF: " // Wifi

#endif // _LANGUAGE_EN_GB_H_
#endif // _LANGUAGE_HU_HU_H_
4 changes: 1 addition & 3 deletions sonoff/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ struct SYSCFG {
uint8_t ledstate; // 2FB
uint8_t param[PARAM8_SIZE]; // 2FC was domoticz_in_topic until 5.1.6
char state_text[4][11]; // 313

byte free_33F[1]; // 33F

uint8_t energy_power_delta; // 33F
uint16_t domoticz_update_timer; // 340
uint16_t pwm_range; // 342

Expand Down
2 changes: 2 additions & 0 deletions sonoff/settings.ino
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ void SettingsDefaultSet2()
// Settings.domoticz_switch_idx[i] = 0;
}

Settings.energy_power_delta = 80;
Settings.energy_power_calibration = HLW_PREF_PULSE;
Settings.energy_voltage_calibration = HLW_UREF_PULSE;
Settings.energy_current_calibration = HLW_IREF_PULSE;
Expand Down Expand Up @@ -889,6 +890,7 @@ void SettingsDelta()
}
if (Settings.version < 0x050C0005) {
Settings.light_rotation = 0;
Settings.energy_power_delta = 80;
char fingerprint[60];
memcpy(fingerprint, Settings.mqtt_fingerprint, sizeof(fingerprint));
char *p = fingerprint;
Expand Down
11 changes: 6 additions & 5 deletions sonoff/sonoff.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ typedef unsigned long power_t; // Power (Relay) type
//#define PWM_FREQ 910 // 100..1000 Hz led refresh (iTead value)
#define PWM_FREQ 880 // 100..1000 Hz led refresh (BN-SZ01 value)

#define MAX_POWER_HOLD 10 // Time in SECONDS to allow max agreed power (Pow)
#define MAX_POWER_WINDOW 30 // Time in SECONDS to disable allow max agreed power (Pow)
#define SAFE_POWER_HOLD 10 // Time in SECONDS to allow max unit safe power (Pow)
#define SAFE_POWER_WINDOW 30 // Time in MINUTES to disable allow max unit safe power (Pow)
#define MAX_POWER_RETRY 5 // Retry count allowing agreed power limit overflow (Pow)
#define DEFAULT_POWER_DELTA 80 // Power change percentage
#define MAX_POWER_HOLD 10 // Time in SECONDS to allow max agreed power
#define MAX_POWER_WINDOW 30 // Time in SECONDS to disable allow max agreed power
#define SAFE_POWER_HOLD 10 // Time in SECONDS to allow max unit safe power
#define SAFE_POWER_WINDOW 30 // Time in MINUTES to disable allow max unit safe power
#define MAX_POWER_RETRY 5 // Retry count allowing agreed power limit overflow

#define STATES 20 // State loops per second
#define SYSLOG_TIMER 600 // Seconds to restore syslog_level
Expand Down
8 changes: 4 additions & 4 deletions sonoff/sonoff.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1218,8 +1218,8 @@ void PublishStatus(uint8_t payload)
}

if ((0 == payload) || (1 == payload)) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS D_STATUS1_PARAMETER "\":{\"" D_JSON_BAUDRATE "\":%d,\"" D_CMND_GROUPTOPIC "\":\"%s\",\"" D_CMND_OTAURL "\":\"%s\",\"" D_JSON_UPTIME "\":\"%s\",\"" D_JSON_STARTUPUTC "\":\"%s\",\"" D_CMND_SLEEP "\":%d,\"" D_JSON_BOOTCOUNT "\":%d,\"" D_JSON_SAVECOUNT "\":%d,\"" D_JSON_SAVEADDRESS "\":\"%X\"}}"),
baudrate, Settings.mqtt_grptopic, Settings.ota_url, GetDateAndTime(DT_UPTIME).c_str(), GetDateAndTime(DT_RESTART).c_str(), Settings.sleep, Settings.bootcount, Settings.save_flag, GetSettingsAddress());
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS D_STATUS1_PARAMETER "\":{\"" D_JSON_BAUDRATE "\":%d,\"" D_CMND_GROUPTOPIC "\":\"%s\",\"" D_CMND_OTAURL "\":\"%s\",\"" D_JSON_RESTARTREASON "\":\"%s\",\"" D_JSON_UPTIME "\":\"%s\",\"" D_JSON_STARTUPUTC "\":\"%s\",\"" D_CMND_SLEEP "\":%d,\"" D_JSON_BOOTCOUNT "\":%d,\"" D_JSON_SAVECOUNT "\":%d,\"" D_JSON_SAVEADDRESS "\":\"%X\"}}"),
baudrate, Settings.mqtt_grptopic, Settings.ota_url, GetResetReason().c_str(), GetDateAndTime(DT_UPTIME).c_str(), GetDateAndTime(DT_RESTART).c_str(), Settings.sleep, Settings.bootcount, Settings.save_flag, GetSettingsAddress());
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "1"));
}

Expand Down Expand Up @@ -1262,8 +1262,8 @@ void PublishStatus(uint8_t payload)

if (energy_flg) {
if ((0 == payload) || (9 == payload)) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS D_STATUS9_MARGIN "\":{\"" D_CMND_POWERLOW "\":%d,\"" D_CMND_POWERHIGH "\":%d,\"" D_CMND_VOLTAGELOW "\":%d,\"" D_CMND_VOLTAGEHIGH "\":%d,\"" D_CMND_CURRENTLOW "\":%d,\"" D_CMND_CURRENTHIGH "\":%d}}"),
Settings.energy_min_power, Settings.energy_max_power, Settings.energy_min_voltage, Settings.energy_max_voltage, Settings.energy_min_current, Settings.energy_max_current);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS D_STATUS9_MARGIN "\":{\"" D_CMND_POWERDELTA "\":%d,\"" D_CMND_POWERLOW "\":%d,\"" D_CMND_POWERHIGH "\":%d,\"" D_CMND_VOLTAGELOW "\":%d,\"" D_CMND_VOLTAGEHIGH "\":%d,\"" D_CMND_CURRENTLOW "\":%d,\"" D_CMND_CURRENTHIGH "\":%d}}"),
Settings.energy_power_delta, Settings.energy_min_power, Settings.energy_max_power, Settings.energy_min_voltage, Settings.energy_max_voltage, Settings.energy_min_current, Settings.energy_max_current);
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "9"));
}
}
Expand Down
45 changes: 40 additions & 5 deletions sonoff/xdrv_03_energy.ino
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
enum EnergyHardware { ENERGY_NONE, ENERGY_HLW8012, ENERGY_CSE7766, ENERGY_PZEM004T };

enum EnergyCommands {
CMND_POWERDELTA,
CMND_POWERLOW, CMND_POWERHIGH, CMND_VOLTAGELOW, CMND_VOLTAGEHIGH, CMND_CURRENTLOW, CMND_CURRENTHIGH,
CMND_POWERCAL, CMND_POWERSET, CMND_VOLTAGECAL, CMND_VOLTAGESET, CMND_CURRENTCAL, CMND_CURRENTSET,
CMND_ENERGYRESET, CMND_MAXENERGY, CMND_MAXENERGYSTART,
CMND_MAXPOWER, CMND_MAXPOWERHOLD, CMND_MAXPOWERWINDOW,
CMND_SAFEPOWER, CMND_SAFEPOWERHOLD, CMND_SAFEPOWERWINDOW };
const char kEnergyCommands[] PROGMEM =
D_CMND_POWERDELTA "|"
D_CMND_POWERLOW "|" D_CMND_POWERHIGH "|" D_CMND_VOLTAGELOW "|" D_CMND_VOLTAGEHIGH "|" D_CMND_CURRENTLOW "|" D_CMND_CURRENTHIGH "|"
D_CMND_POWERCAL "|" D_CMND_POWERSET "|" D_CMND_VOLTAGECAL "|" D_CMND_VOLTAGESET "|" D_CMND_CURRENTCAL "|" D_CMND_CURRENTSET "|"
D_CMND_ENERGYRESET "|" D_CMND_MAXENERGY "|" D_CMND_MAXENERGYSTART "|"
Expand All @@ -51,6 +53,9 @@ float energy_start = 0; // 12345.12345 kWh total previous
unsigned long energy_kWhtoday; // 1212312345 Wh * 10^-5 (deca micro Watt hours) - 5763924 = 0.05763924 kWh = 0.058 kWh = energy_daily
unsigned long energy_period = 0; // 1212312345 Wh * 10^-5 (deca micro Watt hours) - 5763924 = 0.05763924 kWh = 0.058 kWh = energy_daily

float energy_power_last[3] = { 0 };
uint8_t energy_power_delta = 0;

bool energy_power_on = true;

byte energy_min_power_flag = 0;
Expand Down Expand Up @@ -264,6 +269,7 @@ uint8_t cse_receive_flag = 0;
long voltage_cycle = 0;
long current_cycle = 0;
long power_cycle = 0;
unsigned long power_cycle_first = 0;
long cf_pulses = 0;
long cf_pulses_last_time = CSE_PULSES_NOT_INITIALIZED;

Expand Down Expand Up @@ -314,9 +320,6 @@ void CseReceived()
power_cycle = serial_in_buffer[17] << 16 | serial_in_buffer[18] << 8 | serial_in_buffer[19];
cf_pulses = serial_in_buffer[21] << 8 | serial_in_buffer[22];

// if (adjustement & 0x80) { // CF overflow
// cf_pulses += 0x10000;
// }
if (energy_power_on) { // Powered on
if (adjustement & 0x40) { // Voltage valid
energy_voltage = (float)(Settings.energy_voltage_calibration * CSE_UREF) / (float)voltage_cycle;
Expand All @@ -325,9 +328,16 @@ void CseReceived()
if ((header & 0xF2) == 0xF2) { // Power cycle exceeds range
energy_power = 0;
} else {
energy_power = (float)(Settings.energy_power_calibration * CSE_PREF) / (float)power_cycle;
if (0 == power_cycle_first) power_cycle_first = power_cycle; // Skip first incomplete power_cycle
if (power_cycle_first != power_cycle) {
power_cycle_first = -1;
energy_power = (float)(Settings.energy_power_calibration * CSE_PREF) / (float)power_cycle;
} else {
energy_power = 0;
}
}
} else {
power_cycle_first = 0;
energy_power = 0; // Powered on but no load
}
if (adjustement & 0x20) { // Current valid
Expand All @@ -338,6 +348,7 @@ void CseReceived()
}
}
} else { // Powered off
power_cycle_first = 0;
energy_voltage = 0;
energy_power = 0;
energy_current = 0;
Expand Down Expand Up @@ -638,6 +649,20 @@ void EnergyMarginCheck()
return;
}

if (Settings.energy_power_delta) {
float delta = abs(energy_power_last[0] - energy_power);
// Any delta compared to minimal delta
float min_power = (energy_power_last[0] > energy_power) ? energy_power : energy_power_last[0];
if (((delta / min_power) * 100) > Settings.energy_power_delta) {
energy_power_delta = 1;
energy_power_last[1] = energy_power; // We only want one report so reset history
energy_power_last[2] = energy_power;
}
}
energy_power_last[0] = energy_power_last[1]; // Shift in history every second allowing power changes to settle for up to three seconds
energy_power_last[1] = energy_power_last[2];
energy_power_last[2] = energy_power;

if (energy_power_on && (Settings.energy_min_power || Settings.energy_max_power || Settings.energy_min_voltage || Settings.energy_max_voltage || Settings.energy_min_current || Settings.energy_max_current)) {
energy_power_u = (uint16_t)(energy_power);
energy_voltage_u = (uint16_t)(energy_voltage);
Expand Down Expand Up @@ -743,6 +768,8 @@ void EnergyMarginCheck()
}
}
#endif // FEATURE_POWER_LIMIT

if (energy_power_delta) EnergyMqttShow();
}

void EnergyMqttShow()
Expand All @@ -752,6 +779,7 @@ void EnergyMqttShow()
EnergyShow(1);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}"), mqtt_data);
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_ENERGY), Settings.flag.mqtt_sensor_retain);
energy_power_delta = 0;
}

/*********************************************************************************************\
Expand All @@ -768,7 +796,14 @@ boolean EnergyCommand()
unsigned long nvalue = 0;

int command_code = GetCommandCode(command, sizeof(command), XdrvMailbox.topic, kEnergyCommands);
if (CMND_POWERLOW == command_code) {
if (CMND_POWERDELTA == command_code) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 101)) {
Settings.energy_power_delta = (1 == XdrvMailbox.payload) ? DEFAULT_POWER_DELTA : XdrvMailbox.payload;
}
nvalue = Settings.energy_power_delta;
unit = UNIT_PERCENTAGE;
}
else if (CMND_POWERLOW == command_code) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 3601)) {
Settings.energy_min_power = XdrvMailbox.payload;
}
Expand Down
19 changes: 1 addition & 18 deletions sonoff/xdrv_07_home_assistant.ino
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,7 @@ const char HASS_DISCOVER_LIGHT_SCHEME[] PROGMEM =
"%s,\"effect_command_topic\":\"%s\"," // cmnd/led2/Scheme
"\"effect_state_topic\":\"%s\"," // stat/led2/RESULT
"\"effect_value_template\":\"{{value_json." D_CMND_SCHEME "}}\","
"\"effect_list\":\"[0, 1, 2, 3, 4]\""; // Needs to be a Python string list providing Scheme parameter values (Unable to get this functional)
*/
/*
#1690 - investigate
effect_list:
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
"\"effect_list\":[\"0\",\"1\",\"2\",\"3\",\"4\"]"; // string list with reference to scheme parameter. Currently only supports numbers 0 to 11 as it make the mqtt string too long
*/
void HAssDiscovery()
{
Expand Down

0 comments on commit ed2b3b6

Please sign in to comment.