Browse Source

Merge pull request #10 from DanielR92/patch-4

Update getPowermeterWattsMqtt
pull/1581/head
tictrick 10 months ago
committed by GitHub
parent
commit
7b9d90c92f
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      src/config/settings.h
  2. 37
      src/plugins/zeroExport/powermeter.h
  3. 167
      src/plugins/zeroExport/zeroExport.h
  4. 2
      src/web/html/setup.html

2
src/config/settings.h

@ -216,7 +216,7 @@ typedef struct {
#define ZEROEXPORT_GROUP_WR_LIMIT_MIN_DIFF 5
#define ZEROEXPORT_POWERMETER_SHELLY
//#define ZEROEXPORT_POWERMETER_TASMOTA
//#define ZEROEXPORT_POWERMETER_MQTT
#define ZEROEXPORT_POWERMETER_MQTT
//#define ZEROEXPORT_POWERMETER_HICHI
#define ZEROEXPORT_POWERMETER_TIBBER
#define ZEROEXPORT_POWERMETER_SHRDZM

37
src/plugins/zeroExport/powermeter.h

@ -117,6 +117,16 @@ class powermeter {
}
private:
HTTPClient http;
zeroExport_t *mCfg;
JsonObject *mLog;
unsigned long mPreviousTsp = 0;
PowermeterBuffer_t mPowermeterBuffer[ZEROEXPORT_MAX_GROUPS][5] = {0};
short mPowermeterBufferPos[ZEROEXPORT_MAX_GROUPS] = {0};
// set HTTPClient header
void setHeader(HTTPClient* h) {
@ -142,14 +152,7 @@ class powermeter {
logObj["mod"] = "getPowermeterWattsShelly";
HTTPClient http;
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
http.setUserAgent("Ahoy-Agent");
// TODO: Ahoy-0.8.850024-zero
http.setConnectTimeout(500);
http.setTimeout(1000);
http.addHeader("Content-Type", "application/json");
http.addHeader("Accept", "application/json");
setHeader(&http);
String url = String("http://") + String(mCfg->groups[group].pm_url) + String("/") + String(mCfg->groups[group].pm_jsonPath);
logObj["HTTP_URL"] = url;
@ -340,11 +343,9 @@ class powermeter {
logObj["mod"] = "getPowermeterWattsMqtt";
// Hier neuer Code - Anfang
// TODO: Noch nicht komplett
// Hier neuer Code - Ende
// topic for powermeter?
result.P = mCfg->groups[group].pm_P;
result.P1 = result.P2 = result.P3 = mCfg->groups[group].pm_P / 3;
return result;
}
@ -531,16 +532,6 @@ class powermeter {
mPowermeterBufferPos[group]++;
if (mPowermeterBufferPos[group] >= 5) mPowermeterBufferPos[group] = 0;
}
HTTPClient http;
zeroExport_t *mCfg;
JsonObject *mLog;
unsigned long mPreviousTsp = 0;
PowermeterBuffer_t mPowermeterBuffer[ZEROEXPORT_MAX_GROUPS][5] = {0};
short mPowermeterBufferPos[ZEROEXPORT_MAX_GROUPS] = {0};
};
#endif /*__POWERMETER_H__*/

167
src/plugins/zeroExport/zeroExport.h

@ -255,6 +255,7 @@ class ZeroExport {
/** tickerMidnight
* Time pulse Midnicht
* Reboots Inverter at Midnight to reset YieldDay and clean start environment
* @param void
* @returns void
* @todo Reboot der Inverter um Mitternacht in Ahoy selbst verschieben mit separater Config-Checkbox
@ -263,9 +264,8 @@ class ZeroExport {
void tickMidnight(void) {
if (!mIsInitialized) return;
// Select all Inverter to reboot
for (uint8_t group = 0; group < ZEROEXPORT_MAX_GROUPS; group++) {
for (uint8_t inv = 0; inv < ZEROEXPORT_GROUP_MAX_INVERTERS; inv++) {
for (uint8_t inv = 0; inv < ZEROEXPORT_GROUP_MAX_INVERTERS; inv++) {
mCfg->groups[group].inverters[inv].doReboot = 1;
}
}
@ -546,6 +546,17 @@ class ZeroExport {
}
}
// topic for powermeter?
for (uint8_t group = 0; group < ZEROEXPORT_MAX_GROUPS; group++)
{
if(mCfg->groups[group].pm_type == zeroExportPowermeterType_t::Mqtt)
{
mLog["mqttDevice"] = "topicInverter";
if(!topic.equals(mCfg->groups[group].pm_jsonPath)) return;
mCfg->groups[group].pm_P = (int32_t)obj["val"];
}
}
if (mCfg->debug) mLog["Msg"] = obj;
sendLog();
clearLog();
@ -820,6 +831,14 @@ class ZeroExport {
*doLog = true;
}
}
// Battery
String gr = "zero/state/groups/" + String(group) + "/battery";
mqttObj["enabled"] = cfgGroup->battEnabled;
mqttObj["voltageOn"] = cfgGroup->battVoltageOn;
mqttObj["voltageOff"] = cfgGroup->battVoltageOff;
mqttObj["switch"] = cfgGroup->battSwitch;
mqttPublish(gr.c_str(), mqttDoc.as<std::string>().c_str());
mqttDoc.clear();
} else {
mLog["en"] = false;
@ -851,10 +870,8 @@ class ZeroExport {
mCfg->groups[group].pm_P3 = mPowermeter.getDataAVG(group).P3;
if (
(mCfg->groups[group].pm_P == 0) &&
(mCfg->groups[group].pm_P1 == 0) &&
(mCfg->groups[group].pm_P2 == 0) &&
(mCfg->groups[group].pm_P3 == 0)) {
(mCfg->groups[group].pm_P == 0) && (mCfg->groups[group].pm_P1 == 0) &&
(mCfg->groups[group].pm_P2 == 0) && (mCfg->groups[group].pm_P3 == 0)) {
return false;
}
@ -864,12 +881,15 @@ class ZeroExport {
mLog["P3"] = mCfg->groups[group].pm_P3;
// MQTT - Powermeter
mqttObj["Sum"] = mCfg->groups[group].pm_P;
mqttObj["L1"] = mCfg->groups[group].pm_P1;
mqttObj["L2"] = mCfg->groups[group].pm_P2;
mqttObj["L3"] = mCfg->groups[group].pm_P3;
mMqtt->publish(String("zero/state/groups/" + String(group) + "/powermeter/P").c_str(), mqttDoc.as<std::string>().c_str(), false);
mqttDoc.clear();
if(mMqtt->isConnected())
{
mqttObj["Sum"] = mCfg->groups[group].pm_P;
mqttObj["L1"] = mCfg->groups[group].pm_P1;
mqttObj["L2"] = mCfg->groups[group].pm_P2;
mqttObj["L3"] = mCfg->groups[group].pm_P3;
mMqtt->publish(String("zero/state/groups/" + String(group) + "/powermeter/P").c_str(), mqttDoc.as<std::string>().c_str(), false);
mqttDoc.clear();
}
// if (cfgGroup->pm_Publish_W) {
// cfgGroup->pm_Publish_W = false;
@ -1026,6 +1046,18 @@ class ZeroExport {
return false;
}
// Advanced
String gr = "zero/state/groups/" + String(group) + "/advanced";
mqttObj["setPoint"] = cfgGroup->setPoint;
mqttObj["refresh"] = cfgGroup->refresh;
mqttObj["powerTolerance"] = cfgGroup->powerTolerance;
mqttObj["powerMax"] = cfgGroup->powerMax;
mqttObj["Kp"] = cfgGroup->Kp;
mqttObj["Ki"] = cfgGroup->Ki;
mqttObj["Kd"] = cfgGroup->Kd;
mqttPublish(gr.c_str(), mqttDoc.as<std::string>().c_str());
mqttDoc.clear();
return true;
}
@ -1527,19 +1559,39 @@ class ZeroExport {
obj["cmd"] = "limit_nonpersistent_absolute";
mApi->ctrlRequest(obj);
// publish to mqtt when mqtt
if(mMqtt->isConnected())
{
String gr = "zero/state/groups/" + String(group) + "/inverters/" + String(inv);
mqttObj["enabled"] = cfgGroupInv->enabled;
mqttObj["id"] = cfgGroupInv->id;
mqttObj["target"] = cfgGroupInv->target;
mqttObj["powerMin"] = cfgGroupInv->powerMin;
mqttObj["powerMax"] = cfgGroupInv->powerMax;
mMqtt->publish(gr.c_str(), mqttDoc.as<std::string>().c_str(), false);
mqttDoc.clear();
}
if (mCfg->debug) logObj["d"] = obj;
}
return result;
}
/**
*
*/
void PubSubInit(String gr, String payload) {
mMqtt->publish(gr.c_str(), payload.c_str(), false);
/* mqttSubscribe
* when a MQTT Msg is needed to subscribe, then a publish is leading
*/
void mqttSubscribe(String gr, String payload)
{
mqttPublish(gr, payload);
mMqtt->subscribe(gr.c_str(), QOS_2);
}
/* PubInit
* when a MQTT Msg is needed to Publish, but not to subscribe.
*/
void mqttPublish(String gr, String payload, bool retain = false)
{
mMqtt->publish(gr.c_str(), payload.c_str(), retain);
}
/**
*
@ -1551,40 +1603,45 @@ class ZeroExport {
mIsSubscribed = true;
// Global (zeroExport)
mMqtt->publish("zero/set/enabled", ((mCfg->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]), false);
mMqtt->subscribe("zero/set/enabled", QOS_2);
// TODO: Global wird fälschlicherweise hier je nach anzahl der aktivierten Gruppen bis zu 6x ausgeführt.
mqttSubscribe("zero/set/enabled", ((mCfg->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]));
mMqtt->publish("zero/set/sleep", ((mCfg->sleep) ? dict[STR_TRUE] : dict[STR_FALSE]), false);
mMqtt->subscribe("zero/set/sleep", QOS_2);
// TODO: Global wird fälschlicherweise hier je nach anzahl der aktivierten Gruppen bis zu 6x ausgeführt.
mqttSubscribe("zero/set/sleep", ((mCfg->sleep) ? dict[STR_TRUE] : dict[STR_FALSE]));
// Gruppen
String gr;
for (uint8_t group = 0; group < ZEROEXPORT_MAX_GROUPS; group++) {
zeroExportGroup_t *cfgGroup = &mCfg->groups[group];
if(!cfgGroup->enabled) continue; // exit here when group is disabled
gr = "zero/set/groups/" + String(group);
// General
PubSubInit(gr + "/enabled", ((cfgGroup->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]));
PubSubInit(gr + "/sleep", ((cfgGroup->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]));
mqttSubscribe(gr + "/enabled", ((cfgGroup->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]));
mqttSubscribe(gr + "/sleep", ((cfgGroup->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]));
// Powermeter
// Inverters
// Inverters - Only Publish
for (uint8_t inv = 0; inv < ZEROEXPORT_GROUP_MAX_INVERTERS; inv++) {
zeroExportGroupInverter_t *cfgGroupInv = &cfgGroup->inverters[inv];
PubSubInit(gr + "/inverters/" + String(inv) + "/enabled", ((cfgGroupInv->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]));
PubSubInit(gr + "/inverters/" + String(inv) + "/powerMin", String(cfgGroupInv->powerMin));
PubSubInit(gr + "/inverters/" + String(inv) + "/powerMax", String(cfgGroupInv->powerMax));
mqttSubscribe(gr + "/inverters/" + String(inv) + "/enabled", ((cfgGroupInv->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]));
mqttSubscribe(gr + "/inverters/" + String(inv) + "/powerMin", String(cfgGroupInv->powerMin));
mqttSubscribe(gr + "/inverters/" + String(inv) + "/powerMax", String(cfgGroupInv->powerMax));
}
// Battery
PubSubInit(gr + "/battery/switch", ((cfgGroup->battSwitch) ? dict[STR_TRUE] : dict[STR_FALSE]));
mqttSubscribe(gr + "/battery/switch", ((cfgGroup->battSwitch) ? dict[STR_TRUE] : dict[STR_FALSE]));
// Advanced
PubSubInit(gr + "/advanced/setPoint", String(cfgGroup->setPoint));
PubSubInit(gr + "/advanced/powerTolerance", String(cfgGroup->powerTolerance));
PubSubInit(gr + "/advanced/powerMax", String(cfgGroup->powerMax));
mqttSubscribe(gr + "/advanced/setPoint", String(cfgGroup->setPoint));
mqttSubscribe(gr + "/advanced/powerTolerance", String(cfgGroup->powerTolerance));
mqttSubscribe(gr + "/advanced/powerMax", String(cfgGroup->powerMax));
mqttSubscribe(gr + "/advanced/refresh", String(cfgGroup->refresh));
mqttSubscribe(gr + "/advanced/Kp", String(cfgGroup->Kp));
mqttSubscribe(gr + "/advanced/Ki", String(cfgGroup->Ki));
mqttSubscribe(gr + "/advanced/Kd", String(cfgGroup->Kd));
}
}
@ -1603,54 +1660,20 @@ class ZeroExport {
String gr;
// Global (zeroExport)
// TODO: Global wird fälschlicherweise hier je nach anzahl der aktivierten Gruppen bis zu 6x ausgeführt.
mMqtt->publish("zero/state/enabled", ((mCfg->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]), false);
// TODO: Global wird fälschlicherweise hier je nach anzahl der aktivierten Gruppen bis zu 6x ausgeführt.
mMqtt->publish("zero/state/sleep", ((mCfg->sleep) ? dict[STR_TRUE] : dict[STR_FALSE]), false);
mqttSubscribe("zero/set/enabled", ((mCfg->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]));
mqttSubscribe("zero/set/sleep", ((mCfg->sleep) ? dict[STR_TRUE] : dict[STR_FALSE]));
// General
gr = "zero/state/groups/" + String(group) + "/enabled";
mMqtt->publish(gr.c_str(), ((cfgGroup->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]), false);
mqttPublish(gr.c_str(), ((cfgGroup->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]));
mqttPublish(gr.c_str(), ((cfgGroup->sleep) ? dict[STR_TRUE] : dict[STR_FALSE]));
gr = "zero/state/groups/" + String(group) + "/sleep";
mMqtt->publish(gr.c_str(), ((cfgGroup->sleep) ? dict[STR_TRUE] : dict[STR_FALSE]), false);
gr = "zero/state/groups/" + String(group) + "/name";
mMqtt->publish(gr.c_str(), cfgGroup->name, false);
// Inverters
for (uint8_t inv = 0; inv < ZEROEXPORT_GROUP_MAX_INVERTERS; inv++) {
zeroExportGroupInverter_t *cfgGroupInv = &cfgGroup->inverters[inv];
gr = "zero/state/groups/" + String(group) + "/inverters/" + String(inv);
mqttObj["enabled"] = cfgGroupInv->enabled;
mqttObj["id"] = cfgGroupInv->id;
mqttObj["target"] = cfgGroupInv->target;
mqttObj["powerMin"] = cfgGroupInv->powerMin;
mqttObj["powerMax"] = cfgGroupInv->powerMax;
mMqtt->publish(gr.c_str(), mqttDoc.as<std::string>().c_str(), false);
mqttDoc.clear();
}
// Battery
gr = "zero/state/groups/" + String(group) + "/battery";
mqttObj["enabled"] = cfgGroup->battEnabled;
mqttObj["voltageOn"] = cfgGroup->battVoltageOn;
mqttObj["voltageOff"] = cfgGroup->battVoltageOff;
mqttObj["switch"] = cfgGroup->battSwitch;
mMqtt->publish(gr.c_str(), mqttDoc.as<std::string>().c_str(), false);
mqttDoc.clear();
// Advanced
gr = "zero/state/groups/" + String(group) + "/advanced";
mqttObj["setPoint"] = cfgGroup->setPoint;
mqttObj["refresh"] = cfgGroup->refresh;
mqttObj["powerTolerance"] = cfgGroup->powerTolerance;
mqttObj["powerMax"] = cfgGroup->powerMax;
mqttObj["Kp"] = cfgGroup->Kp;
mqttObj["Ki"] = cfgGroup->Ki;
mqttObj["Kd"] = cfgGroup->Kd;
mMqtt->publish(gr.c_str(), mqttDoc.as<std::string>().c_str(), false);
mqttDoc.clear();
}
return true;

2
src/web/html/setup.html

@ -1469,7 +1469,7 @@
e.appendChild(opt("0", "---"));
e.appendChild(opt("1", "Shelly"));
//e.appendChild(opt("2", "Tasmota"));
//e.appendChild(opt("3", "Mqtt"));
e.appendChild(opt("3", "Mqtt"));
//e.appendChild(opt("4", "Hichi"));
e.appendChild(opt("5", "Tibber"));
e.appendChild(opt("6", "Shrdzm"));

Loading…
Cancel
Save