Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Commit

Permalink
Merge pull request #32 from jp112sdl/pow_energycounter
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
jp112sdl authored Jan 4, 2018
2 parents d44613b + d3b988b commit 3f033f5
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 74 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode/settings.json
Iconr
SonoffHMLOX.ino.generic.bin.zip
35 changes: 19 additions & 16 deletions FileHandling.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
#define JSONCONFIG_GW "gw"
#define JSONCONFIG_CCUIP "ccuip"
#define JSONCONFIG_SONOFF "sonoff"
#define JSONCONFIG_LOXUDPPORT "loxudpport"
#define JSONCONFIG_LOXUSERNAME "loxusername"
#define JSONCONFIG_LOXPASSWORD "loxpassword"
#define JSONCONFIG_HMPOWERVARIABLE "powervariable"
#define JSONCONFIG_MEASUREINTERVAL "measureinterval"
#define JSONCONFIG_BACKENDTYPE "backendtype"
#define JSONCONFIG_RESTOREOLDSTATE "restoreOldState"
#define JSONCONFIG_LEDDISABLED "ledDisabled"
#define JSONCONFIG_SONOFFMODEL "sonoffmodel"
#define JSONCONFIG_HLW_CURRENTMULTIPLIER "hlw_currentmultiplier"
#define JSONCONFIG_HLW_VOLTAGEMULTIPLIER "hlw_voltagemultiplier"
#define JSONCONFIG_HLW_POWERMULTIPLIER "hlw_powermultiplier"
#define JSONCFONIG_GPIO14MODE "gpio14mode"
#define JSONCFONIG_GPIO14ASSENDER "gpio14assender"
#define JSONCONFIG_LOXUDPPORT "loxudpport"
#define JSONCONFIG_LOXUSERNAME "loxusername"
#define JSONCONFIG_LOXPASSWORD "loxpassword"
#define JSONCONFIG_HMPOWERVARIABLE "powervariable"
#define JSONCONFIG_HMENERGYCOUNTERVARIABLE "ecvariable"
#define JSONCONFIG_MEASUREINTERVAL "measureinterval"
#define JSONCONFIG_BACKENDTYPE "backendtype"
#define JSONCONFIG_RESTOREOLDSTATE "restoreOldState"
#define JSONCONFIG_LEDDISABLED "ledDisabled"
#define JSONCONFIG_SONOFFMODEL "sonoffmodel"
#define JSONCONFIG_HLW_CURRENTMULTIPLIER "hlw_currentmultiplier"
#define JSONCONFIG_HLW_VOLTAGEMULTIPLIER "hlw_voltagemultiplier"
#define JSONCONFIG_HLW_POWERMULTIPLIER "hlw_powermultiplier"
#define JSONCFONIG_GPIO14MODE "gpio14mode"
#define JSONCFONIG_GPIO14ASSENDER "gpio14assender"

bool loadSystemConfig() {
DEBUG(F("loadSystemConfig mounting FS..."), "loadSystemConfig()", _slInformational);
Expand Down Expand Up @@ -50,6 +51,7 @@ bool loadSystemConfig() {
//((json[JSONCONFIG_LOXPASSWORD]).as<String>()).toCharArray(LoxoneConfig.Password, VARIABLESIZE);
((json[JSONCONFIG_LOXUDPPORT]).as<String>()).toCharArray(LoxoneConfig.UDPPort, 10);
((json[JSONCONFIG_HMPOWERVARIABLE]).as<String>()).toCharArray(HomeMaticConfig.PowerVariableName, VARIABLESIZE);
((json[JSONCONFIG_HMENERGYCOUNTERVARIABLE]).as<String>()).toCharArray(HomeMaticConfig.EnergyCounterVariableName, VARIABLESIZE);
GlobalConfig.MeasureInterval = json[JSONCONFIG_MEASUREINTERVAL];
if (GlobalConfig.MeasureInterval == 0)
GlobalConfig.MeasureInterval = 60;
Expand Down Expand Up @@ -116,6 +118,7 @@ bool saveSystemConfig() {
//json[JSONCONFIG_LOXPASSWORD] = LoxoneConfig.Password;
json[JSONCONFIG_LOXUDPPORT] = LoxoneConfig.UDPPort;
json[JSONCONFIG_HMPOWERVARIABLE] = HomeMaticConfig.PowerVariableName;
json[JSONCONFIG_HMENERGYCOUNTERVARIABLE] = HomeMaticConfig.EnergyCounterVariableName;
json[JSONCONFIG_MEASUREINTERVAL] = GlobalConfig.MeasureInterval;
if (GlobalConfig.MeasureInterval == 0) GlobalConfig.MeasureInterval = 60;
json[JSONCONFIG_SONOFFMODEL] = GlobalConfig.SonoffModel;
Expand Down Expand Up @@ -145,7 +148,7 @@ bool saveSystemConfig() {
return true;
}

void setLastState(bool state) {
void setLastRelayState(bool state) {
GlobalConfig.lastRelayState = state;
if (GlobalConfig.restoreOldRelayState) {
if (SPIFFS.begin()) {
Expand All @@ -162,7 +165,7 @@ void setLastState(bool state) {
}
}

bool getLastState() {
bool getLastRelayState() {
if (GlobalConfig.restoreOldRelayState) {
if (SPIFFS.begin()) {
DEBUG(F("getLastState mounted file system"), "getLastState()", _slInformational);
Expand Down
19 changes: 12 additions & 7 deletions HLW8012.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void hlwundocalibrate() {
saveSystemConfig();
}

void hlwcalibrate(byte exp_voltage, byte exp_power ) {
void hlwcalibrate(int exp_voltage, int exp_power ) {
bool tmpOldState = RELAYSTATE_ON;
if (RelayState == RELAYSTATE_OFF) {
tmpOldState = RELAYSTATE_OFF;
Expand Down Expand Up @@ -90,15 +90,19 @@ void handleHLW8012() {
hlw8012value.powerva = vatemp;
hlw8012value.voltage = vtemp;
hlw8012value.current = ctemp;

if (wtemp > 0)
hlw8012value.energy_counter += (wtemp / 3600) * (((float)HLWCOLLECTINTERVAL / 1000) * hlwvalues.HlwCollectCounter);

hlwvalues.HlwCollectCounter = 0;
}

hlwvalues.ActivePower[hlwvalues.HlwCollectCounter] = hlw8012.getActivePower();
hlwvalues.ApparentPower[hlwvalues.HlwCollectCounter] = hlw8012.getApparentPower();
hlwvalues.Voltage[hlwvalues.HlwCollectCounter] = hlw8012.getVoltage();
hlwvalues.Current[hlwvalues.HlwCollectCounter] = hlw8012.getCurrent();
hlwvalues.HlwCollectCounter++;

} else {
hlw8012value.powerw = 0;
hlw8012value.powerva = 0;
Expand All @@ -109,12 +113,13 @@ void handleHLW8012() {

if (!OTAStart && GlobalConfig.MeasureInterval > 0 && (millis() - LastHlwMeasureMillis) > (GlobalConfig.MeasureInterval * 1000)) {
LastHlwMeasureMillis = millis();
DEBUG("[HLW]: "+String(hlw8012value.powerw)+"W, "+String(hlw8012value.voltage)+"V, "+String(hlw8012value.current)+"A, "+String(hlw8012value.powerva)+"VA, Power Factor (%): "+String((int) (100 * hlw8012.getPowerFactor())));
DEBUG("[HLW]: " + String(hlw8012value.powerw) + "W, " + String(hlw8012value.voltage) + "V, " + String(hlw8012value.current) + "A, " + String(hlw8012value.powerva) + "VA, Power Factor (%): " + String((int) (100 * hlw8012.getPowerFactor())) + ", ENERGY_COUNTER: " + String(hlw8012value.energy_counter) + "Wh");

if (GlobalConfig.BackendType == BackendType_HomeMatic) {
if (String(HomeMaticConfig.PowerVariableName) != "") {
if (String(HomeMaticConfig.PowerVariableName) != "")
setStateCUxD(String(HomeMaticConfig.PowerVariableName), String(hlw8012value.powerw));
}
if (String(HomeMaticConfig.EnergyCounterVariableName) != "")
setStateCUxD(String(HomeMaticConfig.EnergyCounterVariableName), String(hlw8012value.energy_counter));
}
}
}
Expand Down
39 changes: 31 additions & 8 deletions HomeMatic.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
bool setStateCUxD(String id, String value) {
if (id.indexOf(".null.") == -1) {
if (id.indexOf(".null.") == -1 && String(GlobalConfig.ccuIP) != "0.0.0.0") {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.setTimeout(HTTPTimeOut);
Expand All @@ -23,20 +23,22 @@ bool setStateCUxD(String id, String value) {
payload = payload.substring(payload.indexOf("<ret>"));
payload = payload.substring(5, payload.indexOf("</ret>"));


DEBUG("result: " + payload, "setStateCUxD()", (payload != "null") ? _slInformational : _slError);

return (payload != "null");

} else {
if (!doWifiConnect())
ESP.restart();
DEBUG("setStateCUxD: WiFi.status() != WL_CONNECTED, trying to reconnect with doWifiConnect()", "setStateCUxD()", _slError);
/*if (!doWifiConnect()) {
DEBUG("setStateCUxD: doWifiConnect() failed.", "setStateCUxD()", _slError);
//ESP.restart();
}*/
}
} else return true;
}

String getStateCUxD(String id, String type) {
if (id.indexOf(".null.") == -1) {
if (id != "" && id.indexOf(".null.") == -1 && String(GlobalConfig.ccuIP) != "0.0.0.0") {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.setTimeout(HTTPTimeOut);
Expand All @@ -61,9 +63,30 @@ String getStateCUxD(String id, String type) {

return payload;
} else {
if (!doWifiConnect())
ESP.restart();
DEBUG("getStateCUxD: WiFi.status() != WL_CONNECTED, trying to reconnect with doWifiConnect()", "getStateCUxD()", _slError);
/*if (!doWifiConnect()) {
DEBUG("getStateCUxD: doWifiConnect() failed.", "getStateCUxD()", _slError);
//ESP.restart();
}*/
}
} else return "";
} else return "null";
}

String reloadCUxDAddress(bool transmitState) {
String ret = "";
HomeMaticConfig.ChannelName = "CUxD." + getStateCUxD(GlobalConfig.DeviceName, "Address");
ret += "CUxD Address = " + HomeMaticConfig.ChannelName;
DEBUG("HomeMaticConfig.ChannelName = " + HomeMaticConfig.ChannelName);


if (GlobalConfig.SonoffModel == SonoffModel_TouchAsSender || (GlobalConfig.GPIO14Mode != GPIO14Mode_OFF && GlobalConfig.GPIO14asSender)) {
HomeMaticConfig.ChannelNameSender = "CUxD." + getStateCUxD(String(GlobalConfig.DeviceName) + ":1", "Address");
ret += " ; CUxD Address Sender = " + HomeMaticConfig.ChannelNameSender;
DEBUG("HomeMaticConfig.ChannelNameSender = " + HomeMaticConfig.ChannelNameSender);
}

if (transmitState == TRANSMITSTATE)
setStateCUxD(HomeMaticConfig.ChannelName + ".SET_STATE", String(getRelayState()));
return ret;
}

60 changes: 25 additions & 35 deletions SonoffHMLOX.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#include "js_pow.h"
#include "js_fwupd.h"

const String FIRMWARE_VERSION = "1.0.14";
//#define UDPDEBUG
const String FIRMWARE_VERSION = "1.0.15";
#define UDPDEBUG
#define SERIALDEBUG

#define LEDPinSwitch 13
Expand All @@ -34,9 +34,9 @@ const String FIRMWARE_VERSION = "1.0.14";
#define RelayPin 12
#define SwitchPin 0
#define SwitchGPIOPin14 14
#define MillisKeyBounce 150 //Millisekunden zwischen 2xtasten
#define MillisKeyBounce 100 //Millisekunden zwischen 2xtasten
#define ConfigPortalTimeout 180 //Timeout (Sekunden) des AccessPoint-Modus
#define HTTPTimeOut 3000 //Timeout (Millisekunden) für http requests
#define HTTPTimeOut 1500 //Timeout (Millisekunden) für http requests
#define IPSIZE 16
#define VARIABLESIZE 255
#define UDPPORT 6676
Expand Down Expand Up @@ -82,7 +82,7 @@ struct globalconfig_t {
char DeviceName[VARIABLESIZE] = "";
bool restoreOldRelayState = false;
bool lastRelayState = false;
int MeasureInterval = 10;
int MeasureInterval = 10;
byte BackendType = BackendType_HomeMatic;
byte SonoffModel = SonoffModel_Switch;
byte GPIO14Mode = GPIO14Mode_OFF;
Expand All @@ -95,6 +95,7 @@ struct hmconfig_t {
String ChannelName = "";
String ChannelNameSender = "";
char PowerVariableName[VARIABLESIZE] = "";
char EnergyCounterVariableName[VARIABLESIZE] = "";
} HomeMaticConfig;

struct loxoneconfig_t {
Expand Down Expand Up @@ -173,10 +174,11 @@ struct hlwvalues_ {
} hlwvalues;

struct hlw8012value_t {
float voltage = 0;
float current = 0;
float powerw = 0;
float powerva = 0;
float voltage = 0;
float current = 0;
float powerw = 0;
float powerva = 0;
float energy_counter = 0;
} hlw8012value;

struct hlw8012calibrationdata_t {
Expand All @@ -193,7 +195,6 @@ void ICACHE_RAM_ATTR hlw8012_cf1_interrupt() {
void ICACHE_RAM_ATTR hlw8012_cf_interrupt() {
hlw8012.cf_interrupt();
}
//

void setup() {
Serial.begin(115200);
Expand Down Expand Up @@ -259,7 +260,7 @@ void setup() {
break;
case SonoffModel_Pow:
DEBUG("\nSonoff Modell = POW");
LEDPin = 15;
LEDPin = LEDPinPow;
On = HIGH;
Off = LOW;
hlw_init();
Expand Down Expand Up @@ -298,6 +299,10 @@ void setup() {
WebServer.on("/calibrate", calibrateHtml);
WebServer.on("/getPower", replyPower);
WebServer.on("/getPowerJSON", replyPowerJSON);
WebServer.on("/reloadCUxD", []() {
String ret = reloadCUxDAddress(TRANSMITSTATE);
WebServer.send(200, "text/plain", ret);
});
httpUpdater.setup(&WebServer);
WebServer.onNotFound(defaultHtml);

Expand All @@ -307,38 +312,23 @@ void setup() {
DEBUG("Error setting up MDNS responder!");
}

GlobalConfig.lastRelayState = getLastState();

switchLED(GlobalConfig.SonoffModel == SonoffModel_Pow);

if (GlobalConfig.BackendType == BackendType_HomeMatic) {
HomeMaticConfig.ChannelName = "CUxD." + getStateCUxD(GlobalConfig.DeviceName, "Address");
DEBUG("HomeMaticConfig.ChannelName = " + HomeMaticConfig.ChannelName);
if (GlobalConfig.restoreOldRelayState && GlobalConfig.lastRelayState == true) {
switchRelay(RELAYSTATE_ON, NO_TRANSMITSTATE);
} else {
switchRelay(RELAYSTATE_OFF, (getStateCUxD(HomeMaticConfig.ChannelName + ".STATE", "State") == "true"));
}

if (GlobalConfig.SonoffModel == SonoffModel_TouchAsSender || (GlobalConfig.GPIO14Mode != GPIO14Mode_OFF && GlobalConfig.GPIO14asSender)) {
HomeMaticConfig.ChannelNameSender = "CUxD." + getStateCUxD(String(GlobalConfig.DeviceName) + ":1", "Address");
DEBUG("HomeMaticConfig.ChannelNameSender = " + HomeMaticConfig.ChannelNameSender);
}
reloadCUxDAddress(NO_TRANSMITSTATE);
}

if (GlobalConfig.BackendType == BackendType_Loxone) {
if ((GlobalConfig.restoreOldRelayState) && GlobalConfig.lastRelayState == true) {
switchRelay(RELAYSTATE_ON, NO_TRANSMITSTATE);
} else {
switchRelay(RELAYSTATE_OFF, NO_TRANSMITSTATE);
}

GlobalConfig.lastRelayState = getLastRelayState();
if ((GlobalConfig.restoreOldRelayState) && GlobalConfig.lastRelayState == true) {
switchRelay(RELAYSTATE_ON, TRANSMITSTATE);
} else {
switchRelay(RELAYSTATE_OFF, TRANSMITSTATE);
}

startOTAhandling();

DEBUG("Starte UDP-Handler an Port " + String(UDPPORT) + "...");
UDPClient.UDP.begin(UDPPORT);
UDPReady = true;
switchLED(GlobalConfig.SonoffModel == SonoffModel_Pow);
DEBUG(String(GlobalConfig.DeviceName) + " - Boot abgeschlossen, SSID = " + WiFi.SSID() + ", IP = " + String(IpAddress2String(WiFi.localIP())) + ", RSSI = " + WiFi.RSSI() + ", MAC = " + WiFi.macAddress(), "Setup", _slInformational);
}

Expand Down Expand Up @@ -464,7 +454,7 @@ void switchRelay(bool toState, bool transmitState) {
}

digitalWrite(RelayPin, RelayState);
setLastState(RelayState);
setLastRelayState(RelayState);

if (transmitState) {
if (GlobalConfig.BackendType == BackendType_HomeMatic) setStateCUxD(HomeMaticConfig.ChannelName + ".SET_STATE", String(RelayState));
Expand Down
Loading

0 comments on commit 3f033f5

Please sign in to comment.