mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 16:29:51 +03:00
show mqtt subscriptions in mqttlog command
This commit is contained in:
@@ -96,7 +96,6 @@ MyESP::MyESP() {
|
|||||||
|
|
||||||
// ntp
|
// ntp
|
||||||
_ntp_server = strdup(MYESP_NTP_SERVER);
|
_ntp_server = strdup(MYESP_NTP_SERVER);
|
||||||
;
|
|
||||||
_ntp_interval = 60;
|
_ntp_interval = 60;
|
||||||
_ntp_enabled = false;
|
_ntp_enabled = false;
|
||||||
|
|
||||||
@@ -105,6 +104,7 @@ MyESP::MyESP() {
|
|||||||
|
|
||||||
// MQTT log
|
// MQTT log
|
||||||
for (uint8_t i = 0; i < MYESP_MQTTLOG_MAX; i++) {
|
for (uint8_t i = 0; i < MYESP_MQTTLOG_MAX; i++) {
|
||||||
|
MQTT_log[i].type = 0;
|
||||||
MQTT_log[i].timestamp = 0;
|
MQTT_log[i].timestamp = 0;
|
||||||
MQTT_log[i].topic = nullptr;
|
MQTT_log[i].topic = nullptr;
|
||||||
MQTT_log[i].payload = nullptr;
|
MQTT_log[i].payload = nullptr;
|
||||||
@@ -353,16 +353,21 @@ void MyESP::_mqttOnMessage(char * topic, char * payload, size_t len) {
|
|||||||
// MQTT subscribe
|
// MQTT subscribe
|
||||||
void MyESP::mqttSubscribe(const char * topic) {
|
void MyESP::mqttSubscribe(const char * topic) {
|
||||||
if (mqttClient.connected() && (strlen(topic) > 0)) {
|
if (mqttClient.connected() && (strlen(topic) > 0)) {
|
||||||
unsigned int packetId = mqttClient.subscribe(_mqttTopic(topic), _mqtt_qos);
|
char topic_s[MQTT_MAX_TOPIC_SIZE];
|
||||||
myDebug_P(PSTR("[MQTT] Subscribing to %s (PID %d)"), _mqttTopic(topic), packetId);
|
strlcpy(topic_s, _mqttTopic(topic), sizeof(topic_s));
|
||||||
|
(void)mqttClient.subscribe(topic_s, _mqtt_qos);
|
||||||
|
myDebug_P(PSTR("[MQTT] Subscribing to %s"), topic_s);
|
||||||
|
|
||||||
|
// add to mqtt log
|
||||||
|
_addMQTTLog(topic_s, "", 2); // type of 2 means Subscribe. Has an empty payload for now XXX
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MQTT unsubscribe
|
// MQTT unsubscribe
|
||||||
void MyESP::mqttUnsubscribe(const char * topic) {
|
void MyESP::mqttUnsubscribe(const char * topic) {
|
||||||
if (mqttClient.connected() && (strlen(topic) > 0)) {
|
if (mqttClient.connected() && (strlen(topic) > 0)) {
|
||||||
unsigned int packetId = mqttClient.unsubscribe(_mqttTopic(topic));
|
(void)mqttClient.unsubscribe(_mqttTopic(topic));
|
||||||
myDebug_P(PSTR("[MQTT] Unsubscribing to %s (PID %d)"), _mqttTopic(topic), packetId);
|
myDebug_P(PSTR("[MQTT] Unsubscribing to %s"), _mqttTopic(topic));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -371,7 +376,7 @@ void MyESP::mqttPublish(const char * topic, const char * payload) {
|
|||||||
// myDebug_P(PSTR("[MQTT] Sending pubish to %s with payload %s"), _mqttTopic(topic), payload); // for debugging
|
// myDebug_P(PSTR("[MQTT] Sending pubish to %s with payload %s"), _mqttTopic(topic), payload); // for debugging
|
||||||
mqttClient.publish(_mqttTopic(topic), _mqtt_qos, _mqtt_retain, payload);
|
mqttClient.publish(_mqttTopic(topic), _mqtt_qos, _mqtt_retain, payload);
|
||||||
|
|
||||||
_addMQTTLog(topic, payload); // add to the log
|
_addMQTTLog(topic, payload, 1); // add to the log, using type of 1 for Publish
|
||||||
}
|
}
|
||||||
|
|
||||||
// MQTT onConnect - when a connect is established
|
// MQTT onConnect - when a connect is established
|
||||||
@@ -626,7 +631,7 @@ void MyESP::_consoleShowHelp() {
|
|||||||
// see if a char * string is empty. It could not be initialized yet.
|
// see if a char * string is empty. It could not be initialized yet.
|
||||||
// return true if there is a value
|
// return true if there is a value
|
||||||
bool MyESP::_hasValue(char * s) {
|
bool MyESP::_hasValue(char * s) {
|
||||||
if (s == nullptr) {
|
if ((s == nullptr) || (strlen(s) == 0)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return (s[0] != '\0');
|
return (s[0] != '\0');
|
||||||
@@ -2464,8 +2469,9 @@ void MyESP::_sendStatus() {
|
|||||||
// create MQTT log
|
// create MQTT log
|
||||||
JsonArray list = root.createNestedArray("mqttlog");
|
JsonArray list = root.createNestedArray("mqttlog");
|
||||||
|
|
||||||
|
// only send Publish
|
||||||
for (uint8_t i = 0; i < MYESP_MQTTLOG_MAX; i++) {
|
for (uint8_t i = 0; i < MYESP_MQTTLOG_MAX; i++) {
|
||||||
if (MQTT_log[i].topic != nullptr) {
|
if ((MQTT_log[i].type == 1) && (MQTT_log[i].topic != nullptr)) {
|
||||||
JsonObject item = list.createNestedObject();
|
JsonObject item = list.createNestedObject();
|
||||||
item["topic"] = MQTT_log[i].topic;
|
item["topic"] = MQTT_log[i].topic;
|
||||||
item["payload"] = MQTT_log[i].payload;
|
item["payload"] = MQTT_log[i].payload;
|
||||||
@@ -2639,10 +2645,24 @@ void MyESP::_printHeap(const char * s) {
|
|||||||
// print MQTT log - everything that was published last per topic
|
// print MQTT log - everything that was published last per topic
|
||||||
void MyESP::_printMQTTLog() {
|
void MyESP::_printMQTTLog() {
|
||||||
myDebug_P(PSTR("MQTT publish log:"));
|
myDebug_P(PSTR("MQTT publish log:"));
|
||||||
|
uint8_t i;
|
||||||
|
|
||||||
for (uint8_t i = 0; i < MYESP_MQTTLOG_MAX; i++) {
|
for (i = 0; i < MYESP_MQTTLOG_MAX; i++) {
|
||||||
if (MQTT_log[i].topic != nullptr) {
|
if ((MQTT_log[i].topic != nullptr) && (MQTT_log[i].type == 1)) {
|
||||||
myDebug_P(PSTR("(%d) [%lu] Topic:%s Payload:%s"), i, MQTT_log[i].timestamp, MQTT_log[i].topic, MQTT_log[i].payload);
|
myDebug_P(PSTR(" Topic:%s Payload:%s"), MQTT_log[i].topic, MQTT_log[i].payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
myDebug_P(PSTR("")); // newline
|
||||||
|
myDebug_P(PSTR("MQTT subscribe log:"));
|
||||||
|
|
||||||
|
for (i = 0; i < MYESP_MQTTLOG_MAX; i++) {
|
||||||
|
if ((MQTT_log[i].topic != nullptr) && (MQTT_log[i].type == 2)) {
|
||||||
|
if (_hasValue(MQTT_log[i].payload)) {
|
||||||
|
myDebug_P(PSTR(" Topic:%s Last Payload:%s"), MQTT_log[i].topic, MQTT_log[i].payload);
|
||||||
|
} else {
|
||||||
|
myDebug_P(PSTR(" Topic:%s"), MQTT_log[i].topic);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2650,16 +2670,18 @@ void MyESP::_printMQTTLog() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add an MQTT log entry to our buffer
|
// add an MQTT log entry to our buffer
|
||||||
void MyESP::_addMQTTLog(const char * topic, const char * payload) {
|
// type 0=none, 1=publish, 2=subscribe
|
||||||
|
void MyESP::_addMQTTLog(const char * topic, const char * payload, const uint8_t type) {
|
||||||
static uint8_t logCount = 0;
|
static uint8_t logCount = 0;
|
||||||
uint8_t logPointer = 0;
|
uint8_t logPointer = 0;
|
||||||
bool found = false;
|
bool found = false;
|
||||||
|
|
||||||
// myDebug("Publish [#%d] %s (%d) %s (%d)", logCount, topic, strlen(topic), payload, strlen(payload)); // for debugging
|
// myDebug("_addMQTTLog [#%d] %s (%d) [%s] (%d)", logCount, topic, strlen(topic), payload, strlen(payload)); // for debugging
|
||||||
|
|
||||||
// find the topic
|
// find the topic
|
||||||
|
// topics must be unique for either publish or subscribe
|
||||||
while ((logPointer < MYESP_MQTTLOG_MAX) && (_hasValue(MQTT_log[logPointer].topic))) {
|
while ((logPointer < MYESP_MQTTLOG_MAX) && (_hasValue(MQTT_log[logPointer].topic))) {
|
||||||
if (strcmp(MQTT_log[logPointer].topic, topic) == 0) {
|
if ((strcmp(MQTT_log[logPointer].topic, topic) == 0) && (MQTT_log[logPointer].type == type)) {
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -2678,12 +2700,12 @@ void MyESP::_addMQTTLog(const char * topic, const char * payload) {
|
|||||||
if (MQTT_log[logPointer].topic) {
|
if (MQTT_log[logPointer].topic) {
|
||||||
free(MQTT_log[logPointer].topic);
|
free(MQTT_log[logPointer].topic);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MQTT_log[logPointer].payload) {
|
if (MQTT_log[logPointer].payload) {
|
||||||
free(MQTT_log[logPointer].payload);
|
free(MQTT_log[logPointer].payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
// add new record
|
// and add new record
|
||||||
|
MQTT_log[logPointer].type = type; // 0=none, 1=publish, 2=subscribe
|
||||||
MQTT_log[logPointer].topic = strdup(topic);
|
MQTT_log[logPointer].topic = strdup(topic);
|
||||||
MQTT_log[logPointer].payload = strdup(payload);
|
MQTT_log[logPointer].payload = strdup(payload);
|
||||||
MQTT_log[logPointer].timestamp = now();
|
MQTT_log[logPointer].timestamp = now();
|
||||||
|
|||||||
10
src/MyESP.h
10
src/MyESP.h
@@ -9,7 +9,7 @@
|
|||||||
#ifndef MyESP_h
|
#ifndef MyESP_h
|
||||||
#define MyESP_h
|
#define MyESP_h
|
||||||
|
|
||||||
#define MYESP_VERSION "1.2.0"
|
#define MYESP_VERSION "1.2.1"
|
||||||
|
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <ArduinoOTA.h>
|
#include <ArduinoOTA.h>
|
||||||
@@ -87,7 +87,7 @@ extern struct rst_info resetInfo;
|
|||||||
#define MQTT_MAX_PAYLOAD_SIZE 500 // max size of a JSON object. See https://arduinojson.org/v6/assistant/
|
#define MQTT_MAX_PAYLOAD_SIZE 500 // 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_JSON_MAXSIZE 2000 // for large Dynamic json files
|
||||||
#define MYESP_MQTTLOG_MAX 20 // max number of log entries for MQTT publishes
|
#define MYESP_MQTTLOG_MAX 40 // 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_JSON_LOG_MAXSIZE 300 // max size of an JSON log entry
|
||||||
|
|
||||||
// Internal MQTT events
|
// Internal MQTT events
|
||||||
@@ -212,6 +212,7 @@ typedef enum {
|
|||||||
|
|
||||||
// for storing all MQTT publish messages
|
// for storing all MQTT publish messages
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
uint8_t type; // 0=none, 1=publish, 2=subscribe
|
||||||
char * topic;
|
char * topic;
|
||||||
char * payload;
|
char * payload;
|
||||||
time_t timestamp;
|
time_t timestamp;
|
||||||
@@ -317,9 +318,10 @@ class MyESP {
|
|||||||
char * _mqttTopic(const char * topic);
|
char * _mqttTopic(const char * topic);
|
||||||
|
|
||||||
// mqtt log
|
// mqtt log
|
||||||
_MQTT_Log MQTT_log[MYESP_MQTTLOG_MAX]; // log for publish messages
|
_MQTT_Log MQTT_log[MYESP_MQTTLOG_MAX]; // log for publish and subscribe messages
|
||||||
|
|
||||||
void _printMQTTLog();
|
void _printMQTTLog();
|
||||||
void _addMQTTLog(const char * topic, const char * payload);
|
void _addMQTTLog(const char * topic, const char * payload, const uint8_t type);
|
||||||
|
|
||||||
AsyncMqttClient mqttClient; // the MQTT class
|
AsyncMqttClient mqttClient; // the MQTT class
|
||||||
uint32_t _mqtt_reconnect_delay;
|
uint32_t _mqtt_reconnect_delay;
|
||||||
|
|||||||
Reference in New Issue
Block a user