2018-09-04 15:22:34 +01:00
|
|
|
/*
|
|
|
|
xnrg_01_hlw8012.ino - HLW8012 (Sonoff Pow) energy sensor support for Sonoff-Tasmota
|
|
|
|
|
2019-01-01 12:55:01 +00:00
|
|
|
Copyright (C) 2019 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
|
|
|
|
#ifdef HLW_DEBUG
|
|
|
|
unsigned long hlw_debug[HLW_SAMPLE_COUNT];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
unsigned long hlw_cf_pulse_length = 0;
|
|
|
|
unsigned long hlw_cf_pulse_last_time = 0;
|
|
|
|
unsigned long hlw_cf_power_pulse_length = 0;
|
2019-01-05 17:40:33 +00:00
|
|
|
|
2019-01-06 14:17:22 +00:00
|
|
|
unsigned long hlw_cf1_pulse_length = 0;
|
|
|
|
unsigned long hlw_cf1_pulse_last_time = 0;
|
|
|
|
unsigned long hlw_cf1_summed_pulse_length = 0;
|
|
|
|
unsigned long hlw_cf1_pulse_counter = 0;
|
|
|
|
unsigned long hlw_cf1_voltage_pulse_length = 0;
|
|
|
|
unsigned long hlw_cf1_current_pulse_length = 0;
|
2019-01-05 17:40:33 +00:00
|
|
|
|
2019-01-06 14:17:22 +00:00
|
|
|
unsigned long hlw_energy_period_counter = 0;
|
2018-09-07 17:15:47 +01:00
|
|
|
|
|
|
|
unsigned long hlw_power_ratio = 0;
|
|
|
|
unsigned long hlw_voltage_ratio = 0;
|
|
|
|
unsigned long hlw_current_ratio = 0;
|
|
|
|
|
2019-01-06 14:17:22 +00:00
|
|
|
byte hlw_select_ui_flag = 0;
|
|
|
|
byte hlw_ui_flag = 1;
|
|
|
|
byte hlw_model_type = 0;
|
|
|
|
byte hlw_load_off = 1;
|
|
|
|
byte hlw_cf1_timer = 0;
|
2019-01-05 17:40:33 +00:00
|
|
|
|
2018-09-04 15:22:34 +01:00
|
|
|
#ifndef USE_WS2812_DMA // Collides with Neopixelbus but solves exception
|
2018-11-14 13:32:09 +00:00
|
|
|
void HlwCfInterrupt(void) ICACHE_RAM_ATTR;
|
|
|
|
void HlwCf1Interrupt(void) ICACHE_RAM_ATTR;
|
2018-09-04 15:22:34 +01:00
|
|
|
#endif // USE_WS2812_DMA
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void HlwCfInterrupt(void) // Service Power
|
2018-09-04 15:22:34 +01:00
|
|
|
{
|
|
|
|
unsigned long us = micros();
|
|
|
|
|
|
|
|
if (hlw_load_off) { // Restart plen measurement
|
|
|
|
hlw_cf_pulse_last_time = us;
|
|
|
|
hlw_load_off = 0;
|
|
|
|
} else {
|
|
|
|
hlw_cf_pulse_length = us - hlw_cf_pulse_last_time;
|
|
|
|
hlw_cf_pulse_last_time = us;
|
|
|
|
hlw_energy_period_counter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void HlwCf1Interrupt(void) // Service Voltage and Current
|
2018-09-04 15:22:34 +01:00
|
|
|
{
|
|
|
|
unsigned long us = micros();
|
|
|
|
|
|
|
|
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
|
|
|
|
hlw_debug[hlw_cf1_pulse_counter] = hlw_cf1_pulse_length;
|
|
|
|
#endif
|
2018-09-04 15:22:34 +01:00
|
|
|
hlw_cf1_pulse_counter++;
|
2019-01-06 14:17:22 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void HlwEvery200ms(void)
|
2018-09-04 15:22:34 +01:00
|
|
|
{
|
2019-01-05 17:40:33 +00:00
|
|
|
unsigned long cf1_pulse_length = 0;
|
2018-09-04 15:22:34 +01:00
|
|
|
unsigned long hlw_w = 0;
|
|
|
|
unsigned long hlw_u = 0;
|
|
|
|
unsigned long hlw_i = 0;
|
|
|
|
|
|
|
|
if (micros() - hlw_cf_pulse_last_time > (HLW_POWER_PROBE_TIME * 1000000)) {
|
2019-01-06 11:29:38 +00:00
|
|
|
hlw_cf_pulse_length = 0; // No load for some time
|
2018-09-04 15:22:34 +01:00
|
|
|
hlw_load_off = 1;
|
|
|
|
}
|
2019-01-06 11:29:38 +00:00
|
|
|
hlw_cf_power_pulse_length = hlw_cf_pulse_length;
|
2018-09-04 15:22:34 +01:00
|
|
|
|
2019-01-06 11:29:38 +00:00
|
|
|
if (hlw_cf_power_pulse_length && energy_power_on && !hlw_load_off) {
|
2019-01-06 14:17:22 +00:00
|
|
|
hlw_w = (hlw_power_ratio * Settings.energy_power_calibration) / hlw_cf_power_pulse_length; // W *10
|
2018-09-28 14:48:42 +01:00
|
|
|
energy_active_power = (float)hlw_w / 10;
|
2018-09-04 15:22:34 +01:00
|
|
|
} else {
|
2018-09-28 14:48:42 +01:00
|
|
|
energy_active_power = 0;
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
hlw_cf1_timer++;
|
|
|
|
if (hlw_cf1_timer >= 8) {
|
|
|
|
hlw_cf1_timer = 0;
|
|
|
|
hlw_select_ui_flag = (hlw_select_ui_flag) ? 0 : 1;
|
2018-10-07 11:37:03 +01:00
|
|
|
digitalWrite(pin[GPIO_NRG_SEL], hlw_select_ui_flag);
|
2018-09-04 15:22:34 +01:00
|
|
|
|
|
|
|
if (hlw_cf1_pulse_counter) {
|
2019-01-05 17:40:33 +00:00
|
|
|
cf1_pulse_length = hlw_cf1_summed_pulse_length / hlw_cf1_pulse_counter;
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
2019-01-05 17:40:33 +00:00
|
|
|
|
2019-01-06 14:17:22 +00:00
|
|
|
#ifdef HLW_DEBUG
|
|
|
|
// Debugging for calculating mean and median
|
|
|
|
char stemp[100];
|
|
|
|
stemp[0] = '\0';
|
|
|
|
for (uint8_t i = 0; i < hlw_cf1_pulse_counter; i++) {
|
|
|
|
snprintf_P(stemp, sizeof(stemp), PSTR("%s %d"), stemp, hlw_debug[i]);
|
|
|
|
}
|
|
|
|
for (uint8_t i = 0; i < hlw_cf1_pulse_counter; i++) {
|
|
|
|
for (uint8_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]);
|
|
|
|
}
|
2019-01-05 17:40:33 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-06 14:17:22 +00:00
|
|
|
unsigned long median = hlw_debug[(hlw_cf1_pulse_counter +1) / 2];
|
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR("NRG: power %d, ui %d, cnt %d, smpl%s, sum %d, mean %d, median %d"),
|
|
|
|
hlw_cf_power_pulse_length, hlw_select_ui_flag, hlw_cf1_pulse_counter, stemp, hlw_cf1_summed_pulse_length, cf1_pulse_length, median);
|
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
|
|
|
#endif
|
2019-01-05 17:40:33 +00:00
|
|
|
|
2018-09-04 15:22:34 +01:00
|
|
|
if (hlw_select_ui_flag == hlw_ui_flag) {
|
2019-01-05 17:40:33 +00:00
|
|
|
hlw_cf1_voltage_pulse_length = cf1_pulse_length;
|
2018-09-04 15:22:34 +01:00
|
|
|
|
|
|
|
if (hlw_cf1_voltage_pulse_length && energy_power_on) { // If powered on always provide voltage
|
2019-01-06 14:17:22 +00:00
|
|
|
hlw_u = (hlw_voltage_ratio * Settings.energy_voltage_calibration) / hlw_cf1_voltage_pulse_length; // V *10
|
2018-09-04 15:22:34 +01:00
|
|
|
energy_voltage = (float)hlw_u / 10;
|
|
|
|
} else {
|
|
|
|
energy_voltage = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2019-01-05 17:40:33 +00:00
|
|
|
hlw_cf1_current_pulse_length = cf1_pulse_length;
|
2018-09-04 15:22:34 +01:00
|
|
|
|
2018-09-28 14:48:42 +01:00
|
|
|
if (hlw_cf1_current_pulse_length && energy_active_power) { // No current if no power being consumed
|
2019-01-06 14:17:22 +00:00
|
|
|
hlw_i = (hlw_current_ratio * Settings.energy_current_calibration) / hlw_cf1_current_pulse_length; // mA
|
2018-09-04 15:22:34 +01:00
|
|
|
energy_current = (float)hlw_i / 1000;
|
|
|
|
} else {
|
|
|
|
energy_current = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
hlw_cf1_summed_pulse_length = 0;
|
|
|
|
hlw_cf1_pulse_counter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void HlwEverySecond(void)
|
2018-09-04 15:22:34 +01:00
|
|
|
{
|
|
|
|
unsigned long hlw_len;
|
|
|
|
|
|
|
|
if (hlw_energy_period_counter) {
|
|
|
|
hlw_len = 10000 / hlw_energy_period_counter;
|
|
|
|
hlw_energy_period_counter = 0;
|
|
|
|
if (hlw_len) {
|
|
|
|
energy_kWhtoday_delta += ((hlw_power_ratio * Settings.energy_power_calibration) / hlw_len) / 36;
|
|
|
|
EnergyUpdateToday();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void HlwSnsInit(void)
|
2018-09-04 15:22:34 +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-10-07 11:37:03 +01:00
|
|
|
if (hlw_model_type) {
|
2018-09-04 15:22:34 +01:00
|
|
|
hlw_power_ratio = HJL_PREF;
|
|
|
|
hlw_voltage_ratio = HJL_UREF;
|
|
|
|
hlw_current_ratio = HJL_IREF;
|
|
|
|
} else {
|
|
|
|
hlw_power_ratio = HLW_PREF;
|
|
|
|
hlw_voltage_ratio = HLW_UREF;
|
|
|
|
hlw_current_ratio = HLW_IREF;
|
|
|
|
}
|
|
|
|
|
2018-10-07 11:37:03 +01:00
|
|
|
pinMode(pin[GPIO_NRG_SEL], OUTPUT);
|
|
|
|
digitalWrite(pin[GPIO_NRG_SEL], hlw_select_ui_flag);
|
|
|
|
pinMode(pin[GPIO_NRG_CF1], INPUT_PULLUP);
|
|
|
|
attachInterrupt(pin[GPIO_NRG_CF1], HlwCf1Interrupt, FALLING);
|
2018-09-04 15:22:34 +01:00
|
|
|
pinMode(pin[GPIO_HLW_CF], INPUT_PULLUP);
|
|
|
|
attachInterrupt(pin[GPIO_HLW_CF], HlwCfInterrupt, FALLING);
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void HlwDrvInit(void)
|
2018-09-04 15:22:34 +01:00
|
|
|
{
|
|
|
|
if (!energy_flg) {
|
2019-01-05 17:40:33 +00:00
|
|
|
hlw_model_type = 0; // HLW8012
|
2018-10-07 11:37:03 +01:00
|
|
|
if (pin[GPIO_HJL_CF] < 99) {
|
|
|
|
pin[GPIO_HLW_CF] = pin[GPIO_HJL_CF];
|
|
|
|
pin[GPIO_HJL_CF] = 99;
|
2019-01-05 17:40:33 +00:00
|
|
|
hlw_model_type = 1; // HJL-01/BL0937
|
2018-10-07 11:37:03 +01:00
|
|
|
}
|
|
|
|
|
2019-01-05 17:40:33 +00:00
|
|
|
hlw_ui_flag = 1; // Voltage on high
|
2018-10-07 11:37:03 +01:00
|
|
|
if (pin[GPIO_NRG_SEL_INV] < 99) {
|
|
|
|
pin[GPIO_NRG_SEL] = pin[GPIO_NRG_SEL_INV];
|
|
|
|
pin[GPIO_NRG_SEL_INV] = 99;
|
2019-01-05 17:40:33 +00:00
|
|
|
hlw_ui_flag = 0; // Voltage on low
|
2018-10-07 11:37:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((pin[GPIO_NRG_SEL] < 99) && (pin[GPIO_NRG_CF1] < 99) && (pin[GPIO_HLW_CF] < 99)) { // HLW8012 or HJL-01 based device
|
2018-09-04 15:22:34 +01:00
|
|
|
energy_flg = XNRG_01;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
boolean HlwCommand(void)
|
2018-09-04 15:22:34 +01:00
|
|
|
{
|
|
|
|
boolean serviced = true;
|
|
|
|
|
2019-01-05 14:39:56 +00:00
|
|
|
if ((CMND_POWERCAL == energy_command_code) || (CMND_VOLTAGECAL == energy_command_code) || (CMND_CURRENTCAL == energy_command_code)) {
|
|
|
|
// Service in xdrv_03_energy.ino
|
|
|
|
}
|
|
|
|
else if (CMND_POWERSET == energy_command_code) {
|
2019-01-05 17:40:33 +00:00
|
|
|
if (XdrvMailbox.data_len && hlw_cf_power_pulse_length) {
|
|
|
|
Settings.energy_power_calibration = ((unsigned long)(CharToDouble(XdrvMailbox.data) * 10) * hlw_cf_power_pulse_length) / hlw_power_ratio;
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (CMND_VOLTAGESET == energy_command_code) {
|
2018-09-22 14:09:53 +01:00
|
|
|
if (XdrvMailbox.data_len && hlw_cf1_voltage_pulse_length) {
|
|
|
|
Settings.energy_voltage_calibration = ((unsigned long)(CharToDouble(XdrvMailbox.data) * 10) * hlw_cf1_voltage_pulse_length) / hlw_voltage_ratio;
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (CMND_CURRENTSET == energy_command_code) {
|
2018-09-22 14:09:53 +01:00
|
|
|
if (XdrvMailbox.data_len && hlw_cf1_current_pulse_length) {
|
|
|
|
Settings.energy_current_calibration = ((unsigned long)(CharToDouble(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
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
int Xnrg01(byte function)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
if (FUNC_PRE_INIT == function) {
|
|
|
|
HlwDrvInit();
|
|
|
|
}
|
|
|
|
else if (XNRG_01 == energy_flg) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_INIT:
|
|
|
|
HlwSnsInit();
|
|
|
|
break;
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
HlwEverySecond();
|
|
|
|
break;
|
|
|
|
case FUNC_EVERY_200_MSECOND:
|
|
|
|
HlwEvery200ms();
|
|
|
|
break;
|
|
|
|
case FUNC_COMMAND:
|
|
|
|
result = HlwCommand();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_HLW8012
|
|
|
|
#endif // USE_ENERGY_SENSOR
|