Browse Source

Merge branch 'development01' of https://github.com/Argafal/ahoy into development03

pull/788/head
lumapu 2 years ago
parent
commit
3e6693ff30
  1. 21
      src/publisher/pubMqtt.h
  2. 2
      src/publisher/pubMqttDefs.h

21
src/publisher/pubMqtt.h

@ -111,6 +111,7 @@ class PubMqtt {
publish(subtopics[MQTT_UPTIME], val); publish(subtopics[MQTT_UPTIME], val);
publish(subtopics[MQTT_RSSI], String(WiFi.RSSI()).c_str()); publish(subtopics[MQTT_RSSI], String(WiFi.RSSI()).c_str());
publish(subtopics[MQTT_FREE_HEAP], String(ESP.getFreeHeap()).c_str()); publish(subtopics[MQTT_FREE_HEAP], String(ESP.getFreeHeap()).c_str());
publish(subtopics[MQTT_HEAP_FRAG], String(ESP.getHeapFragmentation()).c_str());
} }
bool tickerSun(uint32_t sunrise, uint32_t sunset, uint32_t offs, bool disNightCom) { bool tickerSun(uint32_t sunrise, uint32_t sunset, uint32_t offs, bool disNightCom) {
@ -162,13 +163,15 @@ class PubMqtt {
if(!mClient.connected()) if(!mClient.connected())
return; return;
String topic = ""; memset(mTopic, 0, MQTT_TOPIC_LEN + 32 + MAX_NAME_LENGTH + 1);
if(addTopic) if(addTopic){
topic = String(mCfgMqtt->topic) + "/"; snprintf(mTopic, MQTT_TOPIC_LEN + 32 + MAX_NAME_LENGTH + 1, "%s/%s", mCfgMqtt->topic, subTopic);
topic += String(subTopic); } else {
snprintf(mTopic, MQTT_TOPIC_LEN + 32 + MAX_NAME_LENGTH + 1, "%s", subTopic);
}
do { do {
if(0 != mClient.publish(topic.c_str(), QOS_0, retained, payload)) if(0 != mClient.publish(mTopic, QOS_0, retained, payload))
break; break;
if(!mClient.connected()) if(!mClient.connected())
break; break;
@ -207,7 +210,7 @@ class PubMqtt {
DPRINTLN(DBG_VERBOSE, F("sendMqttDiscoveryConfig")); DPRINTLN(DBG_VERBOSE, F("sendMqttDiscoveryConfig"));
char topic[64], name[32], uniq_id[32]; char topic[64], name[32], uniq_id[32];
DynamicJsonDocument doc(256); StaticJsonDocument<256> doc;
uint8_t fldTotal[4] = {FLD_PAC, FLD_YT, FLD_YD, FLD_PDC}; uint8_t fldTotal[4] = {FLD_PAC, FLD_YT, FLD_YD, FLD_PDC};
const char* unitTotal[4] = {"W", "kWh", "Wh", "W"}; const char* unitTotal[4] = {"W", "kWh", "Wh", "W"};
@ -264,7 +267,7 @@ class PubMqtt {
stateCls = getFieldStateClass(fldTotal[i]); stateCls = getFieldStateClass(fldTotal[i]);
} }
DynamicJsonDocument doc2(512); StaticJsonDocument<512> doc2;
doc2[F("name")] = name; doc2[F("name")] = name;
doc2[F("stat_t")] = String(mCfgMqtt->topic) + "/" + ((!total) ? String(iv->config->name) : "total" ) + String(topic); doc2[F("stat_t")] = String(mCfgMqtt->topic) + "/" + ((!total) ? String(iv->config->name) : "total" ) + String(topic);
doc2[F("unit_of_meas")] = ((!total) ? (iv->getUnit(i,rec)) : (unitTotal[i])); doc2[F("unit_of_meas")] = ((!total) ? (iv->getUnit(i,rec)) : (unitTotal[i]));
@ -356,7 +359,7 @@ class PubMqtt {
if(NULL == mSubscriptionCb) if(NULL == mSubscriptionCb)
return; return;
DynamicJsonDocument json(128); StaticJsonDocument<128> json;
JsonObject root = json.to<JsonObject>(); JsonObject root = json.to<JsonObject>();
bool limitAbs = false; bool limitAbs = false;
@ -636,6 +639,8 @@ class PubMqtt {
char mLwtTopic[MQTT_TOPIC_LEN+5]; char mLwtTopic[MQTT_TOPIC_LEN+5];
const char *mDevName, *mVersion; const char *mDevName, *mVersion;
char mClientId[24]; // number of chars is limited to 23 up to v3.1 of MQTT char mClientId[24]; // number of chars is limited to 23 up to v3.1 of MQTT
// global buffer for mqtt topic. Used when publishing mqtt messages.
char mTopic[MQTT_TOPIC_LEN + 32 + MAX_NAME_LENGTH + 1];
}; };
#endif /*__PUB_MQTT_H__*/ #endif /*__PUB_MQTT_H__*/

2
src/publisher/pubMqttDefs.h

@ -41,6 +41,7 @@ enum {
MQTT_UPTIME = 0, MQTT_UPTIME = 0,
MQTT_RSSI, MQTT_RSSI,
MQTT_FREE_HEAP, MQTT_FREE_HEAP,
MQTT_HEAP_FRAG,
MQTT_SUNRISE, MQTT_SUNRISE,
MQTT_SUNSET, MQTT_SUNSET,
MQTT_COMM_START, MQTT_COMM_START,
@ -64,6 +65,7 @@ const char* const subtopics[] PROGMEM = {
"uptime", "uptime",
"wifi_rssi", "wifi_rssi",
"free_heap", "free_heap",
"heap_frag",
"sunrise", "sunrise",
"sunset", "sunset",
"comm_start", "comm_start",

Loading…
Cancel
Save