diff --git a/MQTTPublisher.cpp b/MQTTPublisher.cpp index 6387535..228e942 100644 --- a/MQTTPublisher.cpp +++ b/MQTTPublisher.cpp @@ -3,7 +3,7 @@ WiFiClient espClient; PubSubClient client(espClient); -MQTTPublisher::MQTTPublisher(SettingsManager * settingsManager, GoodWeCommunicator * goodWe, bool inDebugMode) +MQTTPublisher::MQTTPublisher(SettingsManager* settingsManager, GoodWeCommunicator* goodWe, bool inDebugMode) { randomSeed(micros()); mqttSettingsManager = settingsManager; @@ -44,7 +44,7 @@ bool MQTTPublisher::reconnect() Serial.println("Connecting without user credentials."); clientConnected = client.connect(clientId.c_str()); } - + if (clientConnected) { if (debugMode) @@ -106,58 +106,56 @@ void MQTTPublisher::handle() auto inverters = goodweCommunicator->getInvertersInfo(); for (char cnt = 0; cnt < inverters.size(); cnt++) { - if (inverters[cnt].addressConfirmed) + auto prependTopic = (String("goodwe/") + String(inverters[cnt].serialNumber)); + if (debugMode) { - auto prependTopic = (String("goodwe/") + String(inverters[cnt].serialNumber)); - if (debugMode) - { - Serial.print("Publishing prepend topic for this inverter is: "); - Serial.println(prependTopic); - } + Serial.print("Publishing prepend topic for this inverter is: "); + Serial.println(prependTopic); + } - if (inverters[cnt].isOnline) + if (inverters[cnt].isOnline && inverters[cnt].addressConfirmed) + { + if (sendQuick) { - if (sendQuick) + //send out fast changing values + if (sendOk) sendOk = publishOnMQTT(prependTopic, "/online", "1"); + if (sendOk) sendOk = publishOnMQTT(prependTopic, "/vpv1", String(inverters[cnt].vpv1, 1)); + if (sendOk) sendOk = publishOnMQTT(prependTopic, "/vpv2", String(inverters[cnt].vpv2, 1)); + if (sendOk) sendOk = publishOnMQTT(prependTopic, "/ipv1", String(inverters[cnt].ipv1, 1)); + if (sendOk) sendOk = publishOnMQTT(prependTopic, "/ipv2", String(inverters[cnt].ipv2, 1)); + //publishing sometimes cuases the wdt to reset the ESP. + //On the github page of the pubsubclient it was suggested to add extra client.loop(). + client.loop(); + if (sendOk) sendOk = publishOnMQTT(prependTopic, "/vac1", String(inverters[cnt].vac1, 1)); + if (sendOk) sendOk = publishOnMQTT(prependTopic, "/iac1", String(inverters[cnt].iac1, 1)); + if (sendOk) sendOk = publishOnMQTT(prependTopic, "/fac1", String(inverters[cnt].fac1, 2)); + if (sendOk) sendOk = publishOnMQTT(prependTopic, "/pac", String(inverters[cnt].pac)); + if (sendOk) sendOk = publishOnMQTT(prependTopic, "/temp", String(inverters[cnt].temp, 1)); + + if (inverters[cnt].isDTSeries) { - //send out fast changing values - if (sendOk) sendOk = publishOnMQTT(prependTopic, "/online", "1"); - if (sendOk) sendOk = publishOnMQTT(prependTopic, "/vpv1", String(inverters[cnt].vpv1, 1)); - if (sendOk) sendOk = publishOnMQTT(prependTopic, "/vpv2", String(inverters[cnt].vpv2, 1)); - if (sendOk) sendOk = publishOnMQTT(prependTopic, "/ipv1", String(inverters[cnt].ipv1, 1)); - if (sendOk) sendOk = publishOnMQTT(prependTopic, "/ipv2", String(inverters[cnt].ipv2, 1)); - //publishing sometimes cuases the wdt to reset the ESP. //On the github page of the pubsubclient it was suggested to add extra client.loop(). client.loop(); - if (sendOk) sendOk = publishOnMQTT(prependTopic, "/vac1", String(inverters[cnt].vac1, 1)); - if (sendOk) sendOk = publishOnMQTT(prependTopic, "/iac1", String(inverters[cnt].iac1, 1)); - if (sendOk) sendOk = publishOnMQTT(prependTopic, "/fac1", String(inverters[cnt].fac1, 2)); - if (sendOk) sendOk = publishOnMQTT(prependTopic, "/pac", String(inverters[cnt].pac)); - if (sendOk) sendOk = publishOnMQTT(prependTopic, "/temp", String(inverters[cnt].temp, 1)); - - if (inverters[cnt].isDTSeries) - { - //On the github page of the pubsubclient it was suggested to add extra client.loop(). - client.loop(); - //also send tri fase info - if (sendOk) sendOk = publishOnMQTT(prependTopic, "/vac2", String(inverters[cnt].vac2, 1)); - if (sendOk) sendOk = publishOnMQTT(prependTopic, "/iac2", String(inverters[cnt].iac2, 1)); - if (sendOk) sendOk = publishOnMQTT(prependTopic, "/fac2", String(inverters[cnt].fac2, 2)); - if (sendOk) sendOk = publishOnMQTT(prependTopic, "/vac3", String(inverters[cnt].vac3, 1)); - if (sendOk) sendOk = publishOnMQTT(prependTopic, "/iac3", String(inverters[cnt].iac3, 1)); - if (sendOk) sendOk = publishOnMQTT(prependTopic, "/fac3", String(inverters[cnt].fac3, 2)); - } - } - else - { - //regular - if (sendOk) sendOk = publishOnMQTT(prependTopic, "/workmode", String(inverters[cnt].workMode)); - if (sendOk) sendOk = publishOnMQTT(prependTopic, "/eday", String(inverters[cnt].eDay)); - //TODO: Rest of data + //also send tri fase info + if (sendOk) sendOk = publishOnMQTT(prependTopic, "/vac2", String(inverters[cnt].vac2, 1)); + if (sendOk) sendOk = publishOnMQTT(prependTopic, "/iac2", String(inverters[cnt].iac2, 1)); + if (sendOk) sendOk = publishOnMQTT(prependTopic, "/fac2", String(inverters[cnt].fac2, 2)); + if (sendOk) sendOk = publishOnMQTT(prependTopic, "/vac3", String(inverters[cnt].vac3, 1)); + if (sendOk) sendOk = publishOnMQTT(prependTopic, "/iac3", String(inverters[cnt].iac3, 1)); + if (sendOk) sendOk = publishOnMQTT(prependTopic, "/fac3", String(inverters[cnt].fac3, 2)); } } - else if (sendRegular) //only send offline info on regular basis - if (sendOk) sendOk = publishOnMQTT(prependTopic, "/online", "0"); + else + { + //regular + if (sendOk) sendOk = publishOnMQTT(prependTopic, "/workmode", String(inverters[cnt].workMode)); + if (sendOk) sendOk = publishOnMQTT(prependTopic, "/eday", String(inverters[cnt].eDay)); + //TODO: Rest of data + } } + else if (sendRegular) //only send offline info on regular basis + if (sendOk) sendOk = publishOnMQTT(prependTopic, "/online", "0"); + //On the github page of the pubsubclient it was suggested to add extra client.loop(). client.loop(); @@ -176,10 +174,10 @@ void MQTTPublisher::handle() } } - bool MQTTPublisher::publishOnMQTT(String prepend, String topic, String value) - { - auto retVal = client.publish((prepend.c_str() + topic).c_str(), value.c_str()); - yield(); - return retVal; - } +bool MQTTPublisher::publishOnMQTT(String prepend, String topic, String value) +{ + auto retVal = client.publish((prepend.c_str() + topic).c_str(), value.c_str()); + yield(); + return retVal; +}