2017-12-16 14:51:45 +00:00
|
|
|
/*
|
2018-09-04 15:22:34 +01:00
|
|
|
xdrv_03_energy.ino - Energy sensor support for Sonoff-Tasmota
|
2017-12-16 14:51:45 +00:00
|
|
|
|
2019-01-01 12:55:01 +00:00
|
|
|
Copyright (C) 2019 Theo Arends
|
2017-12-16 14:51:45 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_ENERGY_SENSOR
|
|
|
|
/*********************************************************************************************\
|
2018-09-04 15:22:34 +01:00
|
|
|
* Energy
|
2017-12-16 14:51:45 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-06 16:33:51 +00:00
|
|
|
#define XDRV_03 3
|
|
|
|
#define XSNS_03 3
|
|
|
|
|
|
|
|
#define ENERGY_NONE 0
|
2017-12-16 14:51:45 +00:00
|
|
|
|
2019-05-14 16:46:40 +01:00
|
|
|
#define ENERGY_OVERTEMP 73.0 // Industry standard lowest overtemp in Celsius
|
|
|
|
|
2018-09-04 15:22:34 +01:00
|
|
|
#define FEATURE_POWER_LIMIT true
|
2017-12-16 14:51:45 +00:00
|
|
|
|
2018-11-20 14:00:24 +00:00
|
|
|
#include <Ticker.h>
|
|
|
|
|
2019-01-05 14:39:56 +00:00
|
|
|
#define D_CMND_POWERCAL "PowerCal"
|
|
|
|
#define D_CMND_VOLTAGECAL "VoltageCal"
|
|
|
|
#define D_CMND_CURRENTCAL "CurrentCal"
|
|
|
|
|
2017-12-16 14:51:45 +00:00
|
|
|
enum EnergyCommands {
|
2018-03-14 13:32:51 +00:00
|
|
|
CMND_POWERDELTA,
|
2017-12-16 14:51:45 +00:00
|
|
|
CMND_POWERLOW, CMND_POWERHIGH, CMND_VOLTAGELOW, CMND_VOLTAGEHIGH, CMND_CURRENTLOW, CMND_CURRENTHIGH,
|
2019-01-05 14:39:56 +00:00
|
|
|
CMND_POWERCAL, CMND_VOLTAGECAL, CMND_CURRENTCAL,
|
2018-09-22 14:09:53 +01:00
|
|
|
CMND_POWERSET, CMND_VOLTAGESET, CMND_CURRENTSET, CMND_FREQUENCYSET,
|
2017-12-16 14:51:45 +00:00
|
|
|
CMND_ENERGYRESET, CMND_MAXENERGY, CMND_MAXENERGYSTART,
|
|
|
|
CMND_MAXPOWER, CMND_MAXPOWERHOLD, CMND_MAXPOWERWINDOW,
|
|
|
|
CMND_SAFEPOWER, CMND_SAFEPOWERHOLD, CMND_SAFEPOWERWINDOW };
|
|
|
|
const char kEnergyCommands[] PROGMEM =
|
2018-03-14 13:32:51 +00:00
|
|
|
D_CMND_POWERDELTA "|"
|
2017-12-16 14:51:45 +00:00
|
|
|
D_CMND_POWERLOW "|" D_CMND_POWERHIGH "|" D_CMND_VOLTAGELOW "|" D_CMND_VOLTAGEHIGH "|" D_CMND_CURRENTLOW "|" D_CMND_CURRENTHIGH "|"
|
2019-01-05 14:39:56 +00:00
|
|
|
D_CMND_POWERCAL "|" D_CMND_VOLTAGECAL "|" D_CMND_CURRENTCAL "|"
|
2018-09-22 14:09:53 +01:00
|
|
|
D_CMND_POWERSET "|" D_CMND_VOLTAGESET "|" D_CMND_CURRENTSET "|" D_CMND_FREQUENCYSET "|"
|
2017-12-16 14:51:45 +00:00
|
|
|
D_CMND_ENERGYRESET "|" D_CMND_MAXENERGY "|" D_CMND_MAXENERGYSTART "|"
|
|
|
|
D_CMND_MAXPOWER "|" D_CMND_MAXPOWERHOLD "|" D_CMND_MAXPOWERWINDOW "|"
|
|
|
|
D_CMND_SAFEPOWER "|" D_CMND_SAFEPOWERHOLD "|" D_CMND_SAFEPOWERWINDOW ;
|
|
|
|
|
2018-09-28 14:48:42 +01:00
|
|
|
float energy_voltage = 0; // 123.1 V
|
|
|
|
float energy_current = 0; // 123.123 A
|
|
|
|
float energy_active_power = 0; // 123.1 W
|
|
|
|
float energy_apparent_power = NAN; // 123.1 VA
|
|
|
|
float energy_reactive_power = NAN; // 123.1 VAr
|
|
|
|
float energy_power_factor = NAN; // 0.12
|
|
|
|
float energy_frequency = NAN; // 123.1 Hz
|
|
|
|
float energy_start = 0; // 12345.12345 kWh total previous
|
|
|
|
|
|
|
|
float energy_daily = 0; // 123.123 kWh
|
|
|
|
float energy_total = 0; // 12345.12345 kWh
|
2018-04-30 11:07:48 +01:00
|
|
|
unsigned long energy_kWhtoday_delta = 0; // 1212312345 Wh 10^-5 (deca micro Watt hours) - Overflows to energy_kWhtoday (HLW and CSE only)
|
2018-09-28 14:48:42 +01:00
|
|
|
unsigned long energy_kWhtoday; // 12312312 Wh * 10^-2 (deca milli Watt hours) - 5764 = 0.05764 kWh = 0.058 kWh = energy_daily
|
|
|
|
unsigned long energy_period = 0; // 12312312 Wh * 10^-2 (deca milli Watt hours) - 5764 = 0.05764 kWh = 0.058 kWh = energy_daily
|
2017-12-16 14:51:45 +00:00
|
|
|
|
2018-03-15 16:33:35 +00:00
|
|
|
float energy_power_last[3] = { 0 };
|
|
|
|
uint8_t energy_power_delta = 0;
|
|
|
|
|
Add support for Shelly 1PM Template
Add support for Shelly 1PM Template {"NAME":"Shelly 1PM","GPIO":[56,0,0,0,82,134,0,0,0,0,0,21,0],"FLAG":2,"BASE":18} (#5716)
2019-05-13 17:26:07 +01:00
|
|
|
bool energy_voltage_available = true; // Enable if voltage is measured
|
|
|
|
bool energy_current_available = true; // Enable if current is measured
|
|
|
|
|
2018-09-28 14:48:42 +01:00
|
|
|
bool energy_type_dc = false;
|
2018-02-04 17:09:09 +00:00
|
|
|
bool energy_power_on = true;
|
2019-01-28 13:08:33 +00:00
|
|
|
bool energy_min_power_flag = false;
|
|
|
|
bool energy_max_power_flag = false;
|
|
|
|
bool energy_min_voltage_flag = false;
|
|
|
|
bool energy_max_voltage_flag = false;
|
|
|
|
bool energy_min_current_flag = false;
|
|
|
|
bool energy_max_current_flag = false;
|
2018-02-04 17:09:09 +00:00
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
uint8_t energy_power_steady_cntr = 8; // Allow for power on stabilization
|
|
|
|
uint8_t energy_max_energy_state = 0;
|
2017-12-16 14:51:45 +00:00
|
|
|
|
|
|
|
#if FEATURE_POWER_LIMIT
|
2019-01-28 13:08:33 +00:00
|
|
|
uint8_t energy_mplr_counter = 0;
|
2017-12-16 14:51:45 +00:00
|
|
|
uint16_t energy_mplh_counter = 0;
|
|
|
|
uint16_t energy_mplw_counter = 0;
|
|
|
|
#endif // FEATURE_POWER_LIMIT
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
uint8_t energy_fifth_second = 0;
|
2017-12-16 14:51:45 +00:00
|
|
|
Ticker ticker_energy;
|
|
|
|
|
2018-09-04 15:22:34 +01:00
|
|
|
int energy_command_code = 0;
|
2018-02-04 17:09:09 +00:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void EnergyUpdateToday(void)
|
2018-02-04 17:09:09 +00:00
|
|
|
{
|
2018-04-30 11:07:48 +01:00
|
|
|
if (energy_kWhtoday_delta > 1000) {
|
|
|
|
unsigned long delta = energy_kWhtoday_delta / 1000;
|
|
|
|
energy_kWhtoday_delta -= (delta * 1000);
|
|
|
|
energy_kWhtoday += delta;
|
|
|
|
}
|
2018-02-04 17:09:09 +00:00
|
|
|
RtcSettings.energy_kWhtoday = energy_kWhtoday;
|
2018-04-30 11:07:48 +01:00
|
|
|
energy_daily = (float)energy_kWhtoday / 100000;
|
|
|
|
energy_total = (float)(RtcSettings.energy_kWhtotal + energy_kWhtoday) / 100000;
|
2018-02-04 17:09:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-10 16:21:26 +01:00
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void Energy200ms(void)
|
2017-12-16 14:51:45 +00:00
|
|
|
{
|
2018-09-17 19:32:38 +01:00
|
|
|
energy_power_on = (power != 0) | Settings.flag.no_power_on_check;
|
|
|
|
|
2017-12-16 14:51:45 +00:00
|
|
|
energy_fifth_second++;
|
|
|
|
if (5 == energy_fifth_second) {
|
|
|
|
energy_fifth_second = 0;
|
|
|
|
|
2019-04-15 17:12:42 +01:00
|
|
|
XnrgCall(FUNC_ENERGY_EVERY_SECOND);
|
2018-02-03 22:25:05 +00:00
|
|
|
|
2017-12-16 14:51:45 +00:00
|
|
|
if (RtcTime.valid) {
|
|
|
|
if (LocalTime() == Midnight()) {
|
|
|
|
Settings.energy_kWhyesterday = energy_kWhtoday;
|
2018-04-30 11:07:48 +01:00
|
|
|
Settings.energy_kWhtotal += energy_kWhtoday;
|
2017-12-16 14:51:45 +00:00
|
|
|
RtcSettings.energy_kWhtotal = Settings.energy_kWhtotal;
|
|
|
|
energy_kWhtoday = 0;
|
2018-04-30 11:07:48 +01:00
|
|
|
energy_kWhtoday_delta = 0;
|
2018-01-24 16:31:20 +00:00
|
|
|
energy_period = energy_kWhtoday;
|
2018-02-06 09:06:22 +00:00
|
|
|
EnergyUpdateToday();
|
2017-12-16 14:51:45 +00:00
|
|
|
energy_max_energy_state = 3;
|
|
|
|
}
|
|
|
|
if ((RtcTime.hour == Settings.energy_max_energy_start) && (3 == energy_max_energy_state)) {
|
|
|
|
energy_max_energy_state = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 15:22:34 +01:00
|
|
|
XnrgCall(FUNC_EVERY_200_MSECOND);
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void EnergySaveState(void)
|
2017-12-16 14:51:45 +00:00
|
|
|
{
|
|
|
|
Settings.energy_kWhdoy = (RtcTime.valid) ? RtcTime.day_of_year : 0;
|
|
|
|
Settings.energy_kWhtoday = energy_kWhtoday;
|
2018-02-04 17:09:09 +00:00
|
|
|
RtcSettings.energy_kWhtoday = energy_kWhtoday;
|
2017-12-16 14:51:45 +00:00
|
|
|
Settings.energy_kWhtotal = RtcSettings.energy_kWhtotal;
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool EnergyMargin(bool type, uint16_t margin, uint16_t value, bool &flag, bool &save_flag)
|
2017-12-16 14:51:45 +00:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool change;
|
2017-12-16 14:51:45 +00:00
|
|
|
|
2018-02-06 09:06:22 +00:00
|
|
|
if (!margin) return false;
|
2017-12-16 14:51:45 +00:00
|
|
|
change = save_flag;
|
|
|
|
if (type) {
|
|
|
|
flag = (value > margin);
|
|
|
|
} else {
|
|
|
|
flag = (value < margin);
|
|
|
|
}
|
|
|
|
save_flag = flag;
|
|
|
|
return (change != save_flag);
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void EnergySetPowerSteadyCounter(void)
|
2017-12-16 14:51:45 +00:00
|
|
|
{
|
|
|
|
energy_power_steady_cntr = 2;
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void EnergyMarginCheck(void)
|
2017-12-16 14:51:45 +00:00
|
|
|
{
|
2018-02-03 22:25:05 +00:00
|
|
|
uint16_t energy_daily_u = 0;
|
|
|
|
uint16_t energy_power_u = 0;
|
|
|
|
uint16_t energy_voltage_u = 0;
|
|
|
|
uint16_t energy_current_u = 0;
|
2019-01-28 13:08:33 +00:00
|
|
|
bool flag;
|
|
|
|
bool jsonflg;
|
2017-12-16 14:51:45 +00:00
|
|
|
|
|
|
|
if (energy_power_steady_cntr) {
|
|
|
|
energy_power_steady_cntr--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-15 16:33:35 +00:00
|
|
|
if (Settings.energy_power_delta) {
|
2018-09-28 14:48:42 +01:00
|
|
|
float delta = abs(energy_power_last[0] - energy_active_power);
|
2018-03-15 16:33:35 +00:00
|
|
|
// Any delta compared to minimal delta
|
2018-09-28 14:48:42 +01:00
|
|
|
float min_power = (energy_power_last[0] > energy_active_power) ? energy_active_power : energy_power_last[0];
|
2018-03-15 16:33:35 +00:00
|
|
|
if (((delta / min_power) * 100) > Settings.energy_power_delta) {
|
|
|
|
energy_power_delta = 1;
|
2018-09-28 14:48:42 +01:00
|
|
|
energy_power_last[1] = energy_active_power; // We only want one report so reset history
|
|
|
|
energy_power_last[2] = energy_active_power;
|
2018-03-15 16:33:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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];
|
2018-09-28 14:48:42 +01:00
|
|
|
energy_power_last[2] = energy_active_power;
|
2018-03-15 16:33:35 +00:00
|
|
|
|
2018-02-04 17:09:09 +00:00
|
|
|
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)) {
|
2018-09-28 14:48:42 +01:00
|
|
|
energy_power_u = (uint16_t)(energy_active_power);
|
2017-12-16 14:51:45 +00:00
|
|
|
energy_voltage_u = (uint16_t)(energy_voltage);
|
|
|
|
energy_current_u = (uint16_t)(energy_current * 1000);
|
|
|
|
|
2019-03-08 14:15:42 +00:00
|
|
|
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("NRG: W %d, U %d, I %d"), energy_power_u, energy_voltage_u, energy_current_u);
|
2017-12-16 14:51:45 +00:00
|
|
|
|
2019-03-23 16:00:59 +00:00
|
|
|
Response_P(PSTR("{"));
|
2019-01-28 13:08:33 +00:00
|
|
|
jsonflg = false;
|
|
|
|
if (EnergyMargin(false, Settings.energy_min_power, energy_power_u, flag, energy_min_power_flag)) {
|
2019-03-23 16:00:59 +00:00
|
|
|
ResponseAppend_P(PSTR("%s\"" D_CMND_POWERLOW "\":\"%s\""), (jsonflg)?",":"", GetStateText(flag));
|
2019-01-28 13:08:33 +00:00
|
|
|
jsonflg = true;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
2019-01-28 13:08:33 +00:00
|
|
|
if (EnergyMargin(true, Settings.energy_max_power, energy_power_u, flag, energy_max_power_flag)) {
|
2019-03-23 16:00:59 +00:00
|
|
|
ResponseAppend_P(PSTR("%s\"" D_CMND_POWERHIGH "\":\"%s\""), (jsonflg)?",":"", GetStateText(flag));
|
2019-01-28 13:08:33 +00:00
|
|
|
jsonflg = true;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
2019-01-28 13:08:33 +00:00
|
|
|
if (EnergyMargin(false, Settings.energy_min_voltage, energy_voltage_u, flag, energy_min_voltage_flag)) {
|
2019-03-23 16:00:59 +00:00
|
|
|
ResponseAppend_P(PSTR("%s\"" D_CMND_VOLTAGELOW "\":\"%s\""), (jsonflg)?",":"", GetStateText(flag));
|
2019-01-28 13:08:33 +00:00
|
|
|
jsonflg = true;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
2019-01-28 13:08:33 +00:00
|
|
|
if (EnergyMargin(true, Settings.energy_max_voltage, energy_voltage_u, flag, energy_max_voltage_flag)) {
|
2019-03-23 16:00:59 +00:00
|
|
|
ResponseAppend_P(PSTR("%s\"" D_CMND_VOLTAGEHIGH "\":\"%s\""), (jsonflg)?",":"", GetStateText(flag));
|
2019-01-28 13:08:33 +00:00
|
|
|
jsonflg = true;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
2019-01-28 13:08:33 +00:00
|
|
|
if (EnergyMargin(false, Settings.energy_min_current, energy_current_u, flag, energy_min_current_flag)) {
|
2019-03-23 16:00:59 +00:00
|
|
|
ResponseAppend_P(PSTR("%s%s\"" D_CMND_CURRENTLOW "\":\"%s\""), (jsonflg)?",":"", GetStateText(flag));
|
2019-01-28 13:08:33 +00:00
|
|
|
jsonflg = true;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
2019-01-28 13:08:33 +00:00
|
|
|
if (EnergyMargin(true, Settings.energy_max_current, energy_current_u, flag, energy_max_current_flag)) {
|
2019-03-23 16:00:59 +00:00
|
|
|
ResponseAppend_P(PSTR("%s%s\"" D_CMND_CURRENTHIGH "\":\"%s\""), (jsonflg)?",":"", GetStateText(flag));
|
2019-01-28 13:08:33 +00:00
|
|
|
jsonflg = true;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
if (jsonflg) {
|
Add support for Shelly 1PM Template
Add support for Shelly 1PM Template {"NAME":"Shelly 1PM","GPIO":[56,0,0,0,82,134,0,0,0,0,0,21,0],"FLAG":2,"BASE":18} (#5716)
2019-05-13 17:26:07 +01:00
|
|
|
ResponseJsonEnd();
|
2018-08-27 22:35:23 +01:00
|
|
|
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_MARGINS), MQTT_TELE_RETAIN);
|
2017-12-16 14:51:45 +00:00
|
|
|
EnergyMqttShow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if FEATURE_POWER_LIMIT
|
|
|
|
// Max Power
|
|
|
|
if (Settings.energy_max_power_limit) {
|
2018-09-28 14:48:42 +01:00
|
|
|
if (energy_active_power > Settings.energy_max_power_limit) {
|
2017-12-16 14:51:45 +00:00
|
|
|
if (!energy_mplh_counter) {
|
|
|
|
energy_mplh_counter = Settings.energy_max_power_limit_hold;
|
|
|
|
} else {
|
|
|
|
energy_mplh_counter--;
|
|
|
|
if (!energy_mplh_counter) {
|
2019-03-23 16:00:59 +00:00
|
|
|
Response_P(PSTR("{\"" D_JSON_MAXPOWERREACHED "\":\"%d%s\"}"), energy_power_u, (Settings.flag.value_units) ? " " D_UNIT_WATT : "");
|
2018-01-18 15:19:28 +00:00
|
|
|
MqttPublishPrefixTopic_P(STAT, S_RSLT_WARNING);
|
2017-12-16 14:51:45 +00:00
|
|
|
EnergyMqttShow();
|
2018-05-28 14:52:42 +01:00
|
|
|
ExecuteCommandPower(1, POWER_OFF, SRC_MAXPOWER);
|
2017-12-16 14:51:45 +00:00
|
|
|
if (!energy_mplr_counter) {
|
|
|
|
energy_mplr_counter = Settings.param[P_MAX_POWER_RETRY] +1;
|
|
|
|
}
|
|
|
|
energy_mplw_counter = Settings.energy_max_power_limit_window;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (power && (energy_power_u <= Settings.energy_max_power_limit)) {
|
|
|
|
energy_mplh_counter = 0;
|
|
|
|
energy_mplr_counter = 0;
|
|
|
|
energy_mplw_counter = 0;
|
|
|
|
}
|
|
|
|
if (!power) {
|
|
|
|
if (energy_mplw_counter) {
|
|
|
|
energy_mplw_counter--;
|
|
|
|
} else {
|
|
|
|
if (energy_mplr_counter) {
|
|
|
|
energy_mplr_counter--;
|
|
|
|
if (energy_mplr_counter) {
|
2019-03-23 16:00:59 +00:00
|
|
|
Response_P(PSTR("{\"" D_JSON_POWERMONITOR "\":\"%s\"}"), GetStateText(1));
|
2018-01-18 15:19:28 +00:00
|
|
|
MqttPublishPrefixTopic_P(RESULT_OR_STAT, PSTR(D_JSON_POWERMONITOR));
|
2018-05-28 14:52:42 +01:00
|
|
|
ExecuteCommandPower(1, POWER_ON, SRC_MAXPOWER);
|
2017-12-16 14:51:45 +00:00
|
|
|
} else {
|
2019-03-23 16:00:59 +00:00
|
|
|
Response_P(PSTR("{\"" D_JSON_MAXPOWERREACHEDRETRY "\":\"%s\"}"), GetStateText(0));
|
2018-01-18 15:19:28 +00:00
|
|
|
MqttPublishPrefixTopic_P(STAT, S_RSLT_WARNING);
|
2017-12-16 14:51:45 +00:00
|
|
|
EnergyMqttShow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Max Energy
|
|
|
|
if (Settings.energy_max_energy) {
|
|
|
|
energy_daily_u = (uint16_t)(energy_daily * 1000);
|
|
|
|
if (!energy_max_energy_state && (RtcTime.hour == Settings.energy_max_energy_start)) {
|
|
|
|
energy_max_energy_state = 1;
|
2019-03-23 16:00:59 +00:00
|
|
|
Response_P(PSTR("{\"" D_JSON_ENERGYMONITOR "\":\"%s\"}"), GetStateText(1));
|
2018-01-18 15:19:28 +00:00
|
|
|
MqttPublishPrefixTopic_P(RESULT_OR_STAT, PSTR(D_JSON_ENERGYMONITOR));
|
2018-05-28 14:52:42 +01:00
|
|
|
ExecuteCommandPower(1, POWER_ON, SRC_MAXENERGY);
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
else if ((1 == energy_max_energy_state) && (energy_daily_u >= Settings.energy_max_energy)) {
|
|
|
|
energy_max_energy_state = 2;
|
|
|
|
dtostrfd(energy_daily, 3, mqtt_data);
|
2019-03-23 16:00:59 +00:00
|
|
|
Response_P(PSTR("{\"" D_JSON_MAXENERGYREACHED "\":\"%s%s\"}"), mqtt_data, (Settings.flag.value_units) ? " " D_UNIT_KILOWATTHOUR : "");
|
2018-01-18 15:19:28 +00:00
|
|
|
MqttPublishPrefixTopic_P(STAT, S_RSLT_WARNING);
|
2017-12-16 14:51:45 +00:00
|
|
|
EnergyMqttShow();
|
2018-05-28 14:52:42 +01:00
|
|
|
ExecuteCommandPower(1, POWER_OFF, SRC_MAXENERGY);
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // FEATURE_POWER_LIMIT
|
2018-03-15 16:33:35 +00:00
|
|
|
|
|
|
|
if (energy_power_delta) EnergyMqttShow();
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void EnergyMqttShow(void)
|
2017-12-16 14:51:45 +00:00
|
|
|
{
|
|
|
|
// {"Time":"2017-12-16T11:48:55","ENERGY":{"Total":0.212,"Yesterday":0.000,"Today":0.014,"Period":2.0,"Power":22.0,"Factor":1.00,"Voltage":213.6,"Current":0.100}}
|
2019-03-23 16:00:59 +00:00
|
|
|
Response_P(PSTR("{\"" D_JSON_TIME "\":\"%s\""), GetDateAndTime(DT_LOCAL).c_str());
|
2018-09-08 16:18:31 +01:00
|
|
|
int tele_period_save = tele_period;
|
|
|
|
tele_period = 2;
|
2019-01-28 13:08:33 +00:00
|
|
|
EnergyShow(true);
|
2018-09-08 16:18:31 +01:00
|
|
|
tele_period = tele_period_save;
|
Add support for Shelly 1PM Template
Add support for Shelly 1PM Template {"NAME":"Shelly 1PM","GPIO":[56,0,0,0,82,134,0,0,0,0,0,21,0],"FLAG":2,"BASE":18} (#5716)
2019-05-13 17:26:07 +01:00
|
|
|
ResponseJsonEnd();
|
2018-03-29 13:51:34 +01:00
|
|
|
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain);
|
2018-03-15 16:33:35 +00:00
|
|
|
energy_power_delta = 0;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 16:46:40 +01:00
|
|
|
void EnergyOverTempCheck()
|
|
|
|
{
|
|
|
|
if (global_update) {
|
|
|
|
if (power && (global_temperature > ENERGY_OVERTEMP)) { // Device overtemp, turn off relays
|
|
|
|
SetAllPower(POWER_ALL_OFF, SRC_OVERTEMP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 14:51:45 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Commands
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool EnergyCommand(void)
|
2017-12-16 14:51:45 +00:00
|
|
|
{
|
|
|
|
char command [CMDSZ];
|
|
|
|
char sunit[CMDSZ];
|
2019-01-28 13:08:33 +00:00
|
|
|
bool serviced = true;
|
|
|
|
bool status_flag = false;
|
2017-12-16 14:51:45 +00:00
|
|
|
uint8_t unit = 0;
|
|
|
|
unsigned long nvalue = 0;
|
|
|
|
|
2018-01-05 11:26:19 +00:00
|
|
|
int command_code = GetCommandCode(command, sizeof(command), XdrvMailbox.topic, kEnergyCommands);
|
2018-09-04 15:22:34 +01:00
|
|
|
energy_command_code = command_code;
|
2018-05-09 09:49:43 +01:00
|
|
|
if (-1 == command_code) {
|
|
|
|
serviced = false; // Unknown command
|
|
|
|
}
|
|
|
|
else if (CMND_POWERDELTA == command_code) {
|
2018-03-14 13:32:51 +00:00
|
|
|
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) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 3601)) {
|
|
|
|
Settings.energy_min_power = XdrvMailbox.payload;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
nvalue = Settings.energy_min_power;
|
|
|
|
unit = UNIT_WATT;
|
|
|
|
}
|
|
|
|
else if (CMND_POWERHIGH == command_code) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 3601)) {
|
|
|
|
Settings.energy_max_power = XdrvMailbox.payload;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
nvalue = Settings.energy_max_power;
|
|
|
|
unit = UNIT_WATT;
|
|
|
|
}
|
|
|
|
else if (CMND_VOLTAGELOW == command_code) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 501)) {
|
|
|
|
Settings.energy_min_voltage = XdrvMailbox.payload;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
nvalue = Settings.energy_min_voltage;
|
|
|
|
unit = UNIT_VOLT;
|
|
|
|
}
|
|
|
|
else if (CMND_VOLTAGEHIGH == command_code) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 501)) {
|
|
|
|
Settings.energy_max_voltage = XdrvMailbox.payload;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
nvalue = Settings.energy_max_voltage;
|
|
|
|
unit = UNIT_VOLT;
|
|
|
|
}
|
|
|
|
else if (CMND_CURRENTLOW == command_code) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 16001)) {
|
|
|
|
Settings.energy_min_current = XdrvMailbox.payload;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
nvalue = Settings.energy_min_current;
|
|
|
|
unit = UNIT_MILLIAMPERE;
|
|
|
|
}
|
|
|
|
else if (CMND_CURRENTHIGH == command_code) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 16001)) {
|
|
|
|
Settings.energy_max_current = XdrvMailbox.payload;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
nvalue = Settings.energy_max_current;
|
|
|
|
unit = UNIT_MILLIAMPERE;
|
|
|
|
}
|
2018-01-05 11:26:19 +00:00
|
|
|
else if ((CMND_ENERGYRESET == command_code) && (XdrvMailbox.index > 0) && (XdrvMailbox.index <= 3)) {
|
2017-12-16 14:51:45 +00:00
|
|
|
char *p;
|
2018-01-05 11:26:19 +00:00
|
|
|
unsigned long lnum = strtoul(XdrvMailbox.data, &p, 10);
|
|
|
|
if (p != XdrvMailbox.data) {
|
|
|
|
switch (XdrvMailbox.index) {
|
2017-12-16 14:51:45 +00:00
|
|
|
case 1:
|
2018-04-30 11:07:48 +01:00
|
|
|
energy_kWhtoday = lnum *100;
|
|
|
|
energy_kWhtoday_delta = 0;
|
2018-01-24 16:31:20 +00:00
|
|
|
energy_period = energy_kWhtoday;
|
2017-12-16 14:51:45 +00:00
|
|
|
Settings.energy_kWhtoday = energy_kWhtoday;
|
2018-02-04 17:09:09 +00:00
|
|
|
RtcSettings.energy_kWhtoday = energy_kWhtoday;
|
2018-04-30 11:07:48 +01:00
|
|
|
energy_daily = (float)energy_kWhtoday / 100000;
|
2019-03-01 11:08:41 +00:00
|
|
|
if (!RtcSettings.energy_kWhtotal && !energy_kWhtoday) { Settings.energy_kWhtotal_time = LocalTime(); }
|
2017-12-16 14:51:45 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2018-04-30 11:07:48 +01:00
|
|
|
Settings.energy_kWhyesterday = lnum *100;
|
2017-12-16 14:51:45 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
RtcSettings.energy_kWhtotal = lnum *100;
|
|
|
|
Settings.energy_kWhtotal = RtcSettings.energy_kWhtotal;
|
2018-08-23 16:38:17 +01:00
|
|
|
energy_total = (float)(RtcSettings.energy_kWhtotal + energy_kWhtoday) / 100000;
|
2019-03-01 11:08:41 +00:00
|
|
|
Settings.energy_kWhtotal_time = (!energy_kWhtoday) ? LocalTime() : Midnight();
|
2017-12-16 14:51:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-12-21 15:17:06 +00:00
|
|
|
char energy_total_chr[33];
|
2018-08-23 16:38:17 +01:00
|
|
|
dtostrfd(energy_total, Settings.flag2.energy_resolution, energy_total_chr);
|
2018-12-21 15:17:06 +00:00
|
|
|
char energy_daily_chr[33];
|
2018-08-23 16:38:17 +01:00
|
|
|
dtostrfd(energy_daily, Settings.flag2.energy_resolution, energy_daily_chr);
|
2018-12-21 15:17:06 +00:00
|
|
|
char energy_yesterday_chr[33];
|
2018-04-30 11:07:48 +01:00
|
|
|
dtostrfd((float)Settings.energy_kWhyesterday / 100000, Settings.flag2.energy_resolution, energy_yesterday_chr);
|
2018-08-23 16:38:17 +01:00
|
|
|
|
2019-03-23 16:00:59 +00:00
|
|
|
Response_P(PSTR("{\"%s\":{\"" D_JSON_TOTAL "\":%s,\"" D_JSON_YESTERDAY "\":%s,\"" D_JSON_TODAY "\":%s}}"),
|
2018-08-23 16:38:17 +01:00
|
|
|
command, energy_total_chr, energy_yesterday_chr, energy_daily_chr);
|
2019-01-28 13:08:33 +00:00
|
|
|
status_flag = true;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
2019-01-05 14:39:56 +00:00
|
|
|
else if ((CMND_POWERCAL == command_code) && XnrgCall(FUNC_COMMAND)) { // microseconds
|
|
|
|
if ((XdrvMailbox.payload > 999) && (XdrvMailbox.payload < 32001)) { Settings.energy_power_calibration = XdrvMailbox.payload; }
|
2018-09-04 15:22:34 +01:00
|
|
|
nvalue = Settings.energy_power_calibration;
|
|
|
|
unit = UNIT_MICROSECOND;
|
|
|
|
}
|
2019-01-05 14:39:56 +00:00
|
|
|
else if ((CMND_VOLTAGECAL == command_code) && XnrgCall(FUNC_COMMAND)) { // microseconds
|
|
|
|
if ((XdrvMailbox.payload > 999) && (XdrvMailbox.payload < 32001)) { Settings.energy_voltage_calibration = XdrvMailbox.payload; }
|
2018-09-04 15:22:34 +01:00
|
|
|
nvalue = Settings.energy_voltage_calibration;
|
|
|
|
unit = UNIT_MICROSECOND;
|
|
|
|
}
|
2019-01-05 14:39:56 +00:00
|
|
|
else if ((CMND_CURRENTCAL == command_code) && XnrgCall(FUNC_COMMAND)) { // microseconds
|
|
|
|
if ((XdrvMailbox.payload > 999) && (XdrvMailbox.payload < 32001)) { Settings.energy_current_calibration = XdrvMailbox.payload; }
|
2018-02-03 22:25:05 +00:00
|
|
|
nvalue = Settings.energy_current_calibration;
|
2017-12-16 14:51:45 +00:00
|
|
|
unit = UNIT_MICROSECOND;
|
|
|
|
}
|
2019-01-05 14:39:56 +00:00
|
|
|
else if ((CMND_POWERSET == command_code) && XnrgCall(FUNC_COMMAND)) { // Watt
|
|
|
|
nvalue = Settings.energy_power_calibration;
|
|
|
|
unit = UNIT_MILLISECOND;
|
|
|
|
}
|
|
|
|
else if ((CMND_VOLTAGESET == command_code) && XnrgCall(FUNC_COMMAND)) { // Volt
|
|
|
|
nvalue = Settings.energy_voltage_calibration;
|
|
|
|
unit = UNIT_MILLISECOND;
|
|
|
|
}
|
|
|
|
else if ((CMND_CURRENTSET == command_code) && XnrgCall(FUNC_COMMAND)) { // milliAmpere
|
|
|
|
nvalue = Settings.energy_current_calibration;
|
|
|
|
unit = UNIT_MILLISECOND;
|
|
|
|
}
|
2018-09-21 15:02:56 +01:00
|
|
|
else if ((CMND_FREQUENCYSET == command_code) && XnrgCall(FUNC_COMMAND)) { // Hz
|
2018-09-21 14:22:17 +01:00
|
|
|
nvalue = Settings.energy_frequency_calibration;
|
2019-01-05 14:39:56 +00:00
|
|
|
unit = UNIT_MILLISECOND;
|
2018-09-21 14:22:17 +01:00
|
|
|
}
|
2018-02-03 22:25:05 +00:00
|
|
|
|
2017-12-16 14:51:45 +00:00
|
|
|
#if FEATURE_POWER_LIMIT
|
|
|
|
else if (CMND_MAXPOWER == command_code) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 3601)) {
|
|
|
|
Settings.energy_max_power_limit = XdrvMailbox.payload;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
nvalue = Settings.energy_max_power_limit;
|
|
|
|
unit = UNIT_WATT;
|
|
|
|
}
|
|
|
|
else if (CMND_MAXPOWERHOLD == command_code) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 3601)) {
|
|
|
|
Settings.energy_max_power_limit_hold = (1 == XdrvMailbox.payload) ? MAX_POWER_HOLD : XdrvMailbox.payload;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
nvalue = Settings.energy_max_power_limit_hold;
|
|
|
|
unit = UNIT_SECOND;
|
|
|
|
}
|
|
|
|
else if (CMND_MAXPOWERWINDOW == command_code) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 3601)) {
|
|
|
|
Settings.energy_max_power_limit_window = (1 == XdrvMailbox.payload) ? MAX_POWER_WINDOW : XdrvMailbox.payload;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
nvalue = Settings.energy_max_power_limit_window;
|
|
|
|
unit = UNIT_SECOND;
|
|
|
|
}
|
|
|
|
else if (CMND_SAFEPOWER == command_code) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 3601)) {
|
|
|
|
Settings.energy_max_power_safe_limit = XdrvMailbox.payload;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
nvalue = Settings.energy_max_power_safe_limit;
|
|
|
|
unit = UNIT_WATT;
|
|
|
|
}
|
|
|
|
else if (CMND_SAFEPOWERHOLD == command_code) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 3601)) {
|
|
|
|
Settings.energy_max_power_safe_limit_hold = (1 == XdrvMailbox.payload) ? SAFE_POWER_HOLD : XdrvMailbox.payload;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
nvalue = Settings.energy_max_power_safe_limit_hold;
|
|
|
|
unit = UNIT_SECOND;
|
|
|
|
}
|
|
|
|
else if (CMND_SAFEPOWERWINDOW == command_code) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 1440)) {
|
|
|
|
Settings.energy_max_power_safe_limit_window = (1 == XdrvMailbox.payload) ? SAFE_POWER_WINDOW : XdrvMailbox.payload;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
nvalue = Settings.energy_max_power_safe_limit_window;
|
|
|
|
unit = UNIT_MINUTE;
|
|
|
|
}
|
|
|
|
else if (CMND_MAXENERGY == command_code) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 3601)) {
|
|
|
|
Settings.energy_max_energy = XdrvMailbox.payload;
|
2017-12-16 14:51:45 +00:00
|
|
|
energy_max_energy_state = 3;
|
|
|
|
}
|
|
|
|
nvalue = Settings.energy_max_energy;
|
|
|
|
unit = UNIT_WATTHOUR;
|
|
|
|
}
|
|
|
|
else if (CMND_MAXENERGYSTART == command_code) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 24)) {
|
|
|
|
Settings.energy_max_energy_start = XdrvMailbox.payload;
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
nvalue = Settings.energy_max_energy_start;
|
|
|
|
unit = UNIT_HOUR;
|
|
|
|
}
|
|
|
|
#endif // FEATURE_POWER_LIMIT
|
2018-05-09 09:49:43 +01:00
|
|
|
else serviced = false; // Unknown command
|
|
|
|
|
|
|
|
if (serviced && !status_flag) {
|
2018-09-22 14:09:53 +01:00
|
|
|
|
2019-01-05 14:39:56 +00:00
|
|
|
if (UNIT_MILLISECOND == unit) {
|
2018-09-22 14:09:53 +01:00
|
|
|
snprintf_P(command, sizeof(command), PSTR("%sCal"), command);
|
2019-01-05 14:39:56 +00:00
|
|
|
unit = UNIT_MICROSECOND;
|
2018-09-22 14:09:53 +01:00
|
|
|
}
|
|
|
|
|
2017-12-16 14:51:45 +00:00
|
|
|
if (Settings.flag.value_units) {
|
2019-03-23 16:00:59 +00:00
|
|
|
Response_P(S_JSON_COMMAND_LVALUE_SPACE_UNIT, command, nvalue, GetTextIndexed(sunit, sizeof(sunit), unit, kUnitNames));
|
2017-12-16 14:51:45 +00:00
|
|
|
} else {
|
2019-03-23 16:00:59 +00:00
|
|
|
Response_P(S_JSON_COMMAND_LVALUE, command, nvalue);
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-09 09:49:43 +01:00
|
|
|
|
2017-12-16 14:51:45 +00:00
|
|
|
return serviced;
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void EnergyDrvInit(void)
|
2017-12-16 14:51:45 +00:00
|
|
|
{
|
|
|
|
energy_flg = ENERGY_NONE;
|
2018-09-04 15:22:34 +01:00
|
|
|
XnrgCall(FUNC_PRE_INIT);
|
2018-02-03 22:25:05 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void EnergySnsInit(void)
|
2018-02-03 22:25:05 +00:00
|
|
|
{
|
2018-09-04 15:22:34 +01:00
|
|
|
XnrgCall(FUNC_INIT);
|
2018-02-13 13:30:30 +00:00
|
|
|
|
2017-12-16 14:51:45 +00:00
|
|
|
if (energy_flg) {
|
2018-02-04 17:09:09 +00:00
|
|
|
energy_kWhtoday = (RtcSettingsValid()) ? RtcSettings.energy_kWhtoday : (RtcTime.day_of_year == Settings.energy_kWhdoy) ? Settings.energy_kWhtoday : 0;
|
2018-04-30 11:07:48 +01:00
|
|
|
energy_kWhtoday_delta = 0;
|
2018-01-24 16:31:20 +00:00
|
|
|
energy_period = energy_kWhtoday;
|
2018-02-04 17:09:09 +00:00
|
|
|
EnergyUpdateToday();
|
2017-12-16 14:51:45 +00:00
|
|
|
ticker_energy.attach_ms(200, Energy200ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_WEBSERVER
|
2019-03-19 16:31:43 +00:00
|
|
|
const char HTTP_ENERGY_SNS1[] PROGMEM =
|
2018-09-28 14:48:42 +01:00
|
|
|
"{s}" D_POWERUSAGE_APPARENT "{m}%s " D_UNIT_VA "{e}"
|
|
|
|
"{s}" D_POWERUSAGE_REACTIVE "{m}%s " D_UNIT_VAR "{e}"
|
2018-09-07 17:15:47 +01:00
|
|
|
"{s}" D_POWER_FACTOR "{m}%s{e}";
|
|
|
|
|
Add support for Shelly 1PM Template
Add support for Shelly 1PM Template {"NAME":"Shelly 1PM","GPIO":[56,0,0,0,82,134,0,0,0,0,0,21,0],"FLAG":2,"BASE":18} (#5716)
2019-05-13 17:26:07 +01:00
|
|
|
const char HTTP_ENERGY_SNS2[] PROGMEM =
|
2017-12-16 14:51:45 +00:00
|
|
|
"{s}" D_ENERGY_TODAY "{m}%s " D_UNIT_KILOWATTHOUR "{e}"
|
|
|
|
"{s}" D_ENERGY_YESTERDAY "{m}%s " D_UNIT_KILOWATTHOUR "{e}"
|
|
|
|
"{s}" D_ENERGY_TOTAL "{m}%s " D_UNIT_KILOWATTHOUR "{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void EnergyShow(bool json)
|
2017-12-16 14:51:45 +00:00
|
|
|
{
|
|
|
|
char speriod[20];
|
Add support for Shelly 1PM Template
Add support for Shelly 1PM Template {"NAME":"Shelly 1PM","GPIO":[56,0,0,0,82,134,0,0,0,0,0,21,0],"FLAG":2,"BASE":18} (#5716)
2019-05-13 17:26:07 +01:00
|
|
|
// char sfrequency[20];
|
2017-12-16 14:51:45 +00:00
|
|
|
|
|
|
|
bool show_energy_period = (0 == tele_period);
|
|
|
|
|
2018-09-28 16:02:55 +01:00
|
|
|
float power_factor = energy_power_factor;
|
|
|
|
|
2018-12-21 15:17:06 +00:00
|
|
|
char apparent_power_chr[33];
|
|
|
|
char reactive_power_chr[33];
|
|
|
|
char power_factor_chr[33];
|
|
|
|
char frequency_chr[33];
|
2018-09-28 14:48:42 +01:00
|
|
|
if (!energy_type_dc) {
|
Add support for Shelly 1PM Template
Add support for Shelly 1PM Template {"NAME":"Shelly 1PM","GPIO":[56,0,0,0,82,134,0,0,0,0,0,21,0],"FLAG":2,"BASE":18} (#5716)
2019-05-13 17:26:07 +01:00
|
|
|
if (energy_current_available && energy_voltage_available) {
|
|
|
|
float apparent_power = energy_apparent_power;
|
|
|
|
if (isnan(apparent_power)) {
|
|
|
|
apparent_power = energy_voltage * energy_current;
|
|
|
|
}
|
|
|
|
if (apparent_power < energy_active_power) { // Should be impossible
|
|
|
|
energy_active_power = apparent_power;
|
|
|
|
}
|
2018-09-28 14:48:42 +01:00
|
|
|
|
Add support for Shelly 1PM Template
Add support for Shelly 1PM Template {"NAME":"Shelly 1PM","GPIO":[56,0,0,0,82,134,0,0,0,0,0,21,0],"FLAG":2,"BASE":18} (#5716)
2019-05-13 17:26:07 +01:00
|
|
|
if (isnan(power_factor)) {
|
|
|
|
power_factor = (energy_active_power && apparent_power) ? energy_active_power / apparent_power : 0;
|
|
|
|
if (power_factor > 1) power_factor = 1;
|
|
|
|
}
|
2018-09-28 14:48:42 +01:00
|
|
|
|
Add support for Shelly 1PM Template
Add support for Shelly 1PM Template {"NAME":"Shelly 1PM","GPIO":[56,0,0,0,82,134,0,0,0,0,0,21,0],"FLAG":2,"BASE":18} (#5716)
2019-05-13 17:26:07 +01:00
|
|
|
float reactive_power = energy_reactive_power;
|
|
|
|
if (isnan(reactive_power)) {
|
|
|
|
reactive_power = 0;
|
|
|
|
uint32_t difference = ((uint32_t)(apparent_power * 100) - (uint32_t)(energy_active_power * 100)) / 10;
|
|
|
|
if ((energy_current > 0.005) && ((difference > 15) || (difference > (uint32_t)(apparent_power * 100 / 1000)))) {
|
|
|
|
// calculating reactive power only if current is greater than 0.005A and
|
|
|
|
// difference between active and apparent power is greater than 1.5W or 1%
|
|
|
|
reactive_power = (float)(RoundSqrtInt((uint32_t)(apparent_power * apparent_power * 100) - (uint32_t)(energy_active_power * energy_active_power * 100))) / 10;
|
|
|
|
}
|
2018-09-28 14:48:42 +01:00
|
|
|
}
|
|
|
|
|
Add support for Shelly 1PM Template
Add support for Shelly 1PM Template {"NAME":"Shelly 1PM","GPIO":[56,0,0,0,82,134,0,0,0,0,0,21,0],"FLAG":2,"BASE":18} (#5716)
2019-05-13 17:26:07 +01:00
|
|
|
dtostrfd(apparent_power, Settings.flag2.wattage_resolution, apparent_power_chr);
|
|
|
|
dtostrfd(reactive_power, Settings.flag2.wattage_resolution, reactive_power_chr);
|
|
|
|
dtostrfd(power_factor, 2, power_factor_chr);
|
|
|
|
}
|
2018-09-28 14:48:42 +01:00
|
|
|
if (!isnan(energy_frequency)) {
|
|
|
|
dtostrfd(energy_frequency, Settings.flag2.frequency_resolution, frequency_chr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 15:17:06 +00:00
|
|
|
char voltage_chr[33];
|
2018-09-28 14:48:42 +01:00
|
|
|
dtostrfd(energy_voltage, Settings.flag2.voltage_resolution, voltage_chr);
|
2018-12-21 15:17:06 +00:00
|
|
|
char current_chr[33];
|
2018-09-28 14:48:42 +01:00
|
|
|
dtostrfd(energy_current, Settings.flag2.current_resolution, current_chr);
|
2018-12-21 15:17:06 +00:00
|
|
|
char active_power_chr[33];
|
2018-09-28 14:48:42 +01:00
|
|
|
dtostrfd(energy_active_power, Settings.flag2.wattage_resolution, active_power_chr);
|
2018-12-21 15:17:06 +00:00
|
|
|
char energy_daily_chr[33];
|
2018-09-07 17:15:47 +01:00
|
|
|
dtostrfd(energy_daily, Settings.flag2.energy_resolution, energy_daily_chr);
|
2018-12-21 15:17:06 +00:00
|
|
|
char energy_yesterday_chr[33];
|
2018-09-07 17:15:47 +01:00
|
|
|
dtostrfd((float)Settings.energy_kWhyesterday / 100000, Settings.flag2.energy_resolution, energy_yesterday_chr);
|
2018-12-21 15:17:06 +00:00
|
|
|
char energy_total_chr[33];
|
2018-09-28 14:48:42 +01:00
|
|
|
dtostrfd(energy_total, Settings.flag2.energy_resolution, energy_total_chr);
|
2018-09-07 17:15:47 +01:00
|
|
|
|
2017-12-16 14:51:45 +00:00
|
|
|
float energy = 0;
|
2018-12-21 15:17:06 +00:00
|
|
|
char energy_period_chr[33];
|
2017-12-16 14:51:45 +00:00
|
|
|
if (show_energy_period) {
|
2018-04-30 11:07:48 +01:00
|
|
|
if (energy_period) energy = (float)(energy_kWhtoday - energy_period) / 100;
|
2017-12-16 14:51:45 +00:00
|
|
|
energy_period = energy_kWhtoday;
|
2018-09-07 17:15:47 +01:00
|
|
|
dtostrfd(energy, Settings.flag2.wattage_resolution, energy_period_chr);
|
|
|
|
snprintf_P(speriod, sizeof(speriod), PSTR(",\"" D_JSON_PERIOD "\":%s"), energy_period_chr);
|
|
|
|
}
|
2017-12-16 14:51:45 +00:00
|
|
|
|
|
|
|
if (json) {
|
2019-03-23 16:00:59 +00:00
|
|
|
ResponseAppend_P(PSTR(",\"" D_RSLT_ENERGY "\":{\"" D_JSON_TOTAL_START_TIME "\":\"%s\",\"" D_JSON_TOTAL "\":%s,\"" D_JSON_YESTERDAY "\":%s,\"" D_JSON_TODAY "\":%s%s,\"" D_JSON_POWERUSAGE "\":%s"),
|
|
|
|
GetDateAndTime(DT_ENERGY).c_str(), energy_total_chr, energy_yesterday_chr, energy_daily_chr, (show_energy_period) ? speriod : "", active_power_chr);
|
2018-09-28 14:48:42 +01:00
|
|
|
if (!energy_type_dc) {
|
Add support for Shelly 1PM Template
Add support for Shelly 1PM Template {"NAME":"Shelly 1PM","GPIO":[56,0,0,0,82,134,0,0,0,0,0,21,0],"FLAG":2,"BASE":18} (#5716)
2019-05-13 17:26:07 +01:00
|
|
|
if (energy_current_available && energy_voltage_available) {
|
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_APPARENT_POWERUSAGE "\":%s,\"" D_JSON_REACTIVE_POWERUSAGE "\":%s,\"" D_JSON_POWERFACTOR "\":%s"),
|
|
|
|
apparent_power_chr, reactive_power_chr, power_factor_chr);
|
|
|
|
}
|
|
|
|
if (!isnan(energy_frequency)) {
|
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_FREQUENCY "\":%s"), frequency_chr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (energy_voltage_available) {
|
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_VOLTAGE "\":%s"), voltage_chr);
|
2018-09-28 14:48:42 +01:00
|
|
|
}
|
Add support for Shelly 1PM Template
Add support for Shelly 1PM Template {"NAME":"Shelly 1PM","GPIO":[56,0,0,0,82,134,0,0,0,0,0,21,0],"FLAG":2,"BASE":18} (#5716)
2019-05-13 17:26:07 +01:00
|
|
|
if (energy_current_available) {
|
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_CURRENT "\":%s"), current_chr);
|
|
|
|
}
|
|
|
|
ResponseJsonEnd();
|
2018-09-28 14:48:42 +01:00
|
|
|
|
2017-12-16 14:51:45 +00:00
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
if (show_energy_period) { // Only send if telemetry
|
|
|
|
dtostrfd(energy_total * 1000, 1, energy_total_chr);
|
2018-09-28 14:48:42 +01:00
|
|
|
DomoticzSensorPowerEnergy((int)energy_active_power, energy_total_chr); // PowerUsage, EnergyToday
|
Add support for Shelly 1PM Template
Add support for Shelly 1PM Template {"NAME":"Shelly 1PM","GPIO":[56,0,0,0,82,134,0,0,0,0,0,21,0],"FLAG":2,"BASE":18} (#5716)
2019-05-13 17:26:07 +01:00
|
|
|
if (energy_voltage_available) {
|
|
|
|
DomoticzSensor(DZ_VOLTAGE, voltage_chr); // Voltage
|
|
|
|
}
|
|
|
|
if (energy_current_available) {
|
|
|
|
DomoticzSensor(DZ_CURRENT, current_chr); // Current
|
|
|
|
}
|
2017-12-16 14:51:45 +00:00
|
|
|
}
|
|
|
|
#endif // USE_DOMOTICZ
|
2018-05-17 05:44:46 +01:00
|
|
|
#ifdef USE_KNX
|
|
|
|
if (show_energy_period) {
|
Add support for Shelly 1PM Template
Add support for Shelly 1PM Template {"NAME":"Shelly 1PM","GPIO":[56,0,0,0,82,134,0,0,0,0,0,21,0],"FLAG":2,"BASE":18} (#5716)
2019-05-13 17:26:07 +01:00
|
|
|
if (energy_voltage_available) {
|
|
|
|
KnxSensor(KNX_ENERGY_VOLTAGE, energy_voltage);
|
|
|
|
}
|
|
|
|
if (energy_current_available) {
|
|
|
|
KnxSensor(KNX_ENERGY_CURRENT, energy_current);
|
|
|
|
}
|
2018-09-28 14:48:42 +01:00
|
|
|
KnxSensor(KNX_ENERGY_POWER, energy_active_power);
|
|
|
|
if (!energy_type_dc) { KnxSensor(KNX_ENERGY_POWERFACTOR, power_factor); }
|
2018-05-17 05:44:46 +01:00
|
|
|
KnxSensor(KNX_ENERGY_DAILY, energy_daily);
|
|
|
|
KnxSensor(KNX_ENERGY_TOTAL, energy_total);
|
|
|
|
KnxSensor(KNX_ENERGY_START, energy_start);
|
|
|
|
}
|
|
|
|
#endif // USE_KNX
|
2017-12-16 14:51:45 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
Add support for Shelly 1PM Template
Add support for Shelly 1PM Template {"NAME":"Shelly 1PM","GPIO":[56,0,0,0,82,134,0,0,0,0,0,21,0],"FLAG":2,"BASE":18} (#5716)
2019-05-13 17:26:07 +01:00
|
|
|
if (energy_voltage_available) {
|
|
|
|
WSContentSend_PD(PSTR("{s}" D_VOLTAGE "{m}%s " D_UNIT_VOLT "{e}"), voltage_chr);
|
|
|
|
}
|
|
|
|
if (energy_current_available) {
|
|
|
|
WSContentSend_PD(PSTR("{s}" D_CURRENT "{m}%s " D_UNIT_AMPERE "{e}"), current_chr);
|
|
|
|
}
|
|
|
|
WSContentSend_PD(PSTR("{s}" D_POWERUSAGE "{m}%s " D_UNIT_WATT "{e}"), active_power_chr);
|
2018-09-28 14:48:42 +01:00
|
|
|
if (!energy_type_dc) {
|
Add support for Shelly 1PM Template
Add support for Shelly 1PM Template {"NAME":"Shelly 1PM","GPIO":[56,0,0,0,82,134,0,0,0,0,0,21,0],"FLAG":2,"BASE":18} (#5716)
2019-05-13 17:26:07 +01:00
|
|
|
if (energy_current_available && energy_voltage_available) {
|
|
|
|
WSContentSend_PD(HTTP_ENERGY_SNS1, apparent_power_chr, reactive_power_chr, power_factor_chr);
|
|
|
|
}
|
|
|
|
if (!isnan(energy_frequency)) {
|
|
|
|
WSContentSend_PD(PSTR("{s}" D_FREQUENCY "{m}%s " D_UNIT_HERTZ "{e}"), frequency_chr);
|
|
|
|
}
|
2018-09-28 14:48:42 +01:00
|
|
|
}
|
Add support for Shelly 1PM Template
Add support for Shelly 1PM Template {"NAME":"Shelly 1PM","GPIO":[56,0,0,0,82,134,0,0,0,0,0,21,0],"FLAG":2,"BASE":18} (#5716)
2019-05-13 17:26:07 +01:00
|
|
|
WSContentSend_PD(HTTP_ENERGY_SNS2, energy_daily_chr, energy_yesterday_chr, energy_total_chr);
|
2017-12-16 14:51:45 +00:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xdrv03(uint8_t function)
|
2018-01-05 11:26:19 +00:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2018-01-05 11:26:19 +00:00
|
|
|
|
2018-09-04 15:22:34 +01:00
|
|
|
if (FUNC_PRE_INIT == function) {
|
|
|
|
EnergyDrvInit();
|
|
|
|
}
|
|
|
|
else if (energy_flg) {
|
2018-01-05 11:26:19 +00:00
|
|
|
switch (function) {
|
2019-03-30 12:03:45 +00:00
|
|
|
case FUNC_LOOP:
|
|
|
|
XnrgCall(FUNC_LOOP);
|
|
|
|
break;
|
2018-01-05 11:26:19 +00:00
|
|
|
case FUNC_COMMAND:
|
|
|
|
result = EnergyCommand();
|
|
|
|
break;
|
|
|
|
case FUNC_SET_POWER:
|
|
|
|
EnergySetPowerSteadyCounter();
|
|
|
|
break;
|
2018-09-04 15:22:34 +01:00
|
|
|
case FUNC_SERIAL:
|
|
|
|
result = XnrgCall(FUNC_SERIAL);
|
|
|
|
break;
|
2018-01-05 11:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xsns03(uint8_t function)
|
2017-12-16 14:51:45 +00:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2017-12-16 14:51:45 +00:00
|
|
|
|
|
|
|
if (energy_flg) {
|
|
|
|
switch (function) {
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_INIT:
|
2018-02-13 13:30:30 +00:00
|
|
|
EnergySnsInit();
|
2017-12-16 14:51:45 +00:00
|
|
|
break;
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_EVERY_SECOND:
|
2017-12-16 14:51:45 +00:00
|
|
|
EnergyMarginCheck();
|
2019-05-14 16:46:40 +01:00
|
|
|
EnergyOverTempCheck();
|
2017-12-16 14:51:45 +00:00
|
|
|
break;
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_JSON_APPEND:
|
2019-01-28 13:08:33 +00:00
|
|
|
EnergyShow(true);
|
2017-12-16 14:51:45 +00:00
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
2019-03-19 16:31:43 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
2019-01-28 13:08:33 +00:00
|
|
|
EnergyShow(false);
|
2017-12-16 14:51:45 +00:00
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_SAVE_BEFORE_RESTART:
|
2017-12-16 14:51:45 +00:00
|
|
|
EnergySaveState();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-08-27 22:35:23 +01:00
|
|
|
#endif // USE_ENERGY_SENSOR
|