2018-09-04 15:22:34 +01:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xnrg_01_hlw8012.ino - HLW8012 (Sonoff Pow) energy sensor support for Tasmota
|
2018-09-04 15:22:34 +01:00
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Theo Arends
|
2018-09-04 15:22:34 +01: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
|
|
|
|
#ifdef USE_HLW8012
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* HLW8012, BL0937 or HJL-01 - Energy (Sonoff Pow, HuaFan, KMC70011, BlitzWolf)
|
|
|
|
*
|
|
|
|
* Based on Source: Shenzhen Heli Technology Co., Ltd
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define XNRG_01 1
|
|
|
|
|
2018-10-07 11:37:03 +01:00
|
|
|
// Energy model type 0 (GPIO_HLW_CF) - HLW8012 based (Sonoff Pow, KMC70011, HuaFan, AplicWDP303075)
|
2018-09-04 15:22:34 +01:00
|
|
|
#define HLW_PREF 10000 // 1000.0W
|
|
|
|
#define HLW_UREF 2200 // 220.0V
|
|
|
|
#define HLW_IREF 4545 // 4.545A
|
|
|
|
|
2018-10-07 11:37:03 +01:00
|
|
|
// Energy model type 1 (GPIO_HJL_CF) - HJL-01/BL0937 based (BlitzWolf, Homecube, Gosund, Teckin)
|
2018-09-04 15:22:34 +01:00
|
|
|
#define HJL_PREF 1362
|
|
|
|
#define HJL_UREF 822
|
|
|
|
#define HJL_IREF 3300
|
|
|
|
|
2019-01-06 14:17:22 +00:00
|
|
|
#define HLW_POWER_PROBE_TIME 10 // Number of seconds to probe for power before deciding none used (low power pulse can take up to 10 seconds)
|
|
|
|
#define HLW_SAMPLE_COUNT 10 // Max number of samples per cycle
|
2018-09-04 15:22:34 +01:00
|
|
|
|
2019-01-06 14:17:22 +00:00
|
|
|
//#define HLW_DEBUG
|
2019-08-16 13:41:02 +01:00
|
|
|
|
|
|
|
struct HLW {
|
2019-01-06 14:17:22 +00:00
|
|
|
#ifdef HLW_DEBUG
|
2020-10-13 14:55:46 +01:00
|
|
|
uint32_t debug[HLW_SAMPLE_COUNT];
|
2019-01-06 14:17:22 +00:00
|
|
|
#endif
|
2020-10-13 14:55:46 +01:00
|
|
|
volatile uint32_t cf_pulse_length = 0;
|
|
|
|
volatile uint32_t cf_pulse_last_time = 0;
|
2021-04-14 16:47:04 +01:00
|
|
|
volatile uint32_t cf_summed_pulse_length = 0;
|
|
|
|
volatile uint32_t cf_pulse_counter = 0;
|
2020-10-13 14:55:46 +01:00
|
|
|
uint32_t cf_power_pulse_length = 0;
|
2019-01-06 14:17:22 +00:00
|
|
|
|
2020-10-13 14:55:46 +01:00
|
|
|
volatile uint32_t cf1_pulse_length = 0;
|
|
|
|
volatile uint32_t cf1_pulse_last_time = 0;
|
|
|
|
volatile uint32_t cf1_summed_pulse_length = 0;
|
|
|
|
volatile uint32_t cf1_pulse_counter = 0;
|
2021-04-14 16:47:04 +01:00
|
|
|
|
2020-10-13 14:55:46 +01:00
|
|
|
uint32_t cf1_voltage_pulse_length = 0;
|
|
|
|
uint32_t cf1_current_pulse_length = 0;
|
2019-08-16 13:41:02 +01:00
|
|
|
|
2020-10-13 14:55:46 +01:00
|
|
|
volatile uint32_t energy_period_counter = 0;
|
2019-08-16 13:41:02 +01:00
|
|
|
|
2020-10-13 14:55:46 +01:00
|
|
|
uint32_t power_ratio = 0;
|
|
|
|
uint32_t voltage_ratio = 0;
|
|
|
|
uint32_t current_ratio = 0;
|
2019-08-16 13:41:02 +01:00
|
|
|
|
|
|
|
uint8_t model_type = 0;
|
2020-10-13 14:55:46 +01:00
|
|
|
volatile uint8_t cf1_timer = 0;
|
2019-08-16 13:41:02 +01:00
|
|
|
uint8_t power_retry = 0;
|
|
|
|
bool select_ui_flag = false;
|
|
|
|
bool ui_flag = true;
|
2020-10-13 14:55:46 +01:00
|
|
|
volatile bool load_off = true;
|
2019-08-16 13:41:02 +01:00
|
|
|
} Hlw;
|
2019-01-05 17:40:33 +00:00
|
|
|
|
2019-05-22 09:39:37 +01:00
|
|
|
// Fix core 2.5.x ISR not in IRAM Exception
|
2018-09-04 15:22:34 +01:00
|
|
|
#ifndef USE_WS2812_DMA // Collides with Neopixelbus but solves exception
|
2021-04-02 16:14:08 +01:00
|
|
|
void HlwCfInterrupt(void) IRAM_ATTR;
|
|
|
|
void HlwCf1Interrupt(void) IRAM_ATTR;
|
2018-09-04 15:22:34 +01:00
|
|
|
#endif // USE_WS2812_DMA
|
|
|
|
|
2021-04-14 16:47:04 +01:00
|
|
|
void HlwCfInterrupt(void) { // Service Power
|
2020-10-13 14:55:46 +01:00
|
|
|
uint32_t us = micros();
|
2018-09-04 15:22:34 +01:00
|
|
|
|
2019-08-16 13:41:02 +01:00
|
|
|
if (Hlw.load_off) { // Restart plen measurement
|
|
|
|
Hlw.cf_pulse_last_time = us;
|
|
|
|
Hlw.load_off = false;
|
2018-09-04 15:22:34 +01:00
|
|
|
} else {
|
2019-08-16 13:41:02 +01:00
|
|
|
Hlw.cf_pulse_length = us - Hlw.cf_pulse_last_time;
|
|
|
|
Hlw.cf_pulse_last_time = us;
|
2021-04-14 16:47:04 +01:00
|
|
|
Hlw.cf_summed_pulse_length += Hlw.cf_pulse_length;
|
|
|
|
Hlw.cf_pulse_counter++;
|
2019-08-16 13:41:02 +01:00
|
|
|
Hlw.energy_period_counter++;
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->data_valid[0] = 0;
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 16:47:04 +01:00
|
|
|
void HlwCf1Interrupt(void) { // Service Voltage and Current
|
2020-10-13 14:55:46 +01:00
|
|
|
uint32_t us = micros();
|
2018-09-04 15:22:34 +01:00
|
|
|
|
2019-08-16 13:41:02 +01:00
|
|
|
Hlw.cf1_pulse_length = us - Hlw.cf1_pulse_last_time;
|
|
|
|
Hlw.cf1_pulse_last_time = us;
|
|
|
|
if ((Hlw.cf1_timer > 2) && (Hlw.cf1_timer < 8)) { // Allow for 300 mSec set-up time and measure for up to 1 second
|
|
|
|
Hlw.cf1_summed_pulse_length += Hlw.cf1_pulse_length;
|
2019-01-06 14:17:22 +00:00
|
|
|
#ifdef HLW_DEBUG
|
2019-08-16 13:41:02 +01:00
|
|
|
Hlw.debug[Hlw.cf1_pulse_counter] = Hlw.cf1_pulse_length;
|
2019-01-06 14:17:22 +00:00
|
|
|
#endif
|
2019-08-16 13:41:02 +01:00
|
|
|
Hlw.cf1_pulse_counter++;
|
|
|
|
if (HLW_SAMPLE_COUNT == Hlw.cf1_pulse_counter) {
|
|
|
|
Hlw.cf1_timer = 8; // We need up to HLW_SAMPLE_COUNT samples within 1 second (low current could take up to 0.3 second)
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->data_valid[0] = 0;
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2021-04-14 16:47:04 +01:00
|
|
|
void HlwEvery200ms(void) {
|
2020-10-13 14:55:46 +01:00
|
|
|
uint32_t cf1_pulse_length = 0;
|
|
|
|
uint32_t hlw_w = 0;
|
|
|
|
uint32_t hlw_u = 0;
|
|
|
|
uint32_t hlw_i = 0;
|
2018-09-04 15:22:34 +01:00
|
|
|
|
2019-08-16 13:41:02 +01:00
|
|
|
if (micros() - Hlw.cf_pulse_last_time > (HLW_POWER_PROBE_TIME * 1000000)) {
|
|
|
|
Hlw.cf_pulse_length = 0; // No load for some time
|
|
|
|
Hlw.load_off = true;
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
2021-04-14 16:47:04 +01:00
|
|
|
Hlw.cf_power_pulse_length = Hlw.cf_pulse_length;
|
|
|
|
if (Hlw.cf_pulse_counter && !Hlw.load_off) {
|
|
|
|
Hlw.cf_power_pulse_length = Hlw.cf_summed_pulse_length / Hlw.cf_pulse_counter;
|
|
|
|
}
|
|
|
|
Hlw.cf_summed_pulse_length = 0;
|
|
|
|
Hlw.cf_pulse_counter = 0;
|
2018-09-04 15:22:34 +01:00
|
|
|
|
2023-01-24 15:54:03 +00:00
|
|
|
if (Hlw.cf_power_pulse_length && Energy->power_on && !Hlw.load_off) {
|
2021-06-11 17:14:12 +01:00
|
|
|
hlw_w = (Hlw.power_ratio * Settings->energy_power_calibration) / Hlw.cf_power_pulse_length ; // W *10
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->active_power[0] = (float)hlw_w / 10;
|
2019-08-16 13:41:02 +01:00
|
|
|
Hlw.power_retry = 1; // Workaround issue #5161
|
2018-09-04 15:22:34 +01:00
|
|
|
} else {
|
2019-08-16 13:41:02 +01:00
|
|
|
if (Hlw.power_retry) {
|
|
|
|
Hlw.power_retry--;
|
2019-06-05 15:14:50 +01:00
|
|
|
} else {
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->active_power[0] = 0;
|
2019-06-05 15:14:50 +01:00
|
|
|
}
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
|
|
|
|
2020-04-27 11:54:07 +01:00
|
|
|
if (PinUsed(GPIO_NRG_CF1)) {
|
2019-08-16 13:41:02 +01:00
|
|
|
Hlw.cf1_timer++;
|
|
|
|
if (Hlw.cf1_timer >= 8) {
|
|
|
|
Hlw.cf1_timer = 0;
|
|
|
|
Hlw.select_ui_flag = (Hlw.select_ui_flag) ? false : true;
|
2020-04-27 09:35:38 +01:00
|
|
|
DigitalWrite(GPIO_NRG_SEL, 0, Hlw.select_ui_flag);
|
2018-09-04 15:22:34 +01:00
|
|
|
|
2019-08-16 13:41:02 +01:00
|
|
|
if (Hlw.cf1_pulse_counter) {
|
|
|
|
cf1_pulse_length = Hlw.cf1_summed_pulse_length / Hlw.cf1_pulse_counter;
|
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
|
|
|
}
|
2019-01-05 17:40:33 +00:00
|
|
|
|
2019-01-06 14:17:22 +00:00
|
|
|
#ifdef HLW_DEBUG
|
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
|
|
|
// Debugging for calculating mean and median
|
|
|
|
char stemp[100];
|
|
|
|
stemp[0] = '\0';
|
2019-08-16 13:41:02 +01:00
|
|
|
for (uint32_t i = 0; i < Hlw.cf1_pulse_counter; i++) {
|
|
|
|
snprintf_P(stemp, sizeof(stemp), PSTR("%s %d"), stemp, Hlw.debug[i]);
|
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
|
|
|
}
|
2019-08-16 13:41:02 +01:00
|
|
|
for (uint32_t i = 0; i < Hlw.cf1_pulse_counter; i++) {
|
|
|
|
for (uint32_t j = i + 1; j < Hlw.cf1_pulse_counter; j++) {
|
|
|
|
if (Hlw.debug[i] > Hlw.debug[j]) { // Sort ascending
|
|
|
|
std::swap(Hlw.debug[i], Hlw.debug[j]);
|
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
|
|
|
}
|
2019-01-06 14:17:22 +00:00
|
|
|
}
|
2019-01-05 17:40:33 +00:00
|
|
|
}
|
2020-10-13 14:55:46 +01:00
|
|
|
uint32_t median = Hlw.debug[(Hlw.cf1_pulse_counter +1) / 2];
|
2021-01-23 16:10:06 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR("NRG: power %d, ui %d, cnt %d, smpl%s, sum %d, mean %d, median %d"),
|
2019-08-16 13:41:02 +01:00
|
|
|
Hlw.cf_power_pulse_length , Hlw.select_ui_flag, Hlw.cf1_pulse_counter, stemp, Hlw.cf1_summed_pulse_length, cf1_pulse_length, median);
|
2019-01-06 14:17:22 +00:00
|
|
|
#endif
|
2019-01-05 17:40:33 +00:00
|
|
|
|
2019-08-16 13:41:02 +01:00
|
|
|
if (Hlw.select_ui_flag == Hlw.ui_flag) {
|
|
|
|
Hlw.cf1_voltage_pulse_length = cf1_pulse_length;
|
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
|
|
|
|
2023-01-24 15:54:03 +00:00
|
|
|
if (Hlw.cf1_voltage_pulse_length && Energy->power_on) { // If powered on always provide voltage
|
2021-06-11 17:14:12 +01:00
|
|
|
hlw_u = (Hlw.voltage_ratio * Settings->energy_voltage_calibration) / Hlw.cf1_voltage_pulse_length ; // V *10
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->voltage[0] = (float)hlw_u / 10;
|
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
|
|
|
} else {
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->voltage[0] = 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
|
|
|
}
|
2018-09-04 15:22:34 +01:00
|
|
|
|
|
|
|
} else {
|
2019-08-16 13:41:02 +01:00
|
|
|
Hlw.cf1_current_pulse_length = cf1_pulse_length;
|
2018-09-04 15:22:34 +01:00
|
|
|
|
2023-01-24 15:54:03 +00:00
|
|
|
if (Hlw.cf1_current_pulse_length && Energy->active_power[0]) { // No current if no power being consumed
|
2021-06-11 17:14:12 +01:00
|
|
|
hlw_i = (Hlw.current_ratio * Settings->energy_current_calibration) / Hlw.cf1_current_pulse_length; // mA
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->current[0] = (float)hlw_i / 1000;
|
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
|
|
|
} else {
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->current[0] = 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
|
|
|
}
|
2018-09-04 15:22:34 +01:00
|
|
|
|
|
|
|
}
|
2019-08-16 13:41:02 +01:00
|
|
|
Hlw.cf1_summed_pulse_length = 0;
|
|
|
|
Hlw.cf1_pulse_counter = 0;
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 16:47:04 +01:00
|
|
|
void HlwEverySecond(void) {
|
2023-01-24 15:54:03 +00:00
|
|
|
if (Energy->data_valid[0] > ENERGY_WATCHDOG) {
|
2019-08-16 13:41:02 +01:00
|
|
|
Hlw.cf1_voltage_pulse_length = 0;
|
|
|
|
Hlw.cf1_current_pulse_length = 0;
|
|
|
|
Hlw.cf_power_pulse_length = 0;
|
2019-05-30 11:45:02 +01:00
|
|
|
} else {
|
2020-10-13 14:55:46 +01:00
|
|
|
uint32_t hlw_len;
|
2019-05-30 11:45:02 +01:00
|
|
|
|
2019-08-16 13:41:02 +01:00
|
|
|
if (Hlw.energy_period_counter) {
|
2022-09-11 13:58:25 +01:00
|
|
|
|
|
|
|
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("HLW: EPC %u, CFlen %d usec"), Hlw.energy_period_counter, Hlw.cf_pulse_length);
|
|
|
|
|
2020-09-09 09:56:18 +01:00
|
|
|
hlw_len = 10000 * 100 / Hlw.energy_period_counter; // Add *100 to fix rounding on loads at 3.6kW (#9160)
|
2019-08-16 13:41:02 +01:00
|
|
|
Hlw.energy_period_counter = 0;
|
2019-05-30 11:45:02 +01:00
|
|
|
if (hlw_len) {
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->kWhtoday_delta[0] += (((Hlw.power_ratio * Settings->energy_power_calibration) / 36) * 100) / hlw_len;
|
2019-05-30 11:45:02 +01:00
|
|
|
EnergyUpdateToday();
|
|
|
|
}
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 16:47:04 +01:00
|
|
|
void HlwSnsInit(void) {
|
2021-06-11 17:14:12 +01:00
|
|
|
if (!Settings->energy_power_calibration || (4975 == Settings->energy_power_calibration)) {
|
|
|
|
Settings->energy_power_calibration = HLW_PREF_PULSE;
|
|
|
|
Settings->energy_voltage_calibration = HLW_UREF_PULSE;
|
|
|
|
Settings->energy_current_calibration = HLW_IREF_PULSE;
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
|
|
|
|
2019-08-16 13:41:02 +01:00
|
|
|
if (Hlw.model_type) {
|
|
|
|
Hlw.power_ratio = HJL_PREF;
|
|
|
|
Hlw.voltage_ratio = HJL_UREF;
|
|
|
|
Hlw.current_ratio = HJL_IREF;
|
2018-09-04 15:22:34 +01:00
|
|
|
} else {
|
2019-08-16 13:41:02 +01:00
|
|
|
Hlw.power_ratio = HLW_PREF;
|
|
|
|
Hlw.voltage_ratio = HLW_UREF;
|
|
|
|
Hlw.current_ratio = HLW_IREF;
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
|
|
|
|
2020-04-27 11:54:07 +01:00
|
|
|
if (PinUsed(GPIO_NRG_SEL)) {
|
2020-04-27 10:54:23 +01:00
|
|
|
pinMode(Pin(GPIO_NRG_SEL), OUTPUT);
|
|
|
|
digitalWrite(Pin(GPIO_NRG_SEL), Hlw.select_ui_flag);
|
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
|
|
|
}
|
2020-04-27 11:54:07 +01:00
|
|
|
if (PinUsed(GPIO_NRG_CF1)) {
|
2020-04-27 10:54:23 +01:00
|
|
|
pinMode(Pin(GPIO_NRG_CF1), INPUT_PULLUP);
|
|
|
|
attachInterrupt(Pin(GPIO_NRG_CF1), HlwCf1Interrupt, FALLING);
|
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
|
|
|
}
|
2020-04-27 10:54:23 +01:00
|
|
|
pinMode(Pin(GPIO_HLW_CF), INPUT_PULLUP);
|
|
|
|
attachInterrupt(Pin(GPIO_HLW_CF), HlwCfInterrupt, FALLING);
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 16:47:04 +01:00
|
|
|
void HlwDrvInit(void) {
|
2019-09-08 15:57:56 +01:00
|
|
|
Hlw.model_type = 0; // HLW8012
|
2020-04-27 11:54:07 +01:00
|
|
|
if (PinUsed(GPIO_HJL_CF)) {
|
2020-06-20 16:58:21 +01:00
|
|
|
SetPin(Pin(GPIO_HJL_CF), AGPIO(GPIO_HLW_CF));
|
2019-09-08 15:57:56 +01:00
|
|
|
Hlw.model_type = 1; // HJL-01/BL0937
|
|
|
|
}
|
2018-10-07 11:37:03 +01:00
|
|
|
|
2020-04-27 16:16:52 +01:00
|
|
|
if (PinUsed(GPIO_HLW_CF)) { // HLW8012 or HJL-01 based device Power monitor
|
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
|
|
|
|
2019-09-08 15:57:56 +01:00
|
|
|
Hlw.ui_flag = true; // Voltage on high
|
2020-04-27 11:54:07 +01:00
|
|
|
if (PinUsed(GPIO_NRG_SEL_INV)) {
|
2020-06-20 16:58:21 +01:00
|
|
|
SetPin(Pin(GPIO_NRG_SEL_INV), AGPIO(GPIO_NRG_SEL));
|
2019-09-08 15:57:56 +01:00
|
|
|
Hlw.ui_flag = false; // Voltage on low
|
|
|
|
}
|
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
|
|
|
|
2020-04-27 16:16:52 +01:00
|
|
|
if (PinUsed(GPIO_NRG_CF1)) { // Voltage and/or Current monitor
|
|
|
|
if (!PinUsed(GPIO_NRG_SEL)) { // Voltage and/or Current selector
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->current_available = false; // Assume Voltage
|
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
|
|
|
}
|
2019-09-08 15:57:56 +01:00
|
|
|
} else {
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->current_available = false;
|
|
|
|
Energy->voltage_available = false;
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->use_overtemp = true; // Use global temperature for overtemp detection
|
2021-04-14 16:47:04 +01:00
|
|
|
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.energy_driver = XNRG_01;
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 16:47:04 +01:00
|
|
|
bool HlwCommand(void) {
|
2019-01-28 13:08:33 +00:00
|
|
|
bool serviced = true;
|
2018-09-04 15:22:34 +01:00
|
|
|
|
2023-01-24 15:54:03 +00:00
|
|
|
if ((CMND_POWERCAL == Energy->command_code) || (CMND_VOLTAGECAL == Energy->command_code) || (CMND_CURRENTCAL == Energy->command_code)) {
|
2019-01-05 14:39:56 +00:00
|
|
|
// Service in xdrv_03_energy.ino
|
|
|
|
}
|
2023-01-24 15:54:03 +00:00
|
|
|
else if (CMND_POWERSET == Energy->command_code) {
|
2019-08-16 13:41:02 +01:00
|
|
|
if (XdrvMailbox.data_len && Hlw.cf_power_pulse_length ) {
|
2022-10-29 18:08:06 +01:00
|
|
|
XdrvMailbox.payload = ((uint32_t)(CharToFloat(XdrvMailbox.data) * 10) * Hlw.cf_power_pulse_length ) / Hlw.power_ratio;
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-24 15:54:03 +00:00
|
|
|
else if (CMND_VOLTAGESET == Energy->command_code) {
|
2019-08-16 13:41:02 +01:00
|
|
|
if (XdrvMailbox.data_len && Hlw.cf1_voltage_pulse_length ) {
|
2022-10-29 18:08:06 +01:00
|
|
|
XdrvMailbox.payload = ((uint32_t)(CharToFloat(XdrvMailbox.data) * 10) * Hlw.cf1_voltage_pulse_length ) / Hlw.voltage_ratio;
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-24 15:54:03 +00:00
|
|
|
else if (CMND_CURRENTSET == Energy->command_code) {
|
2019-08-16 13:41:02 +01:00
|
|
|
if (XdrvMailbox.data_len && Hlw.cf1_current_pulse_length) {
|
2022-10-29 18:08:06 +01:00
|
|
|
XdrvMailbox.payload = ((uint32_t)(CharToFloat(XdrvMailbox.data)) * Hlw.cf1_current_pulse_length) / Hlw.current_ratio;
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else serviced = false; // Unknown command
|
|
|
|
|
|
|
|
return serviced;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2022-11-11 09:44:56 +00:00
|
|
|
bool Xnrg01(uint32_t function) {
|
2019-09-08 15:57:56 +01:00
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_200_MSECOND:
|
|
|
|
HlwEvery200ms();
|
|
|
|
break;
|
|
|
|
case FUNC_ENERGY_EVERY_SECOND:
|
|
|
|
HlwEverySecond();
|
|
|
|
break;
|
|
|
|
case FUNC_COMMAND:
|
|
|
|
result = HlwCommand();
|
|
|
|
break;
|
|
|
|
case FUNC_INIT:
|
|
|
|
HlwSnsInit();
|
|
|
|
break;
|
|
|
|
case FUNC_PRE_INIT:
|
|
|
|
HlwDrvInit();
|
|
|
|
break;
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_HLW8012
|
|
|
|
#endif // USE_ENERGY_SENSOR
|