Fixes to mqtt to prevent delays when the broker is unavailable which can trigger the watchdog timer reset

This commit is contained in:
Matt Holmes 2025-03-08 08:41:54 +00:00
parent a50a20ca1c
commit 6109211c96
2 changed files with 46 additions and 13 deletions

View file

@ -121,7 +121,9 @@
/* MQTT options */ /* MQTT options */
// #define MQTT // Enable this line to enable MQTT // #define MQTT // Enable this line to enable MQTT
#define MQTT_QOS 0 // MQTT Quality of Service (0, 1, or 2) #define MQTT_QOS 0 // MQTT Quality of Service (0, 1, or 2)
#define MQTT_PUBLISH_CELL_VOLTAGES // Enable this line to publish cell voltages to MQTT
#define MQTT_TIMEOUT 2000 // MQTT timeout in milliseconds
#define MQTT_MANUAL_TOPIC_OBJECT_NAME #define MQTT_MANUAL_TOPIC_OBJECT_NAME
// Enable MQTT_MANUAL_TOPIC_OBJECT_NAME to use custom MQTT topic, object ID prefix, and device name. // Enable MQTT_MANUAL_TOPIC_OBJECT_NAME to use custom MQTT topic, object ID prefix, and device name.
// WARNING: If this is not defined, the previous default naming format 'battery-emulator_esp32-XXXXXX' (based on hardware ID) will be used. // WARNING: If this is not defined, the previous default naming format 'battery-emulator_esp32-XXXXXX' (based on hardware ID) will be used.

View file

@ -25,16 +25,30 @@ static String object_id_prefix = "";
static String device_name = ""; static String device_name = "";
static String device_id = ""; static String device_id = "";
static void publish_common_info(void); static bool publish_common_info(void);
static void publish_cell_voltages(void); static bool publish_cell_voltages(void);
static void publish_events(void); static bool publish_events(void);
/** Publish global values and call callbacks for specific modules */ /** Publish global values and call callbacks for specific modules */
static void publish_values(void) { static void publish_values(void) {
mqtt_publish((topic_name + "/status").c_str(), "online", false);
publish_events(); if (mqtt_publish((topic_name + "/status").c_str(), "online", false) == false) {
publish_common_info(); return;
publish_cell_voltages(); }
if (publish_events() == false) {
return;
}
if (publish_common_info() == false) {
return;
}
#ifdef MQTT_PUBLISH_CELL_VOLTAGES
if (publish_cell_voltages() == false) {
return;
}
#endif
} }
#ifdef HA_AUTODISCOVERY #ifdef HA_AUTODISCOVERY
@ -169,7 +183,7 @@ void set_battery_attributes(JsonDocument& doc, const DATALAYER_BATTERY_TYPE& bat
static std::vector<EventData> order_events; static std::vector<EventData> order_events;
static void publish_common_info(void) { static bool publish_common_info(void) {
static JsonDocument doc; static JsonDocument doc;
static String state_topic = topic_name + "/info"; static String state_topic = topic_name + "/info";
#ifdef HA_AUTODISCOVERY #ifdef HA_AUTODISCOVERY
@ -191,6 +205,8 @@ static void publish_common_info(void) {
serializeJson(doc, mqtt_msg); serializeJson(doc, mqtt_msg);
if (mqtt_publish(generateCommonInfoAutoConfigTopic(config.object_id).c_str(), mqtt_msg, true)) { if (mqtt_publish(generateCommonInfoAutoConfigTopic(config.object_id).c_str(), mqtt_msg, true)) {
ha_common_info_published = true; ha_common_info_published = true;
} else {
return false;
} }
doc.clear(); doc.clear();
} }
@ -211,18 +227,20 @@ static void publish_common_info(void) {
} }
#endif // DOUBLE_BATTERY #endif // DOUBLE_BATTERY
serializeJson(doc, mqtt_msg); serializeJson(doc, mqtt_msg);
if (!mqtt_publish(state_topic.c_str(), mqtt_msg, false)) { if (mqtt_publish(state_topic.c_str(), mqtt_msg, false) == false) {
#ifdef DEBUG_LOG #ifdef DEBUG_LOG
logging.println("Common info MQTT msg could not be sent"); logging.println("Common info MQTT msg could not be sent");
#endif // DEBUG_LOG #endif // DEBUG_LOG
return false;
} }
doc.clear(); doc.clear();
#ifdef HA_AUTODISCOVERY #ifdef HA_AUTODISCOVERY
} }
#endif // HA_AUTODISCOVERY #endif // HA_AUTODISCOVERY
return true;
} }
static void publish_cell_voltages(void) { static bool publish_cell_voltages(void) {
static JsonDocument doc; static JsonDocument doc;
static String state_topic = topic_name + "/spec_data"; static String state_topic = topic_name + "/spec_data";
#ifdef DOUBLE_BATTERY #ifdef DOUBLE_BATTERY
@ -244,6 +262,7 @@ static void publish_cell_voltages(void) {
serializeJson(doc, mqtt_msg, sizeof(mqtt_msg)); serializeJson(doc, mqtt_msg, sizeof(mqtt_msg));
if (mqtt_publish(generateCellVoltageAutoConfigTopic(cellNumber, "").c_str(), mqtt_msg, true) == false) { if (mqtt_publish(generateCellVoltageAutoConfigTopic(cellNumber, "").c_str(), mqtt_msg, true) == false) {
failed_to_publish = true; failed_to_publish = true;
return false;
} }
} }
doc.clear(); // clear after sending autoconfig doc.clear(); // clear after sending autoconfig
@ -260,6 +279,7 @@ static void publish_cell_voltages(void) {
serializeJson(doc, mqtt_msg, sizeof(mqtt_msg)); serializeJson(doc, mqtt_msg, sizeof(mqtt_msg));
if (mqtt_publish(generateCellVoltageAutoConfigTopic(cellNumber, "_2_").c_str(), mqtt_msg, true) == false) { if (mqtt_publish(generateCellVoltageAutoConfigTopic(cellNumber, "_2_").c_str(), mqtt_msg, true) == false) {
failed_to_publish = true; failed_to_publish = true;
return false;
} }
} }
doc.clear(); // clear after sending autoconfig doc.clear(); // clear after sending autoconfig
@ -286,6 +306,7 @@ static void publish_cell_voltages(void) {
#ifdef DEBUG_LOG #ifdef DEBUG_LOG
logging.println("Cell voltage MQTT msg could not be sent"); logging.println("Cell voltage MQTT msg could not be sent");
#endif // DEBUG_LOG #endif // DEBUG_LOG
return false;
} }
doc.clear(); doc.clear();
} }
@ -306,13 +327,15 @@ static void publish_cell_voltages(void) {
#ifdef DEBUG_LOG #ifdef DEBUG_LOG
logging.println("Cell voltage MQTT msg could not be sent"); logging.println("Cell voltage MQTT msg could not be sent");
#endif // DEBUG_LOG #endif // DEBUG_LOG
return false;
} }
doc.clear(); doc.clear();
} }
#endif // DOUBLE_BATTERY #endif // DOUBLE_BATTERY
return true;
} }
void publish_events() { bool publish_events() {
static JsonDocument doc; static JsonDocument doc;
static String state_topic = topic_name + "/events"; static String state_topic = topic_name + "/events";
#ifdef HA_AUTODISCOVERY #ifdef HA_AUTODISCOVERY
@ -331,6 +354,8 @@ void publish_events() {
serializeJson(doc, mqtt_msg); serializeJson(doc, mqtt_msg);
if (mqtt_publish(generateEventsAutoConfigTopic("event").c_str(), mqtt_msg, true)) { if (mqtt_publish(generateEventsAutoConfigTopic("event").c_str(), mqtt_msg, true)) {
ha_events_published = true; ha_events_published = true;
} else {
return false;
} }
doc.clear(); doc.clear();
@ -368,6 +393,7 @@ void publish_events() {
#ifdef DEBUG_LOG #ifdef DEBUG_LOG
logging.println("Common info MQTT msg could not be sent"); logging.println("Common info MQTT msg could not be sent");
#endif // DEBUG_LOG #endif // DEBUG_LOG
return false;
} else { } else {
set_event_MQTTpublished(event_handle); set_event_MQTTpublished(event_handle);
} }
@ -378,9 +404,10 @@ void publish_events() {
#ifdef HA_AUTODISCOVERY #ifdef HA_AUTODISCOVERY
} }
#endif // HA_AUTODISCOVERY #endif // HA_AUTODISCOVERY
return true;
} }
static void publish_buttons_discovery(void) { static bool publish_buttons_discovery(void) {
#ifdef HA_AUTODISCOVERY #ifdef HA_AUTODISCOVERY
if (ha_buttons_published == false) { if (ha_buttons_published == false) {
#ifdef DEBUG_LOG #ifdef DEBUG_LOG
@ -397,11 +424,14 @@ static void publish_buttons_discovery(void) {
serializeJson(doc, mqtt_msg); serializeJson(doc, mqtt_msg);
if (mqtt_publish(generateButtonAutoConfigTopic(config.object_id).c_str(), mqtt_msg, true)) { if (mqtt_publish(generateButtonAutoConfigTopic(config.object_id).c_str(), mqtt_msg, true)) {
ha_buttons_published = true; ha_buttons_published = true;
} else {
return false;
} }
doc.clear(); doc.clear();
} }
} }
#endif // HA_AUTODISCOVERY #endif // HA_AUTODISCOVERY
return true;
} }
static void subscribe() { static void subscribe() {
@ -511,6 +541,7 @@ void init_mqtt(void) {
mqtt_cfg.session.last_will.retain = true; mqtt_cfg.session.last_will.retain = true;
mqtt_cfg.session.last_will.msg = "offline"; mqtt_cfg.session.last_will.msg = "offline";
mqtt_cfg.session.last_will.msg_len = strlen(mqtt_cfg.session.last_will.msg); mqtt_cfg.session.last_will.msg_len = strlen(mqtt_cfg.session.last_will.msg);
mqtt_cfg.network.timeout_ms = MQTT_TIMEOUT;
client = esp_mqtt_client_init(&mqtt_cfg); client = esp_mqtt_client_init(&mqtt_cfg);
esp_mqtt_client_register_event(client, MQTT_EVENT_ANY, mqtt_event_handler, client); esp_mqtt_client_register_event(client, MQTT_EVENT_ANY, mqtt_event_handler, client);
} }