diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 7be71f867..eb0601eb0 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -687,8 +687,7 @@ void Mqtt::register_mqtt_ha_binary_sensor(const __FlashStringHelper * name, cons return; } - DynamicJsonDocument doc(EMSESP_MAX_JSON_SIZE_SMALL); - // StaticJsonDocument doc; + StaticJsonDocument doc; doc["name"] = name; doc["uniq_id"] = entity; @@ -716,31 +715,16 @@ void Mqtt::register_mqtt_ha_binary_sensor(const __FlashStringHelper * name, cons snprintf_P(ha_device, sizeof(ha_device), PSTR("ems-esp-%s"), EMSdevice::device_type_2_device_name(device_type).c_str()); ids.add(ha_device); - doc.shrinkToFit(); - char topic[MQTT_TOPIC_MAX_SIZE]; snprintf_P(topic, sizeof(topic), PSTR("homeassistant/binary_sensor/ems-esp/%s/config"), entity); - // convert json to string and publish immediately with retain forced to true - char payload_text[256]; - serializeJson(doc, payload_text); // convert json to string - uint16_t packet_id = mqttClient_->publish(topic, 0, true, payload_text); -#if defined(EMSESP_STANDALONE) - LOG_DEBUG(F("Publishing topic %s"), topic); -#else - if (!packet_id) { - LOG_ERROR(F("Failed to publish topic %s"), topic); - } else { - LOG_DEBUG(F("Publishing topic %s"), topic); - } -#endif - - // delay(MQTT_PUBLISH_WAIT); - delay(50); + // queue MQTT publish + publish(topic, doc.as()); } // HA config for a normal 'sensor' type // entity must match the key/value pair in the _data topic +// some string copying here into chars, it looks messy but does help with heap fragmentation issues void Mqtt::register_mqtt_ha_sensor(const char * prefix, const __FlashStringHelper * suffix, const __FlashStringHelper * name, @@ -797,8 +781,7 @@ void Mqtt::register_mqtt_ha_sensor(const char * prefix, } new_name[0] = toupper(new_name[0]); // capitalize first letter - DynamicJsonDocument doc(EMSESP_MAX_JSON_SIZE_SMALL); - // StaticJsonDocument doc; + StaticJsonDocument doc; doc["name"] = new_name; doc["uniq_id"] = uniq.c_str(); @@ -814,9 +797,8 @@ void Mqtt::register_mqtt_ha_sensor(const char * prefix, JsonArray ids = dev.createNestedArray("ids"); ids.add(ha_device); - doc.shrinkToFit(); // convert json to string and publish immediately with retain forced to true - char payload_text[256]; + char payload_text[EMSESP_MAX_JSON_SIZE_HA_CONFIG]; serializeJson(doc, payload_text); // convert json to string uint16_t packet_id = mqttClient_->publish(topic, 0, true, payload_text); @@ -830,7 +812,6 @@ void Mqtt::register_mqtt_ha_sensor(const char * prefix, #endif } - // delay(MQTT_PUBLISH_WAIT); // don't flood asynctcp delay(50); // enough time to send the short message out } } // namespace emsesp diff --git a/src/mqtt.h b/src/mqtt.h index cadcc9948..faf8c8f04 100644 --- a/src/mqtt.h +++ b/src/mqtt.h @@ -38,10 +38,11 @@ using uuid::console::Shell; -#define EMSESP_MAX_JSON_SIZE_SMALL 384 // for smaller json docs when using StaticJsonDocument -#define EMSESP_MAX_JSON_SIZE_MEDIUM 768 // for medium json docs from ems devices, when using StaticJsonDocument -#define EMSESP_MAX_JSON_SIZE_LARGE 1024 // for large json docs from ems devices, like boiler or thermostat data. Using StaticJsonDocument -#define EMSESP_MAX_JSON_SIZE_DYN 2048 // for large json docs from web. Using DynamicJsonDocument +#define EMSESP_MAX_JSON_SIZE_HA_CONFIG 256 // for small HA config payloads +#define EMSESP_MAX_JSON_SIZE_SMALL 384 // for smaller json docs when using StaticJsonDocument +#define EMSESP_MAX_JSON_SIZE_MEDIUM 768 // for medium json docs from ems devices, when using StaticJsonDocument +#define EMSESP_MAX_JSON_SIZE_LARGE 1024 // for large json docs from ems devices, like boiler or thermostat data. Using StaticJsonDocument +#define EMSESP_MAX_JSON_SIZE_DYN 2048 // for large json docs from web. Using DynamicJsonDocument namespace emsesp {