mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 08:19:52 +03:00
retry publish, count #mqtt fails in system cmd - #264
This commit is contained in:
@@ -78,6 +78,7 @@ MyESP::MyESP() {
|
|||||||
_mqtt_will_topic = strdup(MQTT_WILL_TOPIC);
|
_mqtt_will_topic = strdup(MQTT_WILL_TOPIC);
|
||||||
_mqtt_will_online_payload = strdup(MQTT_WILL_ONLINE_PAYLOAD);
|
_mqtt_will_online_payload = strdup(MQTT_WILL_ONLINE_PAYLOAD);
|
||||||
_mqtt_will_offline_payload = strdup(MQTT_WILL_OFFLINE_PAYLOAD);
|
_mqtt_will_offline_payload = strdup(MQTT_WILL_OFFLINE_PAYLOAD);
|
||||||
|
_mqtt_publish_fails = 0; // count of number of failed MQTT topic publishes
|
||||||
|
|
||||||
// network
|
// network
|
||||||
_network_password = nullptr;
|
_network_password = nullptr;
|
||||||
@@ -425,9 +426,19 @@ bool MyESP::mqttPublish(const char * topic, const char * payload, bool retain) {
|
|||||||
if (packet_id) {
|
if (packet_id) {
|
||||||
_addMQTTLog(topic, payload, MYESP_MQTTLOGTYPE_PUBLISH); // add to the log
|
_addMQTTLog(topic, payload, MYESP_MQTTLOGTYPE_PUBLISH); // add to the log
|
||||||
return true;
|
return true;
|
||||||
} else {
|
|
||||||
myDebug_P(PSTR("[MQTT] Error publishing to %s with payload %s [error %d]"), _mqttTopic(topic), payload, packet_id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// it failed, try again https://github.com/proddy/EMS-ESP/issues/264
|
||||||
|
delay(100); // this is blocking and probably not a good idea
|
||||||
|
packet_id = mqttClient.publish(_mqttTopic(topic), _mqtt_qos, retain, payload);
|
||||||
|
if (packet_id) {
|
||||||
|
_addMQTTLog(topic, payload, MYESP_MQTTLOGTYPE_PUBLISH); // add to the log
|
||||||
|
return true; // ok this time
|
||||||
|
}
|
||||||
|
|
||||||
|
// it didn't work again, will return false
|
||||||
|
myDebug_P(PSTR("[MQTT] Error publishing to %s with payload %s [error %d]"), _mqttTopic(topic), payload, packet_id);
|
||||||
|
_mqtt_publish_fails++; // increment failure counter
|
||||||
}
|
}
|
||||||
|
|
||||||
return false; // failed
|
return false; // failed
|
||||||
@@ -1372,6 +1383,7 @@ void MyESP::showSystemStats() {
|
|||||||
|
|
||||||
if (isMQTTConnected()) {
|
if (isMQTTConnected()) {
|
||||||
myDebug_P(PSTR(" [MQTT] is connected (heartbeat %s)"), getHeartbeat() ? "enabled" : "disabled");
|
myDebug_P(PSTR(" [MQTT] is connected (heartbeat %s)"), getHeartbeat() ? "enabled" : "disabled");
|
||||||
|
myDebug_P(PSTR(" [MQTT] # failed topic publishes: %d"), _mqtt_publish_fails);
|
||||||
} else {
|
} else {
|
||||||
myDebug_P(PSTR(" [MQTT] is disconnected"));
|
myDebug_P(PSTR(" [MQTT] is disconnected"));
|
||||||
}
|
}
|
||||||
|
|||||||
13
src/MyESP.h
13
src/MyESP.h
@@ -335,12 +335,12 @@ class MyESP {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
// mqtt
|
// mqtt
|
||||||
void _mqttOnMessage(char * topic, char * payload, size_t len);
|
void _mqttOnMessage(char * topic, char * payload, size_t len);
|
||||||
void _mqttConnect();
|
void _mqttConnect();
|
||||||
void _mqtt_setup();
|
void _mqtt_setup();
|
||||||
void _mqttOnConnect();
|
void _mqttOnConnect();
|
||||||
void _sendStart();
|
void _sendStart();
|
||||||
char * _mqttTopic(const char * topic);
|
char * _mqttTopic(const char * topic);
|
||||||
|
|
||||||
// mqtt log
|
// mqtt log
|
||||||
_MQTT_Log_t MQTT_log[MYESP_MQTTLOG_MAX]; // log for publish and subscribe messages
|
_MQTT_Log_t MQTT_log[MYESP_MQTTLOG_MAX]; // log for publish and subscribe messages
|
||||||
@@ -366,6 +366,7 @@ class MyESP {
|
|||||||
uint32_t _mqtt_last_connection;
|
uint32_t _mqtt_last_connection;
|
||||||
bool _mqtt_connecting;
|
bool _mqtt_connecting;
|
||||||
bool _mqtt_heartbeat;
|
bool _mqtt_heartbeat;
|
||||||
|
uint16_t _mqtt_publish_fails;
|
||||||
|
|
||||||
// wifi
|
// wifi
|
||||||
void _wifiCallback(justwifi_messages_t code, char * parameter);
|
void _wifiCallback(justwifi_messages_t code, char * parameter);
|
||||||
|
|||||||
Reference in New Issue
Block a user