mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 16:29:51 +03:00
MQTT error handling on Publish and Subscribes
This commit is contained in:
@@ -365,7 +365,8 @@ void MyESP::_mqttOnMessage(char * topic, char * payload, size_t len) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MQTT subscribe
|
// MQTT subscribe
|
||||||
void MyESP::mqttSubscribe(const char * topic) {
|
// returns false if failed
|
||||||
|
bool MyESP::mqttSubscribe(const char * topic) {
|
||||||
if (mqttClient.connected() && (strlen(topic) > 0)) {
|
if (mqttClient.connected() && (strlen(topic) > 0)) {
|
||||||
char * topic_s = _mqttTopic(topic);
|
char * topic_s = _mqttTopic(topic);
|
||||||
|
|
||||||
@@ -375,8 +376,10 @@ void MyESP::mqttSubscribe(const char * topic) {
|
|||||||
if (packet_id) {
|
if (packet_id) {
|
||||||
// add to mqtt log
|
// add to mqtt log
|
||||||
_addMQTTLog(topic_s, "", 2); // type of 2 means Subscribe. Has an empty payload for now
|
_addMQTTLog(topic_s, "", 2); // type of 2 means Subscribe. Has an empty payload for now
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
myDebug_P(PSTR("[MQTT] Error subscribing to %s, error %d"), _mqttTopic(topic), packet_id);
|
myDebug_P(PSTR("[MQTT] Error subscribing to %s, error %d"), _mqttTopic(topic), packet_id);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -390,15 +393,18 @@ void MyESP::mqttUnsubscribe(const char * topic) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MQTT Publish
|
// MQTT Publish
|
||||||
void MyESP::mqttPublish(const char * topic, const char * payload) {
|
// returns true if all good
|
||||||
|
bool MyESP::mqttPublish(const char * topic, const char * payload) {
|
||||||
if (mqttClient.connected() && (strlen(topic) > 0)) {
|
if (mqttClient.connected() && (strlen(topic) > 0)) {
|
||||||
// myDebug_P(PSTR("[MQTT] Sending publish to %s with payload %s"), _mqttTopic(topic), payload); // for debugging
|
// myDebug_P(PSTR("[MQTT] Sending publish to %s with payload %s"), _mqttTopic(topic), payload); // for debugging
|
||||||
uint16_t packet_id = mqttClient.publish(_mqttTopic(topic), _mqtt_qos, _mqtt_retain, payload);
|
uint16_t packet_id = mqttClient.publish(_mqttTopic(topic), _mqtt_qos, _mqtt_retain, payload);
|
||||||
|
|
||||||
if (packet_id) {
|
if (packet_id) {
|
||||||
_addMQTTLog(topic, payload, 1); // add to the log, using type of 1 for Publish
|
_addMQTTLog(topic, payload, 1); // add to the log, using type of 1 for Publish
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
myDebug_P(PSTR("[MQTT] Error publishing to %s with payload %s, error %d"), _mqttTopic(topic), payload, packet_id);
|
myDebug_P(PSTR("[MQTT] Error publishing to %s with payload %s, error %d"), _mqttTopic(topic), payload, packet_id);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -827,10 +833,6 @@ bool MyESP::_changeSetting(uint8_t wc, const char * setting, const char * value)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (restart) {
|
|
||||||
myDebug_P(PSTR("Please 'restart' to apply new settings."));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ok = false;
|
bool ok = false;
|
||||||
|
|
||||||
// if we were able to recognize the set command, continue
|
// if we were able to recognize the set command, continue
|
||||||
@@ -854,6 +856,10 @@ bool MyESP::_changeSetting(uint8_t wc, const char * setting, const char * value)
|
|||||||
ok = _fs_createCustomConfig();
|
ok = _fs_createCustomConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (restart) {
|
||||||
|
myDebug_P(PSTR("Please 'restart' to apply new settings."));
|
||||||
|
}
|
||||||
|
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1800,7 +1806,7 @@ bool MyESP::fs_saveCustomConfig(JsonObject root) {
|
|||||||
_writeEvent("INFO", "system", "Custom config stored in the SPIFFS", "");
|
_writeEvent("INFO", "system", "Custom config stored in the SPIFFS", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
myDebug_P(PSTR("[FS] custom config saved"));
|
// myDebug_P(PSTR("[FS] custom config saved"));
|
||||||
ok = true;
|
ok = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1835,7 +1841,7 @@ bool MyESP::fs_saveConfig(JsonObject root) {
|
|||||||
if (_general_log_events) {
|
if (_general_log_events) {
|
||||||
_writeEvent("INFO", "system", "System config stored in the SPIFFS", "");
|
_writeEvent("INFO", "system", "System config stored in the SPIFFS", "");
|
||||||
}
|
}
|
||||||
myDebug_P(PSTR("[FS] system config saved"));
|
// myDebug_P(PSTR("[FS] system config saved"));
|
||||||
ok = true;
|
ok = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
16
src/MyESP.h
16
src/MyESP.h
@@ -9,7 +9,7 @@
|
|||||||
#ifndef MyESP_h
|
#ifndef MyESP_h
|
||||||
#define MyESP_h
|
#define MyESP_h
|
||||||
|
|
||||||
#define MYESP_VERSION "1.2.9"
|
#define MYESP_VERSION "1.2.10"
|
||||||
|
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <ArduinoOTA.h>
|
#include <ArduinoOTA.h>
|
||||||
@@ -86,15 +86,19 @@ extern struct rst_info resetInfo;
|
|||||||
#define MQTT_MAX_TOPIC_SIZE 50 // max length of MQTT topic
|
#define MQTT_MAX_TOPIC_SIZE 50 // max length of MQTT topic
|
||||||
#define MQTT_MAX_PAYLOAD_SIZE 700 // max size of a JSON object. See https://arduinojson.org/v6/assistant/
|
#define MQTT_MAX_PAYLOAD_SIZE 700 // max size of a JSON object. See https://arduinojson.org/v6/assistant/
|
||||||
#define MQTT_MAX_PAYLOAD_SIZE_LARGE 2000 // max size of a large JSON object, like for sending MQTT log
|
#define MQTT_MAX_PAYLOAD_SIZE_LARGE 2000 // max size of a large JSON object, like for sending MQTT log
|
||||||
#define MYESP_JSON_MAXSIZE 2000 // for large Dynamic json files
|
|
||||||
#define MYESP_MQTTLOG_MAX 60 // max number of log entries for MQTT publishes and subscribes
|
|
||||||
#define MYESP_JSON_LOG_MAXSIZE 300 // max size of an JSON log entry
|
|
||||||
|
|
||||||
// Internal MQTT events
|
// Internal MQTT events
|
||||||
#define MQTT_CONNECT_EVENT 0
|
#define MQTT_CONNECT_EVENT 0
|
||||||
#define MQTT_DISCONNECT_EVENT 1
|
#define MQTT_DISCONNECT_EVENT 1
|
||||||
#define MQTT_MESSAGE_EVENT 2
|
#define MQTT_MESSAGE_EVENT 2
|
||||||
|
|
||||||
|
#define MYESP_JSON_MAXSIZE 2000 // for large Dynamic json files
|
||||||
|
#define MYESP_MQTTLOG_MAX 60 // max number of log entries for MQTT publishes and subscribes
|
||||||
|
#define MYESP_JSON_LOG_MAXSIZE 300 // max size of an JSON log entry
|
||||||
|
|
||||||
|
#define MYESP_MQTT_PAYLOAD_ON '1' // for MQTT switch on
|
||||||
|
#define MYESP_MQTT_PAYLOAD_OFF '0' // for MQTT switch off
|
||||||
|
|
||||||
// Telnet
|
// Telnet
|
||||||
#define TELNET_SERIAL_BAUD 115200
|
#define TELNET_SERIAL_BAUD 115200
|
||||||
#define TELNET_MAX_COMMAND_LENGTH 80 // length of a command
|
#define TELNET_MAX_COMMAND_LENGTH 80 // length of a command
|
||||||
@@ -268,9 +272,9 @@ class MyESP {
|
|||||||
|
|
||||||
// mqtt
|
// mqtt
|
||||||
bool isMQTTConnected();
|
bool isMQTTConnected();
|
||||||
void mqttSubscribe(const char * topic);
|
bool mqttSubscribe(const char * topic);
|
||||||
void mqttUnsubscribe(const char * topic);
|
void mqttUnsubscribe(const char * topic);
|
||||||
void mqttPublish(const char * topic, const char * payload);
|
bool mqttPublish(const char * topic, const char * payload);
|
||||||
void setMQTT(mqtt_callback_f callback);
|
void setMQTT(mqtt_callback_f callback);
|
||||||
|
|
||||||
// OTA
|
// OTA
|
||||||
|
|||||||
Reference in New Issue
Block a user