From a1e64c039eff1f0c14c6bda404efcf39bd133cd5 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 18 May 2021 16:41:26 -0400 Subject: [PATCH 1/3] settable keepalive interval --- Adafruit_MQTT.cpp | 27 +++++++++++++++++++++++++-- Adafruit_MQTT.h | 4 ++++ library.properties | 4 ++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Adafruit_MQTT.cpp b/Adafruit_MQTT.cpp index a38ec80..d9973cd 100644 --- a/Adafruit_MQTT.cpp +++ b/Adafruit_MQTT.cpp @@ -132,6 +132,8 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port, const char *cid, will_qos = 0; will_retain = 0; + keepAliveInterval = MQTT_CONN_KEEPALIVE; + packet_id_counter = 0; } @@ -153,6 +155,8 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port, will_qos = 0; will_retain = 0; + keepAliveInterval = MQTT_CONN_KEEPALIVE; + packet_id_counter = 0; } @@ -384,6 +388,25 @@ bool Adafruit_MQTT::will(const char *topic, const char *payload, uint8_t qos, return true; } +/**************************************************************************/ +/*! + @brief Sets the connect packet's KeepAlive Interval, in seconds. This + function MUST be called prior to connect(). + @param keepAlive + Maximum amount of time without communication between the + client and the MQTT broker, in seconds. + @returns True if called prior to connect(), False otherwise. +*/ +/**************************************************************************/ +bool Adafruit_MQTT::setKeepAliveInterval(uint16_t keepAlive) { + if (connected()) { + DEBUG_PRINT(F("keepAlive defined after connection established.")); + return false; + } + keepAliveInterval = keepAlive; + return true; +} + bool Adafruit_MQTT::subscribe(Adafruit_MQTT_Subscribe *sub) { uint8_t i; // see if we are already subscribed @@ -649,9 +672,9 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) { p[0] |= MQTT_CONN_PASSWORDFLAG; p++; - p[0] = MQTT_CONN_KEEPALIVE >> 8; + p[0] = keepAliveInterval >> 8; p++; - p[0] = MQTT_CONN_KEEPALIVE & 0xFF; + p[0] = keepAliveInterval & 0xFF; p++; if (MQTT_PROTOCOL_LEVEL == 3) { diff --git a/Adafruit_MQTT.h b/Adafruit_MQTT.h index 495e043..92e927f 100644 --- a/Adafruit_MQTT.h +++ b/Adafruit_MQTT.h @@ -184,6 +184,9 @@ public: bool will(const char *topic, const char *payload, uint8_t qos = 0, uint8_t retain = 0); + // Sets the KeepAlive Interval, in seconds. + bool setKeepAliveInterval(uint16_t keepAlive); + // Publish a message to a topic using the specified QoS level. Returns true // if the message was published, false otherwise. bool publish(const char *topic, const char *payload, uint8_t qos = 0); @@ -250,6 +253,7 @@ protected: const char *will_payload; uint8_t will_qos; uint8_t will_retain; + uint16_t keepAliveInterval; // MQTT KeepAlive time interval, in seconds uint8_t buffer[MAXBUFFERSIZE]; // one buffer, used for all incoming/outgoing uint16_t packet_id_counter; diff --git a/library.properties b/library.properties index d544748..0022c3b 100644 --- a/library.properties +++ b/library.properties @@ -1,8 +1,8 @@ name=Adafruit MQTT Library -version=2.3.0 +version=2.4.0 author=Adafruit maintainer=Adafruit -sentence=MQTT library that supports the FONA, ESP8266, Yun, and generic Arduino Client hardware. +sentence=MQTT library that supports the FONA, ESP8266, ESP32, Yun, and generic Arduino Client hardware. paragraph=Simple MQTT library that supports the bare minimum to publish and subscribe to topics. category=Communication url=https://github.com/adafruit/Adafruit_MQTT_Library From 86aff92f0ba45dbeb189bc214808277512224e1c Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 18 May 2021 16:43:33 -0400 Subject: [PATCH 2/3] fixup --- Adafruit_MQTT.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Adafruit_MQTT.cpp b/Adafruit_MQTT.cpp index d9973cd..bb7f8cf 100644 --- a/Adafruit_MQTT.cpp +++ b/Adafruit_MQTT.cpp @@ -388,7 +388,7 @@ bool Adafruit_MQTT::will(const char *topic, const char *payload, uint8_t qos, return true; } -/**************************************************************************/ +/***************************************************************************/ /*! @brief Sets the connect packet's KeepAlive Interval, in seconds. This function MUST be called prior to connect(). @@ -397,7 +397,7 @@ bool Adafruit_MQTT::will(const char *topic, const char *payload, uint8_t qos, client and the MQTT broker, in seconds. @returns True if called prior to connect(), False otherwise. */ -/**************************************************************************/ +/***************************************************************************/ bool Adafruit_MQTT::setKeepAliveInterval(uint16_t keepAlive) { if (connected()) { DEBUG_PRINT(F("keepAlive defined after connection established.")); From c5cdfc956422720db3dc9db1003387f41f276230 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 18 May 2021 16:54:01 -0400 Subject: [PATCH 3/3] clang format --- Adafruit_MQTT.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Adafruit_MQTT.h b/Adafruit_MQTT.h index 92e927f..1307753 100644 --- a/Adafruit_MQTT.h +++ b/Adafruit_MQTT.h @@ -253,7 +253,7 @@ protected: const char *will_payload; uint8_t will_qos; uint8_t will_retain; - uint16_t keepAliveInterval; // MQTT KeepAlive time interval, in seconds + uint16_t keepAliveInterval; // MQTT KeepAlive time interval, in seconds uint8_t buffer[MAXBUFFERSIZE]; // one buffer, used for all incoming/outgoing uint16_t packet_id_counter;