use mqtt queue for ESP32

This commit is contained in:
MichaelDvP
2020-11-18 10:01:41 +01:00
parent 09aa236d2f
commit 4bf821fbd5
2 changed files with 13 additions and 2 deletions

View File

@@ -789,8 +789,11 @@ void Mqtt::register_mqtt_ha_sensor(const char * prefix,
}
new_name[0] = toupper(new_name[0]); // capitalize first letter
#if defined(ESP32)
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_HA_CONFIG> doc;
#else
DynamicJsonDocument doc(EMSESP_MAX_JSON_SIZE_HA_CONFIG);
#endif
doc["name"] = new_name;
doc["uniq_id"] = uniq;
if (uom != nullptr) {
@@ -805,9 +808,14 @@ void Mqtt::register_mqtt_ha_sensor(const char * prefix,
JsonArray ids = dev.createNestedArray("ids");
ids.add(ha_device);
#if defined(ESP32)
// queue MQTT publish
publish_retain(topic, doc.as<JsonObject>(), true);
#else
// convert json to string and publish immediately with retain forced to true
std::string payload_text;
serializeJson(doc, payload_text); // convert json to string
uint16_t packet_id = mqttClient_->publish(topic, 0, true, payload_text.c_str());
if (!packet_id) {
LOG_ERROR(F("Failed to publish topic %s"), topic);
@@ -820,6 +828,7 @@ void Mqtt::register_mqtt_ha_sensor(const char * prefix,
}
delay(50); // enough time to send the short message out
#endif
}
} // namespace emsesp

View File

@@ -39,7 +39,7 @@
using uuid::console::Shell;
#define EMSESP_MAX_JSON_SIZE_HA_CONFIG 384 // for small HA config payloads
#define EMSESP_MAX_JSON_SIZE_SMALL 384 // for smaller json docs when using StaticJsonDocument
#define EMSESP_MAX_JSON_SIZE_SMALL 256 // 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
@@ -176,6 +176,8 @@ class Mqtt {
#if defined(EMSESP_STANDALONE)
static constexpr size_t MAX_MQTT_MESSAGES = 70; // size of queue
#elif defined(ESP32)
static constexpr size_t MAX_MQTT_MESSAGES = 100; // size of queue
#else
static constexpr size_t MAX_MQTT_MESSAGES = 20; // size of queue
#endif