HA binary sensors put on MQTT queue

This commit is contained in:
proddy
2020-11-15 15:22:54 +01:00
parent 88135ad277
commit 978e0b8d83
2 changed files with 11 additions and 29 deletions

View File

@@ -687,8 +687,7 @@ void Mqtt::register_mqtt_ha_binary_sensor(const __FlashStringHelper * name, cons
return; return;
} }
DynamicJsonDocument doc(EMSESP_MAX_JSON_SIZE_SMALL); StaticJsonDocument<EMSESP_MAX_JSON_SIZE_SMALL> doc;
// StaticJsonDocument<EMSESP_MAX_JSON_SIZE_SMALL> doc;
doc["name"] = name; doc["name"] = name;
doc["uniq_id"] = entity; 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()); 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); ids.add(ha_device);
doc.shrinkToFit();
char topic[MQTT_TOPIC_MAX_SIZE]; char topic[MQTT_TOPIC_MAX_SIZE];
snprintf_P(topic, sizeof(topic), PSTR("homeassistant/binary_sensor/ems-esp/%s/config"), entity); 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 // queue MQTT publish
char payload_text[256]; publish(topic, doc.as<JsonObject>());
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);
} }
// HA config for a normal 'sensor' type // HA config for a normal 'sensor' type
// entity must match the key/value pair in the _data topic // 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, void Mqtt::register_mqtt_ha_sensor(const char * prefix,
const __FlashStringHelper * suffix, const __FlashStringHelper * suffix,
const __FlashStringHelper * name, 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 new_name[0] = toupper(new_name[0]); // capitalize first letter
DynamicJsonDocument doc(EMSESP_MAX_JSON_SIZE_SMALL); StaticJsonDocument<EMSESP_MAX_JSON_SIZE_HA_CONFIG> doc;
// StaticJsonDocument<EMSESP_MAX_JSON_SIZE_SMALL> doc;
doc["name"] = new_name; doc["name"] = new_name;
doc["uniq_id"] = uniq.c_str(); doc["uniq_id"] = uniq.c_str();
@@ -814,9 +797,8 @@ void Mqtt::register_mqtt_ha_sensor(const char * prefix,
JsonArray ids = dev.createNestedArray("ids"); JsonArray ids = dev.createNestedArray("ids");
ids.add(ha_device); ids.add(ha_device);
doc.shrinkToFit();
// convert json to string and publish immediately with retain forced to true // 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 serializeJson(doc, payload_text); // convert json to string
uint16_t packet_id = mqttClient_->publish(topic, 0, true, payload_text); 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 #endif
} }
// delay(MQTT_PUBLISH_WAIT); // don't flood asynctcp
delay(50); // enough time to send the short message out delay(50); // enough time to send the short message out
} }
} // namespace emsesp } // namespace emsesp

View File

@@ -38,10 +38,11 @@
using uuid::console::Shell; using uuid::console::Shell;
#define EMSESP_MAX_JSON_SIZE_SMALL 384 // for smaller json docs when using StaticJsonDocument #define EMSESP_MAX_JSON_SIZE_HA_CONFIG 256 // for small HA config payloads
#define EMSESP_MAX_JSON_SIZE_MEDIUM 768 // for medium json docs from ems devices, when using StaticJsonDocument #define EMSESP_MAX_JSON_SIZE_SMALL 384 // for smaller json docs 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_MEDIUM 768 // for medium json docs from ems devices, when using StaticJsonDocument
#define EMSESP_MAX_JSON_SIZE_DYN 2048 // for large json docs from web. Using DynamicJsonDocument #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 { namespace emsesp {