v5.10.0b - Add PZEM004T support

5.10.0b
 * Change Sonoff Pow Energy MQTT data JSON message and
consolidate Status 8 into Status 10
 * Add optional support for PZEM004T
energy sensor to enable with define USE_PZEM004T in user_config.h (#614)
This commit is contained in:
arendst 2017-12-16 15:51:45 +01:00
parent b07d6220e9
commit f9ce916e0b
18 changed files with 1282 additions and 864 deletions

View File

@ -1,7 +1,7 @@
## Sonoff-Tasmota
Provide ESP8266 based Sonoff by [iTead Studio](https://www.itead.cc/) and ElectroDragon IoT Relay with Serial, Web and MQTT control allowing 'Over the Air' or OTA firmware updates using Arduino IDE.
Current version is **5.10.0a** - See [sonoff/_releasenotes.ino](https://github.com/arendst/Sonoff-Tasmota/blob/development/sonoff/_releasenotes.ino) for change information.
Current version is **5.10.0b** - See [sonoff/_releasenotes.ino](https://github.com/arendst/Sonoff-Tasmota/blob/development/sonoff/_releasenotes.ino) for change information.
### ATTENTION All versions

View File

@ -1,4 +1,8 @@
/* 5.10.0a
/* 5.10.0b
* Change Sonoff Pow Energy MQTT data JSON message and consolidate Status 8 into Status 10
* Add optional support for PZEM004T energy sensor to enable with define USE_PZEM004T in user_config.h (#614)
*
* 5.10.0a
* Add (experimental) support for sensor SHT3x
* Add support for sensor MH-Z19(B) using serial interface to be enabled with define USE_MHZ19_HARD_SERIAL in user_config.h (#561, #1248)
* Add (experimental) support for sensor MH-Z19(B) using SoftwareSerial to be enabled with define USE_MHZ19_SOFT_SERIAL_OBSOLETE in user_config.h (#561, #1248)
@ -12,8 +16,10 @@
* Add support for TSL2561 using adafruit library (#661, #1311)
* Add alternative support for SHT3x enabled with define USE_SHT3X_V2 in user_config.h (#1314)
* Add alternative support for SHT3x enabled with define USE_SHT3X_V3 in user_config.h (#1314)
* Fix PWM initialization in Dimmer/Color mode (#1321)
* Fix BME680 pressure data (#1356)
* Minor webpage HTML optimizations (#1358)
* Change text to Active for 3 minutes (#1364)
*
* 5.10.0 20171201
* Upgrade library ArduinoJson to 5.11.2

View File

@ -233,7 +233,7 @@
#define D_OSWATCH "osWatch"
#define D_BLOCKED_LOOP "Blocked Loop"
#define D_WPS_FAILED_WITH_STATUS "WPS fehlgeschlagen mit Status"
#define D_ACTIVE_FOR_1_MINUTE "aktiv für 1 Minute"
#define D_ACTIVE_FOR_3_MINUTES "aktiv für 3 Minuten"
#define D_FAILED_TO_START "Starten fehlgeschlagen"
#define D_PATCH_ISSUE_2186 "Patch-Problem 2186"
#define D_CONNECTING_TO_AP "verbinden mit AP"
@ -431,7 +431,7 @@
#define D_HUE_POST_ARGS "Hue POST args"
#define D_3_RESPONSE_PACKETS_SENT "3 Antwortpakete gesendet"
// xsns_03_hlw8012.ino
// xsns_03_energy.ino
#define D_MAXPOWERREACHED "MaxPowerReached"
#define D_MAXPOWERREACHEDRETRY "MaxPowerReachedRetry"
#define D_POWERMONITOR "PowerMonitor"
@ -477,6 +477,8 @@
#define D_SENSOR_IRRECV "IRRecv"
#define D_SENSOR_MHZ_RX "MHZ Rx"
#define D_SENSOR_MHZ_TX "MHZ Tx"
#define D_SENSOR_PZEM_RX "PZEM Rx"
#define D_SENSOR_PZEM_TX "PZEM Tx"
#define D_SENSOR_SPI_CS "SPI CS"
#define D_SENSOR_SPI_DC "SPI DC"
#define D_SENSOR_BACKLIGHT "BLight"
@ -678,7 +680,7 @@
#define D_CMND_RFLOW "RfLow"
#define D_CMND_RFSYNC "RfSync"
// Commands xsns_03_hlw8012.ino
// Commands xsns_03_energy.ino
#define D_CMND_POWERLOW "PowerLow"
#define D_CMND_POWERHIGH "PowerHigh"
#define D_CMND_VOLTAGELOW "VoltageLow"

View File

@ -233,7 +233,7 @@
#define D_OSWATCH "osWatch"
#define D_BLOCKED_LOOP "Blocked Loop"
#define D_WPS_FAILED_WITH_STATUS "WPSconfig FAILED with status"
#define D_ACTIVE_FOR_1_MINUTE "active for 1 minute"
#define D_ACTIVE_FOR_3_MINUTES "active for 3 minutes"
#define D_FAILED_TO_START "failed to start"
#define D_PATCH_ISSUE_2186 "Patch issue 2186"
#define D_CONNECTING_TO_AP "Connecting to AP"
@ -431,7 +431,7 @@
#define D_HUE_POST_ARGS "Hue POST args"
#define D_3_RESPONSE_PACKETS_SENT "3 response packets sent"
// xsns_03_hlw8012.ino
// xsns_03_energy.ino
#define D_MAXPOWERREACHED "MaxPowerReached"
#define D_MAXPOWERREACHEDRETRY "MaxPowerReachedRetry"
#define D_POWERMONITOR "PowerMonitor"
@ -477,6 +477,8 @@
#define D_SENSOR_IRRECV "IRrecv"
#define D_SENSOR_MHZ_RX "MHZ Rx"
#define D_SENSOR_MHZ_TX "MHZ Tx"
#define D_SENSOR_PZEM_RX "PZEM Rx"
#define D_SENSOR_PZEM_TX "PZEM Tx"
#define D_SENSOR_SPI_CS "SPI CS"
#define D_SENSOR_SPI_DC "SPI DC"
#define D_SENSOR_BACKLIGHT "BLight"
@ -678,7 +680,7 @@
#define D_CMND_RFLOW "RfLow"
#define D_CMND_RFSYNC "RfSync"
// Commands xsns_03_hlw8012.ino
// Commands xsns_03_energy.ino
#define D_CMND_POWERLOW "PowerLow"
#define D_CMND_POWERHIGH "PowerHigh"
#define D_CMND_VOLTAGELOW "VoltageLow"

View File

@ -233,7 +233,7 @@
#define D_OSWATCH "osWatch"
#define D_BLOCKED_LOOP "Blocked Loop"
#define D_WPS_FAILED_WITH_STATUS "WPSconfig mislukt met status"
#define D_ACTIVE_FOR_1_MINUTE "1 minuut actief"
#define D_ACTIVE_FOR_3_MINUTES "3 minuten actief"
#define D_FAILED_TO_START "mislukt"
#define D_PATCH_ISSUE_2186 "Patch issue 2186"
#define D_CONNECTING_TO_AP "Verbinden met AP"
@ -431,7 +431,7 @@
#define D_HUE_POST_ARGS "Hue POST argumenten"
#define D_3_RESPONSE_PACKETS_SENT "3 antwoord paketten verstuurd"
// xsns_03_hlw8012.ino
// xsns_03_energy.ino
#define D_MAXPOWERREACHED "MaxPowerReached"
#define D_MAXPOWERREACHEDRETRY "MaxPowerReachedRetry"
#define D_POWERMONITOR "PowerMonitor"
@ -476,6 +476,8 @@
#define D_SENSOR_COUNTER "Teller" // Suffix "1"
#define D_SENSOR_MHZ_RX "MHZ Rx"
#define D_SENSOR_MHZ_TX "MHZ Tx"
#define D_SENSOR_PZEM_RX "PZEM Rx"
#define D_SENSOR_PZEM_TX "PZEM Tx"
#define D_SENSOR_IRRECV "IRrecv"
#define D_SENSOR_SPI_CS "SPI CS"
#define D_SENSOR_SPI_DC "SPI DC"
@ -678,7 +680,7 @@
#define D_CMND_RFLOW "RfLow"
#define D_CMND_RFSYNC "RfSync"
// Commands xsns_03_hlw8012.ino
// Commands xsns_03_energy.ino
#define D_CMND_POWERLOW "PowerLow"
#define D_CMND_POWERHIGH "PowerHigh"
#define D_CMND_VOLTAGELOW "VoltageLow"

View File

@ -233,7 +233,7 @@
#define D_OSWATCH "osWatch"
#define D_BLOCKED_LOOP "Petla zablokowana"
#define D_WPS_FAILED_WITH_STATUS "Blad WPSconfig ze statusem"
#define D_ACTIVE_FOR_1_MINUTE "aktywny 1 minute"
#define D_ACTIVE_FOR_3_MINUTES "aktywny 3 minuty"
#define D_FAILED_TO_START "nie udalo sie uruchomic"
#define D_PATCH_ISSUE_2186 "Blad latki 2186"
#define D_CONNECTING_TO_AP "Laczenie z AP"
@ -431,7 +431,7 @@
#define D_HUE_POST_ARGS "Hue POST args"
#define D_3_RESPONSE_PACKETS_SENT "3 pakiety odpowiedzi wysylane"
// xsns_03_hlw8012.ino
// xsns_03_energy.ino
#define D_MAXPOWERREACHED "MaksMocOsiagnieta"
#define D_MAXPOWERREACHEDRETRY "MaksMocOsiagnietaPonowienie"
#define D_POWERMONITOR "MonitorMocy"
@ -477,6 +477,8 @@
#define D_SENSOR_IRRECV "IRrecv"
#define D_SENSOR_MHZ_RX "MHZ Rx"
#define D_SENSOR_MHZ_TX "MHZ Tx"
#define D_SENSOR_PZEM_RX "PZEM Rx"
#define D_SENSOR_PZEM_TX "PZEM Tx"
#define D_SENSOR_SPI_CS "SPI CS"
#define D_SENSOR_SPI_DC "SPI DC"
#define D_SENSOR_BACKLIGHT "BLight"
@ -678,7 +680,7 @@
#define D_CMND_RFLOW "RfLow"
#define D_CMND_RFSYNC "RfSync"
// Commands xsns_03_hlw8012.ino
// Commands xsns_03_energy.ino
#define D_CMND_POWERLOW "PowerLow"
#define D_CMND_POWERHIGH "PowerHigh"
#define D_CMND_VOLTAGELOW "VoltageLow"

View File

@ -158,23 +158,23 @@ struct SYSCFG {
unsigned long hlw_power_calibration; // 364
unsigned long hlw_voltage_calibration; // 368
unsigned long hlw_current_calibration; // 36C
unsigned long hlw_kWhtoday; // 370
unsigned long hlw_kWhyesterday; // 374
uint16_t hlw_kWhdoy; // 378
uint16_t hlw_pmin; // 37A
uint16_t hlw_pmax; // 37C
uint16_t hlw_umin; // 37E
uint16_t hlw_umax; // 380
uint16_t hlw_imin; // 382
uint16_t hlw_imax; // 384
uint16_t hlw_mpl; // 386 MaxPowerLimit
uint16_t hlw_mplh; // 388 MaxPowerLimitHold
uint16_t hlw_mplw; // 38A MaxPowerLimitWindow
uint16_t hlw_mspl; // 38C MaxSafePowerLimit
uint16_t hlw_msplh; // 38E MaxSafePowerLimitHold
uint16_t hlw_msplw; // 390 MaxSafePowerLimitWindow
uint16_t hlw_mkwh; // 392 MaxEnergy
uint16_t hlw_mkwhs; // 394 MaxEnergyStart
unsigned long energy_kWhtoday; // 370
unsigned long energy_kWhyesterday; // 374
uint16_t energy_kWhdoy; // 378
uint16_t energy_min_power; // 37A
uint16_t energy_max_power; // 37C
uint16_t energy_min_voltage; // 37E
uint16_t energy_max_voltage; // 380
uint16_t energy_min_current; // 382
uint16_t energy_max_current; // 384
uint16_t energy_max_power_limit; // 386 MaxPowerLimit
uint16_t energy_max_power_limit_hold; // 388 MaxPowerLimitHold
uint16_t energy_max_power_limit_window; // 38A MaxPowerLimitWindow
uint16_t energy_max_power_safe_limit; // 38C MaxSafePowerLimit
uint16_t energy_max_power_safe_limit_hold; // 38E MaxSafePowerLimitHold
uint16_t energy_max_power_safe_limit_window; // 390 MaxSafePowerLimitWindow
uint16_t energy_max_energy; // 392 MaxEnergy
uint16_t energy_max_energy_start; // 394 MaxEnergyStart
uint16_t mqtt_retry; // 396
uint8_t poweronstate; // 398
uint8_t last_module; // 399
@ -238,7 +238,7 @@ struct SYSCFG {
byte free_542[2]; // 542
uint32_t ip_address[4]; // 544
unsigned long hlw_kWhtotal; // 554
unsigned long energy_kWhtotal; // 554
char mqtt_fulltopic[100]; // 558
SysBitfield2 flag2; // 5BC Add flag2 since 5.9.2
@ -254,8 +254,8 @@ struct RTCMEM {
uint16_t valid; // 000
byte oswatch_blocked_loop; // 002
uint8_t unused; // 003
unsigned long hlw_kWhtoday; // 004
unsigned long hlw_kWhtotal; // 008
unsigned long energy_kWhtoday; // 004
unsigned long energy_kWhtotal; // 008
unsigned long pulse_counter[MAX_COUNTERS]; // 00C
power_t power; // 01C
} RtcSettings;

View File

@ -63,8 +63,8 @@ void RtcSettingsLoad()
if (RtcSettings.valid != RTC_MEM_VALID) {
memset(&RtcSettings, 0, sizeof(RTCMEM));
RtcSettings.valid = RTC_MEM_VALID;
RtcSettings.hlw_kWhtoday = Settings.hlw_kWhtoday;
RtcSettings.hlw_kWhtotal = Settings.hlw_kWhtotal;
RtcSettings.energy_kWhtoday = Settings.energy_kWhtoday;
RtcSettings.energy_kWhtotal = Settings.energy_kWhtotal;
for (byte i = 0; i < MAX_COUNTERS; i++) {
RtcSettings.pulse_counter[i] = Settings.pulse_counter[i];
}
@ -182,10 +182,7 @@ void SettingsSaveAll()
} else {
Settings.power = 0;
}
if (hlw_flg) {
HlwSaveState();
}
CounterSaveState();
XsnsCall(FUNC_XSNS_SAVE_STATE);
SettingsSave(0);
}
@ -454,23 +451,23 @@ void SettingsDefaultSet2()
Settings.hlw_power_calibration = HLW_PREF_PULSE;
Settings.hlw_voltage_calibration = HLW_UREF_PULSE;
Settings.hlw_current_calibration = HLW_IREF_PULSE;
// Settings.hlw_kWhtoday = 0;
// Settings.hlw_kWhyesterday = 0;
// Settings.hlw_kWhdoy = 0;
// Settings.hlw_pmin = 0;
// Settings.hlw_pmax = 0;
// Settings.hlw_umin = 0;
// Settings.hlw_umax = 0;
// Settings.hlw_imin = 0;
// Settings.hlw_imax = 0;
// Settings.hlw_mpl = 0; // MaxPowerLimit
Settings.hlw_mplh = MAX_POWER_HOLD;
Settings.hlw_mplw = MAX_POWER_WINDOW;
// Settings.hlw_mspl = 0; // MaxSafePowerLimit
Settings.hlw_msplh = SAFE_POWER_HOLD;
Settings.hlw_msplw = SAFE_POWER_WINDOW;
// Settings.hlw_mkwh = 0; // MaxEnergy
// Settings.hlw_mkwhs = 0; // MaxEnergyStart
// Settings.energy_kWhtoday = 0;
// Settings.energy_kWhyesterday = 0;
// Settings.energy_kWhdoy = 0;
// Settings.energy_min_power = 0;
// Settings.energy_max_power = 0;
// Settings.energy_min_voltage = 0;
// Settings.energy_max_voltage = 0;
// Settings.energy_min_current = 0;
// Settings.energy_max_current = 0;
// Settings.energy_max_power_limit = 0; // MaxPowerLimit
Settings.energy_max_power_limit_hold = MAX_POWER_HOLD;
Settings.energy_max_power_limit_window = MAX_POWER_WINDOW;
// Settings.energy_max_power_safe_limit = 0; // MaxSafePowerLimit
Settings.energy_max_power_safe_limit_hold = SAFE_POWER_HOLD;
Settings.energy_max_power_safe_limit_window = SAFE_POWER_WINDOW;
// Settings.energy_max_energy = 0; // MaxEnergy
// Settings.energy_max_energy_start = 0; // MaxEnergyStart
SettingsDefaultSet_3_2_4();
@ -501,8 +498,8 @@ void SettingsDefaultSet2()
SettingsDefaultSet_5_0_2();
// 5.0.4
// Settings.hlw_kWhtotal = 0;
RtcSettings.hlw_kWhtotal = 0;
// Settings.energy_kWhtotal = 0;
RtcSettings.energy_kWhtotal = 0;
// 5.0.5
strlcpy(Settings.mqtt_fulltopic, MQTT_FULLTOPIC, sizeof(Settings.mqtt_fulltopic));
@ -712,8 +709,8 @@ void SettingsDelta()
Settings.save_data = SAVE_DATA;
}
if (Settings.version < 0x05000400) {
Settings.hlw_kWhtotal = 0;
RtcSettings.hlw_kWhtotal = 0;
Settings.energy_kWhtotal = 0;
RtcSettings.energy_kWhtotal = 0;
}
if (Settings.version < 0x05000500) {
strlcpy(Settings.mqtt_fulltopic, MQTT_FULLTOPIC, sizeof(Settings.mqtt_fulltopic));

View File

@ -132,7 +132,7 @@ enum LightTypes {LT_BASIC, LT_PWM1, LT_PWM2, LT_PWM3, LT_PWM4, LT_PWM5, LT_PWM6,
enum LichtSubtypes {LST_NONE, LST_SINGLE, LST_COLDWARM, LST_RGB, LST_RGBW, LST_RGBWC};
enum LichtSchemes {LS_POWER, LS_WAKEUP, LS_CYCLEUP, LS_CYCLEDN, LS_RANDOM, LS_MAX};
enum XsnsFunctions {FUNC_XSNS_INIT, FUNC_XSNS_PREP, FUNC_XSNS_JSON_APPEND, FUNC_XSNS_MQTT_SHOW, FUNC_XSNS_WEB};
enum XsnsFunctions {FUNC_XSNS_INIT, FUNC_XSNS_EVERY_SECOND, FUNC_XSNS_PREP, FUNC_XSNS_JSON_APPEND, FUNC_XSNS_WEB, FUNC_XSNS_SAVE_STATE};
const uint8_t kDefaultRfCode[9] PROGMEM = { 0x21, 0x16, 0x01, 0x0E, 0x03, 0x48, 0x2E, 0x1A, 0x00 };

View File

@ -25,8 +25,8 @@
- Select IDE Tools - Flash Size: "1M (no SPIFFS)"
====================================================*/
#define VERSION 0x050A0001
#define VERSION_STRING "5.10.0a" // Would be great to have a macro that fills this from VERSION ...
#define VERSION 0x050A0002
#define VERSION_STRING "5.10.0b" // Would be great to have a macro that fills this from VERSION ...
// Location specific includes
#include "sonoff.h" // Enumaration used in user_config.h
@ -42,7 +42,7 @@
#if (MQTT_MAX_PACKET_SIZE -TOPSZ -7) < MESSZ // If the max message size is too small, throw an error at compile time. See PubSubClient.cpp line 359
#error "MQTT_MAX_PACKET_SIZE is too small in libraries/PubSubClient/src/PubSubClient.h, increase it to at least 512"
#endif
#include <Ticker.h> // RTC, HLW8012, OSWatch
#include <Ticker.h> // RTC, Energy, OSWatch
#include <ESP8266WiFi.h> // MQTT, Ota, WifiManager
#include <ESP8266HTTPClient.h> // MQTT, Ota
#include <ESP8266httpUpdate.h> // Ota
@ -177,7 +177,7 @@ power_t rel_inverted = 0; // Relay inverted flag (1 = (0 = On,
uint8_t led_inverted = 0; // LED inverted flag (1 = (0 = On, 1 = Off))
uint8_t pwm_inverted = 0; // PWM inverted flag (1 = inverted)
uint8_t dht_flg = 0; // DHT configured
uint8_t hlw_flg = 0; // Power monitor configured
uint8_t energy_flg = 1; // Energy monitor configured
uint8_t i2c_flg = 0; // I2C configured
uint8_t spi_flg = 0; // SPI configured
uint8_t light_type = 0; // Light types
@ -189,7 +189,7 @@ boolean (*xsns_func_ptr[XSNS_MAX])(byte); // External Sensor Function Pointers
char my_hostname[33]; // Composed Wifi hostname
char mqtt_client[33]; // Composed MQTT Clientname
char serial_in_buffer[INPUT_BUFFER_SIZE + 2]; // Receive buffer
char mqtt_data[MESSZ]; // MQTT publish buffer
char mqtt_data[TOPSZ + MESSZ]; // MQTT publish buffer
char log_data[TOPSZ + MESSZ]; // Logging
String web_log[MAX_LOG_LINES]; // Web log buffer
String backlog[MAX_BACKLOG]; // Command backlog
@ -325,7 +325,7 @@ void SetDevicePower(power_t rpower)
rpower >>= 1;
}
}
HlwSetPowerSteadyCounter(2);
EnergySetPowerSteadyCounter(2);
}
void SetLedPower(uint8_t state)
@ -1497,7 +1497,7 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
else if (Settings.flag.mqtt_enabled && MqttCommand(grpflg, type, index, dataBuf, data_len, payload, payload16)) {
// Serviced
}
else if (hlw_flg && HlwCommand(type, index, dataBuf, data_len, payload)) {
else if (energy_flg && EnergyCommand(type, index, dataBuf, data_len, payload)) {
// Serviced
}
else if ((SONOFF_BRIDGE == Settings.module) && SonoffBridgeCommand(type, index, dataBuf, data_len, payload)) {
@ -1704,7 +1704,7 @@ void PublishStatus(uint8_t payload)
if ((!Settings.flag.mqtt_enabled) && (6 == payload)) {
payload = 99;
}
if ((!hlw_flg) && ((8 == payload) || (9 == payload))) {
if (!energy_flg && (9 == payload)) {
payload = 99;
}
@ -1757,24 +1757,23 @@ void PublishStatus(uint8_t payload)
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "7"));
}
if (hlw_flg) {
if ((0 == payload) || (8 == payload)) {
HlwMqttStatus();
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "8"));
}
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.hlw_pmin, Settings.hlw_pmax, Settings.hlw_umin, Settings.hlw_umax, Settings.hlw_imin, Settings.hlw_imax);
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"));
}
}
if ((0 == payload) || (10 == payload)) {
if ((0 == payload) || (8 == payload) || (10 == payload)) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS D_STATUS10_SENSOR "\":"));
MqttShowSensor();
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}"), mqtt_data);
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "10"));
if (8 == payload) {
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "8"));
} else {
MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "10"));
}
}
if ((0 == payload) || (11 == payload)) {
@ -1884,14 +1883,10 @@ void PerformEverySecond()
if (MqttShowSensor()) {
MqttPublishPrefixTopic_P(2, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain);
}
XsnsCall(FUNC_XSNS_MQTT_SHOW);
}
}
if (hlw_flg) {
HlwMarginCheck();
}
XsnsCall(FUNC_XSNS_EVERY_SECOND);
if ((2 == RtcTime.minute) && latest_uptime_flag) {
latest_uptime_flag = false;
@ -2627,8 +2622,6 @@ void GpioInit()
}
#endif // USE_IR_RECEIVE
#endif // USE_IR_REMOTE
hlw_flg = ((pin[GPIO_HLW_SEL] < 99) && (pin[GPIO_HLW_CF1] < 99) && (pin[GPIO_HLW_CF] < 99));
}
extern "C" {

View File

@ -58,6 +58,9 @@ void WifiWpsStatusCallback(wps_cb_status status);
#ifdef USE_EMULATION
#undef USE_EMULATION // Disable Wemo or Hue emulation
#endif
#ifdef USE_PZEM004T
#undef USE_PZEM004T // Disable PZEM004T energy sensor
#endif
#ifdef USE_DS18x20
#undef USE_DS18x20 // Disable DS18x20 sensor
#endif

View File

@ -78,8 +78,10 @@ enum UserSelectablePins {
GPIO_LED2_INV,
GPIO_LED3_INV,
GPIO_LED4_INV,
GPIO_MHZ_TXD,
GPIO_MHZ_RXD,
GPIO_MHZ_TXD, // MH-Z19 Serial interface
GPIO_MHZ_RXD, // MH-Z19 Serial interface
GPIO_PZEM_TX, // PZEM004T Serial interface
GPIO_PZEM_RX, // PZEM004T Serial interface
GPIO_SENSOR_END };
// Text in webpage Module Parameters and commands GPIOS and GPIO
@ -141,7 +143,9 @@ const char kSensors[GPIO_SENSOR_END][9] PROGMEM = {
D_SENSOR_LED "3i",
D_SENSOR_LED "4i",
D_SENSOR_MHZ_TX,
D_SENSOR_MHZ_RX
D_SENSOR_MHZ_RX,
D_SENSOR_PZEM_TX,
D_SENSOR_PZEM_RX
};
// Programmer selectable GPIO functionality offset by user selectable GPIOs

View File

@ -434,12 +434,12 @@ void WifiConfig(uint8_t type)
restart_flag = 2;
}
else if (WIFI_SMARTCONFIG == wifi_config_type) {
AddLog_P(LOG_LEVEL_INFO, S_LOG_WIFI, PSTR(D_WCFG_1_SMARTCONFIG D_ACTIVE_FOR_1_MINUTE));
AddLog_P(LOG_LEVEL_INFO, S_LOG_WIFI, PSTR(D_WCFG_1_SMARTCONFIG D_ACTIVE_FOR_3_MINUTES));
WiFi.beginSmartConfig();
}
else if (WIFI_WPSCONFIG == wifi_config_type) {
if (WifiWpsConfigBegin()) {
AddLog_P(LOG_LEVEL_INFO, S_LOG_WIFI, PSTR(D_WCFG_3_WPSCONFIG D_ACTIVE_FOR_1_MINUTE));
AddLog_P(LOG_LEVEL_INFO, S_LOG_WIFI, PSTR(D_WCFG_3_WPSCONFIG D_ACTIVE_FOR_3_MINUTES));
} else {
AddLog_P(LOG_LEVEL_INFO, S_LOG_WIFI, PSTR(D_WCFG_3_WPSCONFIG D_FAILED_TO_START));
wifi_config_counter = 3;
@ -447,7 +447,7 @@ void WifiConfig(uint8_t type)
}
#ifdef USE_WEBSERVER
else if (WIFI_MANAGER == wifi_config_type) {
AddLog_P(LOG_LEVEL_INFO, S_LOG_WIFI, PSTR(D_WCFG_2_WIFIMANAGER D_ACTIVE_FOR_1_MINUTE));
AddLog_P(LOG_LEVEL_INFO, S_LOG_WIFI, PSTR(D_WCFG_2_WIFIMANAGER D_ACTIVE_FOR_3_MINUTES));
WifiManagerBegin();
}
#endif // USE_WEBSERVER

View File

@ -163,6 +163,8 @@
// -- Sensor code selection -----------------------
#define USE_ADC_VCC // Display Vcc in Power status. Disable for use as Analog input on selected devices
//#define USE_PZEM004T // Add support for PZEM004T Energy monitor (+2k3 code)
// WARNING: Select none for default one DS18B20 sensor or enable one of the following two options for multiple sensors
//#define USE_DS18x20 // Optional for more than one DS18x20 sensors with id sort, single scan and read retry (+1k3 code)
//#define USE_DS18x20_LEGACY // Optional for more than one DS18x20 sensors with dynamic scan using library OneWire (+1k5 code)

1171
sonoff/xsns_03_energy.ino Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,767 +0,0 @@
/*
xsns_03_hlw8012.ino - sonoff pow HLW8012 energy sensor support for Sonoff-Tasmota
Copyright (C) 2017 Theo Arends
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/>.
*/
/*********************************************************************************************\
* HLW8012 - Energy
*
* Based on Source: Shenzhen Heli Technology Co., Ltd
\*********************************************************************************************/
#define FEATURE_POWER_LIMIT true
/*********************************************************************************************/
#define HLW_PREF 10000 // 1000.0W
#define HLW_UREF 2200 // 220.0V
#define HLW_IREF 4545 // 4.545A
#define HLW_POWER_PROBE_TIME 10 // Number of seconds to probe for power before deciding none used
enum Hlw8012Commands {
CMND_POWERLOW, CMND_POWERHIGH, CMND_VOLTAGELOW, CMND_VOLTAGEHIGH, CMND_CURRENTLOW, CMND_CURRENTHIGH,
CMND_HLWPCAL, CMND_HLWPSET, CMND_HLWUCAL, CMND_HLWUSET, CMND_HLWICAL, CMND_HLWISET,
CMND_ENERGYRESET, CMND_MAXENERGY, CMND_MAXENERGYSTART,
CMND_MAXPOWER, CMND_MAXPOWERHOLD, CMND_MAXPOWERWINDOW,
CMND_SAFEPOWER, CMND_SAFEPOWERHOLD, CMND_SAFEPOWERWINDOW };
const char kHlw8012Commands[] PROGMEM =
D_CMND_POWERLOW "|" D_CMND_POWERHIGH "|" D_CMND_VOLTAGELOW "|" D_CMND_VOLTAGEHIGH "|" D_CMND_CURRENTLOW "|" D_CMND_CURRENTHIGH "|"
D_CMND_HLWPCAL "|" D_CMND_HLWPSET "|" D_CMND_HLWUCAL "|" D_CMND_HLWUSET "|" D_CMND_HLWICAL "|" D_CMND_HLWISET "|"
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 ;
byte hlw_pmin_flag = 0;
byte hlw_pmax_flag = 0;
byte hlw_umin_flag = 0;
byte hlw_umax_flag = 0;
byte hlw_imin_flag = 0;
byte hlw_imax_flag = 0;
byte power_steady_cntr;
byte hlw_mkwh_state = 0;
#if FEATURE_POWER_LIMIT
byte hlw_mplr_counter = 0;
uint16_t hlw_mplh_counter = 0;
uint16_t hlw_mplw_counter = 0;
#endif // FEATURE_POWER_LIMIT
byte hlw_select_ui_flag;
byte hlw_load_off;
byte hlw_cf1_timer;
byte hlw_fifth_second;
byte hlw_startup;
unsigned long hlw_cf_pulse_length;
unsigned long hlw_cf_pulse_last_time;
unsigned long hlw_cf1_pulse_length;
unsigned long hlw_cf1_pulse_last_time;
unsigned long hlw_cf1_summed_pulse_length;
unsigned long hlw_cf1_pulse_counter;
unsigned long hlw_cf1_voltage_pulse_length;
unsigned long hlw_cf1_current_pulse_length;
unsigned long hlw_energy_counter;
unsigned long hlw_energy_period_counter;
unsigned long hlw_kWhtoday;
uint32_t hlw_lasttime;
unsigned long hlw_cf1_voltage_max_pulse_counter;
unsigned long hlw_cf1_current_max_pulse_counter;
Ticker tickerHLW;
#ifndef USE_WS2812_DMA // Collides with Neopixelbus but solves exception
void HlwCfInterrupt() ICACHE_RAM_ATTR;
void HlwCf1Interrupt() ICACHE_RAM_ATTR;
#endif // USE_WS2812_DMA
void HlwCfInterrupt() // Service Power
{
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++;
hlw_energy_counter++;
}
}
void HlwCf1Interrupt() // Service Voltage and Current
{
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;
hlw_cf1_pulse_counter++;
if (10 == hlw_cf1_pulse_counter) {
hlw_cf1_timer = 8; // We need up to ten samples within 1 second (low current could take up to 0.3 second)
}
}
}
void hlw_200mS()
{
unsigned long hlw_len;
unsigned long hlw_temp;
hlw_fifth_second++;
if (5 == hlw_fifth_second) {
hlw_fifth_second = 0;
if (hlw_energy_period_counter) {
hlw_len = 10000 / hlw_energy_period_counter;
hlw_energy_period_counter = 0;
if (hlw_len) {
hlw_temp = ((HLW_PREF * Settings.hlw_power_calibration) / hlw_len) / 36;
hlw_kWhtoday += hlw_temp;
RtcSettings.hlw_kWhtoday = hlw_kWhtoday;
}
}
if (RtcTime.valid) {
if (LocalTime() == Midnight()) {
Settings.hlw_kWhyesterday = hlw_kWhtoday;
Settings.hlw_kWhtotal += (hlw_kWhtoday / 1000);
RtcSettings.hlw_kWhtotal = Settings.hlw_kWhtotal;
hlw_kWhtoday = 0;
RtcSettings.hlw_kWhtoday = hlw_kWhtoday;
hlw_mkwh_state = 3;
}
if ((RtcTime.hour == Settings.hlw_mkwhs) && (3 == hlw_mkwh_state)) {
hlw_mkwh_state = 0;
}
if (hlw_startup && (RtcTime.day_of_year == Settings.hlw_kWhdoy)) {
hlw_kWhtoday = Settings.hlw_kWhtoday;
RtcSettings.hlw_kWhtoday = hlw_kWhtoday;
hlw_startup = 0;
}
}
}
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 = 1;
}
hlw_cf1_timer++;
if (hlw_cf1_timer >= 8) {
hlw_cf1_timer = 0;
hlw_select_ui_flag = (hlw_select_ui_flag) ? 0 : 1;
digitalWrite(pin[GPIO_HLW_SEL], hlw_select_ui_flag);
if (hlw_cf1_pulse_counter) {
hlw_cf1_pulse_length = hlw_cf1_summed_pulse_length / hlw_cf1_pulse_counter;
} else {
hlw_cf1_pulse_length = 0;
}
if (hlw_select_ui_flag) {
hlw_cf1_voltage_pulse_length = hlw_cf1_pulse_length;
hlw_cf1_voltage_max_pulse_counter = hlw_cf1_pulse_counter;
} else {
hlw_cf1_current_pulse_length = hlw_cf1_pulse_length;
hlw_cf1_current_max_pulse_counter = hlw_cf1_pulse_counter;
}
hlw_cf1_summed_pulse_length = 0;
hlw_cf1_pulse_counter = 0;
}
}
void HlwSaveState()
{
Settings.hlw_kWhdoy = (RtcTime.valid) ? RtcTime.day_of_year : 0;
Settings.hlw_kWhtoday = hlw_kWhtoday;
Settings.hlw_kWhtotal = RtcSettings.hlw_kWhtotal;
}
void HlwReadEnergy(byte option, float &total_energy, float &daily_energy, float &energy, float &watts, float &voltage, float &current, float &power_factor)
{
/* option 0 = do not calculate period energy usage
* option 1 = calculate period energy usage
*/
unsigned long cur_kWhtoday = hlw_kWhtoday;
unsigned long hlw_len;
unsigned long hlw_temp;
unsigned long hlw_w;
unsigned long hlw_u;
unsigned long hlw_i;
uint16_t hlw_period;
//snprintf_P(log_data, sizeof(log_data), PSTR("HLW: CF %d, CF1U %d (%d), CF1I %d (%d)"), hlw_cf_pulse_length, hlw_cf1_voltage_pulse_length, hlw_cf1_voltage_max_pulse_counter, hlw_cf1_current_pulse_length, hlw_cf1_current_max_pulse_counter);
//AddLog(LOG_LEVEL_DEBUG);
total_energy = (float)(RtcSettings.hlw_kWhtotal + (cur_kWhtoday / 1000)) / 100000;
daily_energy = 0;
if (cur_kWhtoday) {
daily_energy = (float)cur_kWhtoday / 100000000;
}
energy = 0;
if (option) {
if (!hlw_lasttime) {
hlw_period = Settings.tele_period;
} else {
hlw_period = LocalTime() - hlw_lasttime;
}
hlw_lasttime = LocalTime();
if (hlw_period) {
uint16_t hlw_interval = 3600 / hlw_period;
if (hlw_energy_counter) {
hlw_len = hlw_period * 1000000 / hlw_energy_counter;
if (hlw_interval && hlw_len) {
hlw_energy_counter = 0;
hlw_temp = ((HLW_PREF * Settings.hlw_power_calibration) / hlw_len) / hlw_interval;
energy = (float)hlw_temp / 10;
}
}
}
}
watts = 0;
if (hlw_cf_pulse_length && (power &1) && !hlw_load_off) {
hlw_w = (HLW_PREF * Settings.hlw_power_calibration) / hlw_cf_pulse_length;
watts = (float)hlw_w / 10;
}
voltage = 0;
if (hlw_cf1_voltage_pulse_length && (power &1)) { // If powered on always provide voltage
hlw_u = (HLW_UREF * Settings.hlw_voltage_calibration) / hlw_cf1_voltage_pulse_length;
voltage = (float)hlw_u / 10;
}
current = 0;
if (hlw_cf1_current_pulse_length && watts) { // No current if no power being consumed
hlw_i = (HLW_IREF * Settings.hlw_current_calibration) / hlw_cf1_current_pulse_length;
current = (float)hlw_i / 1000;
}
power_factor = 0;
if (hlw_i && hlw_u && hlw_w && watts) {
hlw_temp = (hlw_w * 100) / ((hlw_u * hlw_i) / 1000);
if (hlw_temp > 100) {
hlw_temp = 100;
}
power_factor = (float)hlw_temp / 100;
}
}
void HlwInit()
{
if (!Settings.hlw_power_calibration || (4975 == Settings.hlw_power_calibration)) {
Settings.hlw_power_calibration = HLW_PREF_PULSE;
Settings.hlw_voltage_calibration = HLW_UREF_PULSE;
Settings.hlw_current_calibration = HLW_IREF_PULSE;
}
hlw_cf_pulse_length = 0;
hlw_cf_pulse_last_time = 0;
hlw_cf1_pulse_length = 0;
hlw_cf1_pulse_last_time = 0;
hlw_cf1_voltage_pulse_length = 0;
hlw_cf1_current_pulse_length = 0;
hlw_cf1_voltage_max_pulse_counter = 0;
hlw_cf1_current_max_pulse_counter = 0;
hlw_load_off = 1;
hlw_energy_counter = 0;
hlw_energy_period_counter = 0;
hlw_kWhtoday = (RtcSettingsValid()) ? RtcSettings.hlw_kWhtoday : 0;
hlw_select_ui_flag = 0; // Voltage;
pinMode(pin[GPIO_HLW_SEL], OUTPUT);
digitalWrite(pin[GPIO_HLW_SEL], hlw_select_ui_flag);
pinMode(pin[GPIO_HLW_CF1], INPUT_PULLUP);
attachInterrupt(pin[GPIO_HLW_CF1], HlwCf1Interrupt, FALLING);
pinMode(pin[GPIO_HLW_CF], INPUT_PULLUP);
attachInterrupt(pin[GPIO_HLW_CF], HlwCfInterrupt, FALLING);
hlw_startup = 1;
hlw_lasttime = 0;
hlw_fifth_second = 0;
hlw_cf1_timer = 0;
tickerHLW.attach_ms(200, hlw_200mS);
}
/********************************************************************************************/
boolean HlwMargin(byte type, uint16_t margin, uint16_t value, byte &flag, byte &save_flag)
{
byte change;
if (!margin) {
return false;
}
change = save_flag;
if (type) {
flag = (value > margin);
} else {
flag = (value < margin);
}
save_flag = flag;
return (change != save_flag);
}
void HlwSetPowerSteadyCounter(byte value)
{
power_steady_cntr = 2;
}
void HlwMarginCheck()
{
float total_energy;
float daily_energy;
float energy;
float watts;
float voltage;
float current;
float power_factor;
uint16_t udaily_energy;
uint16_t uwatts;
uint16_t uvoltage;
uint16_t ucurrent;
boolean flag;
boolean jsonflg;
if (power_steady_cntr) {
power_steady_cntr--;
return;
}
HlwReadEnergy(0, total_energy, daily_energy, energy, watts, voltage, current, power_factor);
if (power && (Settings.hlw_pmin || Settings.hlw_pmax || Settings.hlw_umin || Settings.hlw_umax || Settings.hlw_imin || Settings.hlw_imax)) {
uwatts = (uint16_t)(watts);
uvoltage = (uint16_t)(voltage);
ucurrent = (uint16_t)(current * 1000);
// snprintf_P(log_data, sizeof(log_data), PSTR("HLW: W %d, U %d, I %d"), watts, voltage, ucurrent);
// AddLog(LOG_LEVEL_DEBUG);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{"));
jsonflg = 0;
if (HlwMargin(0, Settings.hlw_pmin, uwatts, flag, hlw_pmin_flag)) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%s\"" D_CMND_POWERLOW "\":\"%s\""), mqtt_data, (jsonflg)?",":"", GetStateText(flag));
jsonflg = 1;
}
if (HlwMargin(1, Settings.hlw_pmax, uwatts, flag, hlw_pmax_flag)) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%s\"" D_CMND_POWERHIGH "\":\"%s\""), mqtt_data, (jsonflg)?",":"", GetStateText(flag));
jsonflg = 1;
}
if (HlwMargin(0, Settings.hlw_umin, uvoltage, flag, hlw_umin_flag)) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%s\"" D_CMND_VOLTAGELOW "\":\"%s\""), mqtt_data, (jsonflg)?",":"", GetStateText(flag));
jsonflg = 1;
}
if (HlwMargin(1, Settings.hlw_umax, uvoltage, flag, hlw_umax_flag)) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%s\"" D_CMND_VOLTAGEHIGH "\":\"%s\""), mqtt_data, (jsonflg)?",":"", GetStateText(flag));
jsonflg = 1;
}
if (HlwMargin(0, Settings.hlw_imin, ucurrent, flag, hlw_imin_flag)) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%s\"" D_CMND_CURRENTLOW "\":\"%s\""), mqtt_data, (jsonflg)?",":"", GetStateText(flag));
jsonflg = 1;
}
if (HlwMargin(1, Settings.hlw_imax, ucurrent, flag, hlw_imax_flag)) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%s\"" D_CMND_CURRENTHIGH "\":\"%s\""), mqtt_data, (jsonflg)?",":"", GetStateText(flag));
jsonflg = 1;
}
if (jsonflg) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}"), mqtt_data);
MqttPublishPrefixTopic_P(2, PSTR(D_RSLT_MARGINS));
MqttShowHlw8012(0);
}
}
#if FEATURE_POWER_LIMIT
// Max Power
if (Settings.hlw_mpl) {
if (watts > Settings.hlw_mpl) {
if (!hlw_mplh_counter) {
hlw_mplh_counter = Settings.hlw_mplh;
} else {
hlw_mplh_counter--;
if (!hlw_mplh_counter) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_MAXPOWERREACHED "\":\"%d%s\"}"), uwatts, (Settings.flag.value_units) ? " " D_UNIT_WATT : "");
MqttPublishPrefixTopic_P(1, S_RSLT_WARNING);
MqttShowHlw8012(0);
ExecuteCommandPower(1, 0);
if (!hlw_mplr_counter) {
hlw_mplr_counter = Settings.param[P_MAX_POWER_RETRY] +1;
}
hlw_mplw_counter = Settings.hlw_mplw;
}
}
}
else if (power && (uwatts <= Settings.hlw_mpl)) {
hlw_mplh_counter = 0;
hlw_mplr_counter = 0;
hlw_mplw_counter = 0;
}
if (!power) {
if (hlw_mplw_counter) {
hlw_mplw_counter--;
} else {
if (hlw_mplr_counter) {
hlw_mplr_counter--;
if (hlw_mplr_counter) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_POWERMONITOR "\":\"%s\"}"), GetStateText(1));
MqttPublishPrefixTopic_P(5, PSTR(D_POWERMONITOR));
ExecuteCommandPower(1, 1);
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_MAXPOWERREACHEDRETRY "\":\"%s\"}"), GetStateText(0));
MqttPublishPrefixTopic_P(1, S_RSLT_WARNING);
MqttShowHlw8012(0);
}
}
}
}
}
// Max Energy
if (Settings.hlw_mkwh) {
udaily_energy = (uint16_t)(daily_energy * 1000);
if (!hlw_mkwh_state && (RtcTime.hour == Settings.hlw_mkwhs)) {
hlw_mkwh_state = 1;
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_ENERGYMONITOR "\":\"%s\"}"), GetStateText(1));
MqttPublishPrefixTopic_P(5, PSTR(D_ENERGYMONITOR));
ExecuteCommandPower(1, 1);
}
else if ((1 == hlw_mkwh_state) && (udaily_energy >= Settings.hlw_mkwh)) {
hlw_mkwh_state = 2;
dtostrfd(daily_energy, 3, mqtt_data);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_MAXENERGYREACHED "\":\"%s%s\"}"), mqtt_data, (Settings.flag.value_units) ? " " D_UNIT_KILOWATTHOUR : "");
MqttPublishPrefixTopic_P(1, S_RSLT_WARNING);
MqttShowHlw8012(0);
ExecuteCommandPower(1, 0);
}
}
#endif // FEATURE_POWER_LIMIT
}
/*********************************************************************************************\
* Commands
\*********************************************************************************************/
boolean HlwCommand(char *type, uint16_t index, char *dataBuf, uint16_t data_len, int16_t payload)
{
char command [CMDSZ];
char sunit[CMDSZ];
boolean serviced = true;
uint8_t status_flag = 0;
uint8_t unit = 0;
unsigned long nvalue = 0;
int command_code = GetCommandCode(command, sizeof(command), type, kHlw8012Commands);
if (CMND_POWERLOW == command_code) {
if ((payload >= 0) && (payload < 3601)) {
Settings.hlw_pmin = payload;
}
nvalue = Settings.hlw_pmin;
unit = UNIT_WATT;
}
else if (CMND_POWERHIGH == command_code) {
if ((payload >= 0) && (payload < 3601)) {
Settings.hlw_pmax = payload;
}
nvalue = Settings.hlw_pmax;
unit = UNIT_WATT;
}
else if (CMND_VOLTAGELOW == command_code) {
if ((payload >= 0) && (payload < 501)) {
Settings.hlw_umin = payload;
}
nvalue = Settings.hlw_umin;
unit = UNIT_VOLT;
}
else if (CMND_VOLTAGEHIGH == command_code) {
if ((payload >= 0) && (payload < 501)) {
Settings.hlw_umax = payload;
}
nvalue = Settings.hlw_umax;
unit = UNIT_VOLT;
}
else if (CMND_CURRENTLOW == command_code) {
if ((payload >= 0) && (payload < 16001)) {
Settings.hlw_imin = payload;
}
nvalue = Settings.hlw_imin;
unit = UNIT_MILLIAMPERE;
}
else if (CMND_CURRENTHIGH == command_code) {
if ((payload >= 0) && (payload < 16001)) {
Settings.hlw_imax = payload;
}
nvalue = Settings.hlw_imax;
unit = UNIT_MILLIAMPERE;
}
else if ((CMND_ENERGYRESET == command_code) && (index > 0) && (index <= 3)) {
char *p;
unsigned long lnum = strtoul(dataBuf, &p, 10);
if (p != dataBuf) {
switch (index) {
case 1:
hlw_kWhtoday = lnum *100000;
RtcSettings.hlw_kWhtoday = hlw_kWhtoday;
Settings.hlw_kWhtoday = hlw_kWhtoday;
break;
case 2:
Settings.hlw_kWhyesterday = lnum *100000;
break;
case 3:
RtcSettings.hlw_kWhtotal = lnum *100;
Settings.hlw_kWhtotal = RtcSettings.hlw_kWhtotal;
break;
}
}
char syesterday_energy[10];
char stoday_energy[10];
char stotal_energy[10];
dtostrfd((float)Settings.hlw_kWhyesterday / 100000000, Settings.flag2.energy_resolution, syesterday_energy);
dtostrfd((float)RtcSettings.hlw_kWhtoday / 100000000, Settings.flag2.energy_resolution, stoday_energy);
dtostrfd((float)(RtcSettings.hlw_kWhtotal + (hlw_kWhtoday / 1000)) / 100000, Settings.flag2.energy_resolution, stotal_energy);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":{\"" D_TOTAL "\":%s,\"" D_YESTERDAY "\":%s,\"" D_TODAY "\":%s}}"),
command, stotal_energy, syesterday_energy, stoday_energy);
status_flag = 1;
}
else if (CMND_HLWPCAL == command_code) {
if ((payload > 0) && (payload < 32001)) {
Settings.hlw_power_calibration = (payload > 4000) ? payload : HLW_PREF_PULSE; // 12530
}
nvalue = Settings.hlw_power_calibration;
unit = UNIT_MICROSECOND;
}
else if (CMND_HLWPSET == command_code) {
if ((payload > 0) && (payload < 3601) && hlw_cf_pulse_length) {
Settings.hlw_power_calibration = (payload * 10 * hlw_cf_pulse_length) / HLW_PREF;
}
snprintf_P(command, sizeof(command), PSTR(D_CMND_HLWPCAL));
nvalue = Settings.hlw_power_calibration;
unit = UNIT_MICROSECOND;
}
else if (CMND_HLWUCAL == command_code) {
if ((payload > 0) && (payload < 32001)) {
Settings.hlw_voltage_calibration = (payload > 999) ? payload : HLW_UREF_PULSE; // 1950
}
nvalue = Settings.hlw_voltage_calibration;
unit = UNIT_MICROSECOND;
}
else if (CMND_HLWUSET == command_code) {
if ((payload > 0) && (payload < 501) && hlw_cf1_voltage_pulse_length) {
Settings.hlw_voltage_calibration = (payload * 10 * hlw_cf1_voltage_pulse_length) / HLW_UREF;
}
snprintf_P(command, sizeof(command), PSTR(D_CMND_HLWUCAL));
nvalue = Settings.hlw_voltage_calibration;
unit = UNIT_MICROSECOND;
}
else if (CMND_HLWICAL == command_code) {
if ((payload > 0) && (payload < 32001)) {
Settings.hlw_current_calibration = (payload > 1100) ? payload : HLW_IREF_PULSE; // 3500
}
nvalue = Settings.hlw_current_calibration;
unit = UNIT_MICROSECOND;
}
else if (CMND_HLWISET == command_code) {
if ((payload > 0) && (payload < 16001) && hlw_cf1_current_pulse_length) {
Settings.hlw_current_calibration = (payload * hlw_cf1_current_pulse_length) / HLW_IREF;
}
snprintf_P(command, sizeof(command), PSTR(D_CMND_HLWICAL));
nvalue = Settings.hlw_current_calibration;
unit = UNIT_MICROSECOND;
}
#if FEATURE_POWER_LIMIT
else if (CMND_MAXPOWER == command_code) {
if ((payload >= 0) && (payload < 3601)) {
Settings.hlw_mpl = payload;
}
nvalue = Settings.hlw_mpl;
unit = UNIT_WATT;
}
else if (CMND_MAXPOWERHOLD == command_code) {
if ((payload >= 0) && (payload < 3601)) {
Settings.hlw_mplh = (1 == payload) ? MAX_POWER_HOLD : payload;
}
nvalue = Settings.hlw_mplh;
unit = UNIT_SECOND;
}
else if (CMND_MAXPOWERWINDOW == command_code) {
if ((payload >= 0) && (payload < 3601)) {
Settings.hlw_mplw = (1 == payload) ? MAX_POWER_WINDOW : payload;
}
nvalue = Settings.hlw_mplw;
unit = UNIT_SECOND;
}
else if (CMND_SAFEPOWER == command_code) {
if ((payload >= 0) && (payload < 3601)) {
Settings.hlw_mspl = payload;
}
nvalue = Settings.hlw_mspl;
unit = UNIT_WATT;
}
else if (CMND_SAFEPOWERHOLD == command_code) {
if ((payload >= 0) && (payload < 3601)) {
Settings.hlw_msplh = (1 == payload) ? SAFE_POWER_HOLD : payload;
}
nvalue = Settings.hlw_msplh;
unit = UNIT_SECOND;
}
else if (CMND_SAFEPOWERWINDOW == command_code) {
if ((payload >= 0) && (payload < 1440)) {
Settings.hlw_msplw = (1 == payload) ? SAFE_POWER_WINDOW : payload;
}
nvalue = Settings.hlw_msplw;
unit = UNIT_MINUTE;
}
else if (CMND_MAXENERGY == command_code) {
if ((payload >= 0) && (payload < 3601)) {
Settings.hlw_mkwh = payload;
hlw_mkwh_state = 3;
}
nvalue = Settings.hlw_mkwh;
unit = UNIT_WATTHOUR;
}
else if (CMND_MAXENERGYSTART == command_code) {
if ((payload >= 0) && (payload < 24)) {
Settings.hlw_mkwhs = payload;
}
nvalue = Settings.hlw_mkwhs;
unit = UNIT_HOUR;
}
#endif // FEATURE_POWER_LIMIT
else {
serviced = false;
}
if (!status_flag) {
if (Settings.flag.value_units) {
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE_SPACE_UNIT, command, nvalue, GetTextIndexed(sunit, sizeof(sunit), unit, kUnitNames));
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, nvalue);
}
}
return serviced;
}
/********************************************************************************************/
#ifdef USE_WEBSERVER
const char HTTP_ENERGY_SNS[] PROGMEM =
"{s}" D_VOLTAGE "{m}%s " D_UNIT_VOLT "{e}"
"{s}" D_CURRENT "{m}%s " D_UNIT_AMPERE "{e}"
"{s}" D_POWERUSAGE "{m}%s " D_UNIT_WATT "{e}"
"{s}" D_POWER_FACTOR "{m}%s{e}"
"{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
void HlwShow(boolean json, boolean option)
{
/* option 0 = do not show period energy usage
* option 1 = show period energy usage
*/
float total_energy;
float daily_energy;
float energy;
float watts;
float voltage;
float current;
float power_factor;
char stotal_energy[10];
char sdaily_energy[10];
char senergy[10];
char swatts[10];
char svoltage[10];
char scurrent[10];
char spower_factor[10];
char syesterday_energy[10];
char speriod[20];
HlwReadEnergy(option, total_energy, daily_energy, energy, watts, voltage, current, power_factor);
dtostrfd(total_energy, Settings.flag2.energy_resolution, stotal_energy);
dtostrfd(daily_energy, Settings.flag2.energy_resolution, sdaily_energy);
dtostrfd(energy, Settings.flag2.wattage_resolution, senergy);
dtostrfd(watts, Settings.flag2.wattage_resolution, swatts);
dtostrfd(voltage, Settings.flag2.voltage_resolution, svoltage);
dtostrfd(current, Settings.flag2.current_resolution, scurrent);
dtostrfd(power_factor, 2, spower_factor);
dtostrfd((float)Settings.hlw_kWhyesterday / 100000000, Settings.flag2.energy_resolution, syesterday_energy);
if (json) {
snprintf_P(speriod, sizeof(speriod), PSTR(",\"" D_PERIOD "\":%s"), senergy);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s\"" D_TOTAL "\":%s,\"" D_YESTERDAY "\":%s,\"" D_TODAY "\":%s%s,\"" D_POWERUSAGE "\":%s,\"" D_POWERFACTOR "\":%s,\"" D_VOLTAGE "\":%s,\"" D_CURRENT "\":%s}"),
mqtt_data, stotal_energy, syesterday_energy, sdaily_energy, (option) ? speriod : "", swatts, spower_factor, svoltage, scurrent);
#ifdef USE_DOMOTICZ
if (option) { // Only send if telemetry
dtostrfd(total_energy * 1000, 1, stotal_energy);
DomoticzSensorPowerEnergy((uint16_t)watts, stotal_energy); // PowerUsage, EnergyToday
DomoticzSensor(DZ_VOLTAGE, svoltage); // Voltage
DomoticzSensor(DZ_CURRENT, scurrent); // Current
}
#endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_ENERGY_SNS, svoltage, scurrent, swatts, spower_factor, sdaily_energy, syesterday_energy, stotal_energy);
#endif // USE_WEBSERVER
}
}
void MqttShowHlw8012(byte option)
{
/* option 0 = do not show period energy usage
* option 1 = show period energy usage
*/
// {"Time":"2017-03-04T13:37:24", "Total":0.013, "Yesterday":0.013, "Today":0.000, "Period":0, "Power":0, "Factor":0.00, "Voltage":0, "Current":0.000}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_TIME "\":\"%s\","), GetDateAndTime().c_str());
HlwShow(1, option);
MqttPublishPrefixTopic_P(2, PSTR(D_RSLT_ENERGY), Settings.flag.mqtt_sensor_retain);
}
void HlwMqttStatus()
{
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS D_STATUS8_POWER "\":{"));
HlwShow(1, 0);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}"), mqtt_data);
}
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
#define XSNS_03
boolean Xsns03(byte function)
{
boolean result = false;
if (hlw_flg) {
switch (function) {
case FUNC_XSNS_INIT:
HlwInit();
break;
// case FUNC_XSNS_PREP:
// break;
// case FUNC_XSNS_JSON_APPEND:
// break;
case FUNC_XSNS_MQTT_SHOW:
MqttShowHlw8012(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
HlwShow(0, 0);
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -77,7 +77,7 @@
#define INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS (0x0007)
#define INA219_REG_SHUNTVOLTAGE (0x01)
#define INA219_REG_BUSVOLTAGE (0x02)
#define INA219_REG_BUenergy_voltage_chr (0x02)
#define INA219_REG_POWER (0x03)
#define INA219_REG_CURRENT (0x04)
#define INA219_REG_CALIBRATION (0x05)
@ -129,11 +129,11 @@ float Ina219GetShuntVoltage_mV()
return value * 0.01;
}
float Ina219GetBusVoltage_V()
float Ina219GetBuenergy_voltage_chr_V()
{
// Shift to the right 3 to drop CNVR and OVF and multiply by LSB
// raw bus voltage (16-bit signed integer, so +-32767)
int16_t value = (int16_t)(((uint16_t)I2cReadS16(ina219_address, INA219_REG_BUSVOLTAGE) >> 3) * 4);
int16_t value = (int16_t)(((uint16_t)I2cReadS16(ina219_address, INA219_REG_BUenergy_voltage_chr) >> 3) * 4);
// bus voltage in volts
return value * 0.001;
}
@ -185,7 +185,7 @@ void Ina219Show(boolean json)
char current[10];
char power[10];
float fvoltage = Ina219GetBusVoltage_V() + (Ina219GetShuntVoltage_mV() / 1000);
float fvoltage = Ina219GetBuenergy_voltage_chr_V() + (Ina219GetShuntVoltage_mV() / 1000);
float fcurrent = Ina219GetCurrent_mA() / 1000;
float fpower = fvoltage * fcurrent;
dtostrfd(fvoltage, Settings.flag2.voltage_resolution, voltage);

View File

@ -117,10 +117,11 @@ boolean XsnsCall(byte Function)
switch (Function) {
case FUNC_XSNS_INIT:
case FUNC_XSNS_EVERY_SECOND:
case FUNC_XSNS_PREP:
case FUNC_XSNS_JSON_APPEND:
case FUNC_XSNS_MQTT_SHOW:
case FUNC_XSNS_WEB:
case FUNC_XSNS_SAVE_STATE:
for (byte x = 0; x < xsns_present; x++) {
xsns_func_ptr[x](Function);
}