From 3eac3e32eb1e6a56e6402f8c7023703b7b43d8da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Mu=CC=88ller?= Date: Mon, 11 Nov 2024 20:27:30 +0100 Subject: [PATCH 1/8] added publish functions --- src/MqttClient.cpp | 22 ++++++++++++++++++++++ src/MqttClient.h | 4 ++++ 2 files changed, 26 insertions(+) diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index 221b230..060dd13 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -259,6 +259,28 @@ int MqttClient::endMessage() return 1; } +int MqttClient::publish(const char* topic, const char* payload, bool retain, uint8_t qos, bool dup) { + int ret = beginMessage(topic, strlen_P(payload), retain, qos, dup); + if (!ret) { + return ret; + } + print(payload); + ret = endMessage(); + return ret; +} + +int MqttClient::publish(const String& topic, const char* payload, bool retain, uint8_t qos, bool dup) { + publish(topic.c_str(), payload, retain, qos, dup); +} + +int MqttClient::publish(const const char* topic, String& payload, bool retain, uint8_t qos, bool dup) { + publish(topic, payload.c_str(), retain, qos, dup); +} + +int MqttClient::publish(const String& topic, String& payload, bool retain, uint8_t qos, bool dup){ + publish(topic.c_str(), payload.c_str(), retain, qos, dup); +} + int MqttClient::beginWill(const char* topic, unsigned short size, bool retain, uint8_t qos) { int topicLength = strlen(topic); diff --git a/src/MqttClient.h b/src/MqttClient.h index 522f023..b08d71b 100644 --- a/src/MqttClient.h +++ b/src/MqttClient.h @@ -64,6 +64,10 @@ class MqttClient : public Client { int beginMessage(const char* topic, bool retain = false, uint8_t qos = 0, bool dup = false); int beginMessage(const String& topic, bool retain = false, uint8_t qos = 0, bool dup = false); int endMessage(); + int publish(const char* topic, const char* payload, bool retain = false, uint8_t qos = 0, bool dup = false); + int publish(const String& topic, const char* payload, bool retain = false, uint8_t qos = 0, bool dup = false); + int publish(const const char* topic, String& payload, bool retain = false, uint8_t qos = 0, bool dup = false); + int publish(const String& topic, String& payload, bool retain = false, uint8_t qos = 0, bool dup = false); int beginWill(const char* topic, unsigned short size, bool retain, uint8_t qos); int beginWill(const String& topic, unsigned short size, bool retain, uint8_t qos); From e26da8abb082c8f82b52556763e4c0cf4ad12573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Mu=CC=88ller?= Date: Mon, 11 Nov 2024 20:43:28 +0100 Subject: [PATCH 2/8] fix const const --- src/MqttClient.cpp | 2 +- src/MqttClient.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index 060dd13..8bb0644 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -273,7 +273,7 @@ int MqttClient::publish(const String& topic, const char* payload, bool retain, u publish(topic.c_str(), payload, retain, qos, dup); } -int MqttClient::publish(const const char* topic, String& payload, bool retain, uint8_t qos, bool dup) { +int MqttClient::publish(const char* topic, String& payload, bool retain, uint8_t qos, bool dup) { publish(topic, payload.c_str(), retain, qos, dup); } diff --git a/src/MqttClient.h b/src/MqttClient.h index b08d71b..c7a8919 100644 --- a/src/MqttClient.h +++ b/src/MqttClient.h @@ -66,7 +66,7 @@ class MqttClient : public Client { int endMessage(); int publish(const char* topic, const char* payload, bool retain = false, uint8_t qos = 0, bool dup = false); int publish(const String& topic, const char* payload, bool retain = false, uint8_t qos = 0, bool dup = false); - int publish(const const char* topic, String& payload, bool retain = false, uint8_t qos = 0, bool dup = false); + int publish(const char* topic, String& payload, bool retain = false, uint8_t qos = 0, bool dup = false); int publish(const String& topic, String& payload, bool retain = false, uint8_t qos = 0, bool dup = false); int beginWill(const char* topic, unsigned short size, bool retain, uint8_t qos); From 41251529634eee6de137c81c69a2b88e57edaede Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Mu=CC=88ller?= Date: Mon, 11 Nov 2024 20:47:21 +0100 Subject: [PATCH 3/8] fix missing return --- src/MqttClient.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index 8bb0644..6aa263b 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -270,15 +270,15 @@ int MqttClient::publish(const char* topic, const char* payload, bool retain, uin } int MqttClient::publish(const String& topic, const char* payload, bool retain, uint8_t qos, bool dup) { - publish(topic.c_str(), payload, retain, qos, dup); + return publish(topic.c_str(), payload, retain, qos, dup); } int MqttClient::publish(const char* topic, String& payload, bool retain, uint8_t qos, bool dup) { - publish(topic, payload.c_str(), retain, qos, dup); + return publish(topic, payload.c_str(), retain, qos, dup); } -int MqttClient::publish(const String& topic, String& payload, bool retain, uint8_t qos, bool dup){ - publish(topic.c_str(), payload.c_str(), retain, qos, dup); +int MqttClient::publish(const String& topic, String& payload, bool retain, uint8_t qos, bool dup) { + return publish(topic.c_str(), payload.c_str(), retain, qos, dup); } int MqttClient::beginWill(const char* topic, unsigned short size, bool retain, uint8_t qos) From 9af832b90f43976bcca6e190f56b38a166dba7c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Mu=CC=88ller?= Date: Sun, 17 Nov 2024 21:30:09 +0100 Subject: [PATCH 4/8] added documentation to publish functions --- src/MqttClient.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index 6aa263b..ff0fb58 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -259,6 +259,17 @@ int MqttClient::endMessage() return 1; } +/** + * @brief Pubish a MQTT messages to the broker. + * + * @param topic The topic to publish to. + * @param payload The payload to publish. + * @param retain Publish the MQTT message with the retain flag. Default false. + * @param qos The Quality of Service of the MQTT message. [0 .. 2]. Default 0. + * @param dup Set or reset the duplicate flag of MQTT message. Default false (reset). + * @return 0 - Failed to send message. + * 1 - Successfully send message. + */ int MqttClient::publish(const char* topic, const char* payload, bool retain, uint8_t qos, bool dup) { int ret = beginMessage(topic, strlen_P(payload), retain, qos, dup); if (!ret) { @@ -269,14 +280,47 @@ int MqttClient::publish(const char* topic, const char* payload, bool retain, uin return ret; } +/** + * @brief Pubish a MQTT messages to the broker. + * + * @param topic The topic to publish to. + * @param payload The payload to publish. + * @param retain Publish the MQTT message with the retain flag. Default false. + * @param qos The Quality of Service of the MQTT message. [0 .. 2]. Default 0. + * @param dup Set or reset the duplicate flag of MQTT message. Default false (reset). + * @return 0 - Failed to send message. + * 1 - Successfully send message. + */ int MqttClient::publish(const String& topic, const char* payload, bool retain, uint8_t qos, bool dup) { return publish(topic.c_str(), payload, retain, qos, dup); } +/** + * @brief Pubish a MQTT messages to the broker. + * + * @param topic The topic to publish to. + * @param payload The payload to publish. + * @param retain Publish the MQTT message with the retain flag. Default false. + * @param qos The Quality of Service of the MQTT message. [0 .. 2]. Default 0. + * @param dup Set or reset the duplicate flag of MQTT message. Default false (reset). + * @return 0 - Failed to send message. + * 1 - Successfully send message. + */ int MqttClient::publish(const char* topic, String& payload, bool retain, uint8_t qos, bool dup) { return publish(topic, payload.c_str(), retain, qos, dup); } +/** + * @brief Pubish a MQTT messages to the broker. + * + * @param topic The topic to publish to. + * @param payload The payload to publish. + * @param retain Publish the MQTT message with the retain flag. Default false. + * @param qos The Quality of Service of the MQTT message. [0 .. 2]. Default 0. + * @param dup Set or reset the duplicate flag of MQTT message. Default false (reset). + * @return 0 - Failed to send message. + * 1 - Successfully send message. + */ int MqttClient::publish(const String& topic, String& payload, bool retain, uint8_t qos, bool dup) { return publish(topic.c_str(), payload.c_str(), retain, qos, dup); } From a47d75107e7f53ea170c98f1dbb0913c43429c4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Mu=CC=88ller?= Date: Sun, 17 Nov 2024 21:32:48 +0100 Subject: [PATCH 5/8] fixed misspelling --- src/MqttClient.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index ff0fb58..d7ffec0 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -260,7 +260,7 @@ int MqttClient::endMessage() } /** - * @brief Pubish a MQTT messages to the broker. + * @brief Publish a MQTT messages to the broker. * * @param topic The topic to publish to. * @param payload The payload to publish. @@ -281,7 +281,7 @@ int MqttClient::publish(const char* topic, const char* payload, bool retain, uin } /** - * @brief Pubish a MQTT messages to the broker. + * @brief Publish a MQTT messages to the broker. * * @param topic The topic to publish to. * @param payload The payload to publish. @@ -296,7 +296,7 @@ int MqttClient::publish(const String& topic, const char* payload, bool retain, u } /** - * @brief Pubish a MQTT messages to the broker. + * @brief Publish a MQTT messages to the broker. * * @param topic The topic to publish to. * @param payload The payload to publish. @@ -311,7 +311,7 @@ int MqttClient::publish(const char* topic, String& payload, bool retain, uint8_t } /** - * @brief Pubish a MQTT messages to the broker. + * @brief Publish a MQTT messages to the broker. * * @param topic The topic to publish to. * @param payload The payload to publish. From de886e7cd8ec9898b76670889814e92708279334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Mu=CC=88ller?= Date: Sun, 17 Nov 2024 21:33:41 +0100 Subject: [PATCH 6/8] fixed misspelling --- src/MqttClient.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index d7ffec0..1828909 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -260,7 +260,7 @@ int MqttClient::endMessage() } /** - * @brief Publish a MQTT messages to the broker. + * @brief Publish a MQTT message to the broker. * * @param topic The topic to publish to. * @param payload The payload to publish. @@ -281,7 +281,7 @@ int MqttClient::publish(const char* topic, const char* payload, bool retain, uin } /** - * @brief Publish a MQTT messages to the broker. + * @brief Publish a MQTT message to the broker. * * @param topic The topic to publish to. * @param payload The payload to publish. @@ -296,7 +296,7 @@ int MqttClient::publish(const String& topic, const char* payload, bool retain, u } /** - * @brief Publish a MQTT messages to the broker. + * @brief Publish a MQTT message to the broker. * * @param topic The topic to publish to. * @param payload The payload to publish. @@ -311,7 +311,7 @@ int MqttClient::publish(const char* topic, String& payload, bool retain, uint8_t } /** - * @brief Publish a MQTT messages to the broker. + * @brief Publish a MQTT message to the broker. * * @param topic The topic to publish to. * @param payload The payload to publish. From a6212f5dc23a44976e9b62d1b6e1f2cc44005014 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Mu=CC=88ller?= Date: Sun, 17 Nov 2024 21:35:30 +0100 Subject: [PATCH 7/8] fixed misspelling --- src/MqttClient.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index 1828909..b4092d7 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -265,8 +265,8 @@ int MqttClient::endMessage() * @param topic The topic to publish to. * @param payload The payload to publish. * @param retain Publish the MQTT message with the retain flag. Default false. - * @param qos The Quality of Service of the MQTT message. [0 .. 2]. Default 0. - * @param dup Set or reset the duplicate flag of MQTT message. Default false (reset). + * @param qos The quality of service of the MQTT message. [0 .. 2]. Default 0. + * @param dup Set or reset the duplicate flag of the MQTT message. Default false (reset). * @return 0 - Failed to send message. * 1 - Successfully send message. */ @@ -286,8 +286,8 @@ int MqttClient::publish(const char* topic, const char* payload, bool retain, uin * @param topic The topic to publish to. * @param payload The payload to publish. * @param retain Publish the MQTT message with the retain flag. Default false. - * @param qos The Quality of Service of the MQTT message. [0 .. 2]. Default 0. - * @param dup Set or reset the duplicate flag of MQTT message. Default false (reset). + * @param qos The quality of service of the MQTT message. [0 .. 2]. Default 0. + * @param dup Set or reset the duplicate flag of the MQTT message. Default false (reset). * @return 0 - Failed to send message. * 1 - Successfully send message. */ @@ -301,8 +301,8 @@ int MqttClient::publish(const String& topic, const char* payload, bool retain, u * @param topic The topic to publish to. * @param payload The payload to publish. * @param retain Publish the MQTT message with the retain flag. Default false. - * @param qos The Quality of Service of the MQTT message. [0 .. 2]. Default 0. - * @param dup Set or reset the duplicate flag of MQTT message. Default false (reset). + * @param qos The quality of service of the MQTT message. [0 .. 2]. Default 0. + * @param dup Set or reset the duplicate flag of the MQTT message. Default false (reset). * @return 0 - Failed to send message. * 1 - Successfully send message. */ @@ -316,8 +316,8 @@ int MqttClient::publish(const char* topic, String& payload, bool retain, uint8_t * @param topic The topic to publish to. * @param payload The payload to publish. * @param retain Publish the MQTT message with the retain flag. Default false. - * @param qos The Quality of Service of the MQTT message. [0 .. 2]. Default 0. - * @param dup Set or reset the duplicate flag of MQTT message. Default false (reset). + * @param qos The quality of service of the MQTT message. [0 .. 2]. Default 0. + * @param dup Set or reset the duplicate flag of the MQTT message. Default false (reset). * @return 0 - Failed to send message. * 1 - Successfully send message. */ From f0ffdd7991f330a49f91f967168c958051c51c34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Mu=CC=88ller?= Date: Sun, 17 Nov 2024 21:39:24 +0100 Subject: [PATCH 8/8] simplify publish function --- src/MqttClient.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index b4092d7..f38db5d 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -272,11 +272,10 @@ int MqttClient::endMessage() */ int MqttClient::publish(const char* topic, const char* payload, bool retain, uint8_t qos, bool dup) { int ret = beginMessage(topic, strlen_P(payload), retain, qos, dup); - if (!ret) { - return ret; + if (ret) { + print(payload); + ret = endMessage(); } - print(payload); - ret = endMessage(); return ret; }