|
@ -46,6 +46,7 @@ class PubMqtt { |
|
|
mSys = sys; |
|
|
mSys = sys; |
|
|
mUtcTimestamp = utcTs; |
|
|
mUtcTimestamp = utcTs; |
|
|
mIntervalTimeout = 1; |
|
|
mIntervalTimeout = 1; |
|
|
|
|
|
mReconnectRequest = false; |
|
|
|
|
|
|
|
|
snprintf(mLwtTopic, MQTT_TOPIC_LEN + 5, "%s/mqtt", mCfgMqtt->topic); |
|
|
snprintf(mLwtTopic, MQTT_TOPIC_LEN + 5, "%s/mqtt", mCfgMqtt->topic); |
|
|
|
|
|
|
|
@ -66,6 +67,7 @@ class PubMqtt { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void connect() { |
|
|
void connect() { |
|
|
|
|
|
mReconnectRequest = false; |
|
|
if(!mClient.connected()) |
|
|
if(!mClient.connected()) |
|
|
mClient.connect(); |
|
|
mClient.connect(); |
|
|
} |
|
|
} |
|
@ -80,7 +82,10 @@ class PubMqtt { |
|
|
sendIvData(); |
|
|
sendIvData(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if(mReconnectRequest) { |
|
|
|
|
|
connect(); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void tickerMinute() { |
|
|
void tickerMinute() { |
|
@ -147,12 +152,21 @@ class PubMqtt { |
|
|
if(!mClient.connected()) |
|
|
if(!mClient.connected()) |
|
|
return; |
|
|
return; |
|
|
|
|
|
|
|
|
if(addTopic) { |
|
|
String topic = ""; |
|
|
String topic = String(mCfgMqtt->topic) + "/" + String(subTopic); |
|
|
if(addTopic) |
|
|
mClient.publish(topic.c_str(), QOS_0, retained, payload); |
|
|
topic = String(mCfgMqtt->topic) + "/"; |
|
|
} |
|
|
topic += String(subTopic); |
|
|
else |
|
|
|
|
|
mClient.publish(subTopic, QOS_0, retained, payload); |
|
|
do { |
|
|
|
|
|
if(0 != mClient.publish(topic.c_str(), QOS_0, retained, payload)) |
|
|
|
|
|
break; |
|
|
|
|
|
if(!mClient.connected()) |
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
mClient.loop(); |
|
|
|
|
|
yield(); |
|
|
|
|
|
} while(1); |
|
|
|
|
|
|
|
|
mTxCnt++; |
|
|
mTxCnt++; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -181,7 +195,7 @@ class PubMqtt { |
|
|
void sendDiscoveryConfig(void) { |
|
|
void sendDiscoveryConfig(void) { |
|
|
DPRINTLN(DBG_VERBOSE, F("sendMqttDiscoveryConfig")); |
|
|
DPRINTLN(DBG_VERBOSE, F("sendMqttDiscoveryConfig")); |
|
|
|
|
|
|
|
|
char topic[64], buffer[512], name[32], uniq_id[32]; |
|
|
char topic[64], name[32], uniq_id[32]; |
|
|
DynamicJsonDocument doc(512); |
|
|
DynamicJsonDocument doc(512); |
|
|
for (uint8_t id = 0; id < mSys->getNumInverters(); id++) { |
|
|
for (uint8_t id = 0; id < mSys->getNumInverters(); id++) { |
|
|
Inverter<> *iv = mSys->getInverterByPos(id); |
|
|
Inverter<> *iv = mSys->getInverterByPos(id); |
|
@ -221,8 +235,12 @@ class PubMqtt { |
|
|
doc[F("stat_cla")] = String(stateCls); |
|
|
doc[F("stat_cla")] = String(stateCls); |
|
|
|
|
|
|
|
|
snprintf(topic, 64, "%s/sensor/%s/ch%d_%s/config", MQTT_DISCOVERY_PREFIX, iv->config->name, rec->assign[i].ch, iv->getFieldName(i, rec)); |
|
|
snprintf(topic, 64, "%s/sensor/%s/ch%d_%s/config", MQTT_DISCOVERY_PREFIX, iv->config->name, rec->assign[i].ch, iv->getFieldName(i, rec)); |
|
|
serializeJson(doc, buffer); |
|
|
size_t size = measureJson(doc) + 1; |
|
|
publish(topic, buffer, true, false); |
|
|
char *buf = new char[size]; |
|
|
|
|
|
memset(buf, 0, size); |
|
|
|
|
|
serializeJson(doc, buf, size); |
|
|
|
|
|
publish(topic, buf, true, false); |
|
|
|
|
|
delete[] buf; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
yield(); |
|
|
yield(); |
|
@ -258,7 +276,7 @@ class PubMqtt { |
|
|
switch (reason) { |
|
|
switch (reason) { |
|
|
case espMqttClientTypes::DisconnectReason::TCP_DISCONNECTED: |
|
|
case espMqttClientTypes::DisconnectReason::TCP_DISCONNECTED: |
|
|
DBGPRINTLN(F("TCP disconnect")); |
|
|
DBGPRINTLN(F("TCP disconnect")); |
|
|
connect(); |
|
|
mReconnectRequest = true; |
|
|
break; |
|
|
break; |
|
|
case espMqttClientTypes::DisconnectReason::MQTT_UNACCEPTABLE_PROTOCOL_VERSION: |
|
|
case espMqttClientTypes::DisconnectReason::MQTT_UNACCEPTABLE_PROTOCOL_VERSION: |
|
|
DBGPRINTLN(F("wrong protocol version")); |
|
|
DBGPRINTLN(F("wrong protocol version")); |
|
@ -561,6 +579,7 @@ class PubMqtt { |
|
|
std::queue<uint8_t> mSendList; |
|
|
std::queue<uint8_t> mSendList; |
|
|
subscriptionCb mSubscriptionCb; |
|
|
subscriptionCb mSubscriptionCb; |
|
|
bool mIvAvail; // shows if at least one inverter is available
|
|
|
bool mIvAvail; // shows if at least one inverter is available
|
|
|
|
|
|
bool mReconnectRequest; |
|
|
uint8_t mLastIvState[MAX_NUM_INVERTERS]; |
|
|
uint8_t mLastIvState[MAX_NUM_INVERTERS]; |
|
|
uint16_t mIntervalTimeout; |
|
|
uint16_t mIntervalTimeout; |
|
|
|
|
|
|
|
|