Skip to content

Commit e524b5f

Browse files
authored
Merge pull request #194 from brentru/add-kat-public-funct
Add setKeepAliveInterval() setter
2 parents ea2fb46 + c5cdfc9 commit e524b5f

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

Adafruit_MQTT.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port, const char *cid,
132132
will_qos = 0;
133133
will_retain = 0;
134134

135+
keepAliveInterval = MQTT_CONN_KEEPALIVE;
136+
135137
packet_id_counter = 0;
136138
}
137139

@@ -153,6 +155,8 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port,
153155
will_qos = 0;
154156
will_retain = 0;
155157

158+
keepAliveInterval = MQTT_CONN_KEEPALIVE;
159+
156160
packet_id_counter = 0;
157161
}
158162

@@ -384,6 +388,25 @@ bool Adafruit_MQTT::will(const char *topic, const char *payload, uint8_t qos,
384388
return true;
385389
}
386390

391+
/***************************************************************************/
392+
/*!
393+
@brief Sets the connect packet's KeepAlive Interval, in seconds. This
394+
function MUST be called prior to connect().
395+
@param keepAlive
396+
Maximum amount of time without communication between the
397+
client and the MQTT broker, in seconds.
398+
@returns True if called prior to connect(), False otherwise.
399+
*/
400+
/***************************************************************************/
401+
bool Adafruit_MQTT::setKeepAliveInterval(uint16_t keepAlive) {
402+
if (connected()) {
403+
DEBUG_PRINT(F("keepAlive defined after connection established."));
404+
return false;
405+
}
406+
keepAliveInterval = keepAlive;
407+
return true;
408+
}
409+
387410
bool Adafruit_MQTT::subscribe(Adafruit_MQTT_Subscribe *sub) {
388411
uint8_t i;
389412
// see if we are already subscribed
@@ -649,9 +672,9 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
649672
p[0] |= MQTT_CONN_PASSWORDFLAG;
650673
p++;
651674

652-
p[0] = MQTT_CONN_KEEPALIVE >> 8;
675+
p[0] = keepAliveInterval >> 8;
653676
p++;
654-
p[0] = MQTT_CONN_KEEPALIVE & 0xFF;
677+
p[0] = keepAliveInterval & 0xFF;
655678
p++;
656679

657680
if (MQTT_PROTOCOL_LEVEL == 3) {

Adafruit_MQTT.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ class Adafruit_MQTT {
184184
bool will(const char *topic, const char *payload, uint8_t qos = 0,
185185
uint8_t retain = 0);
186186

187+
// Sets the KeepAlive Interval, in seconds.
188+
bool setKeepAliveInterval(uint16_t keepAlive);
189+
187190
// Publish a message to a topic using the specified QoS level. Returns true
188191
// if the message was published, false otherwise.
189192
bool publish(const char *topic, const char *payload, uint8_t qos = 0);
@@ -250,6 +253,7 @@ class Adafruit_MQTT {
250253
const char *will_payload;
251254
uint8_t will_qos;
252255
uint8_t will_retain;
256+
uint16_t keepAliveInterval; // MQTT KeepAlive time interval, in seconds
253257
uint8_t buffer[MAXBUFFERSIZE]; // one buffer, used for all incoming/outgoing
254258
uint16_t packet_id_counter;
255259

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name=Adafruit MQTT Library
2-
version=2.3.0
2+
version=2.4.0
33
author=Adafruit
44
maintainer=Adafruit <info@adafruit.com>
5-
sentence=MQTT library that supports the FONA, ESP8266, Yun, and generic Arduino Client hardware.
5+
sentence=MQTT library that supports the FONA, ESP8266, ESP32, Yun, and generic Arduino Client hardware.
66
paragraph=Simple MQTT library that supports the bare minimum to publish and subscribe to topics.
77
category=Communication
88
url=https://github.com/adafruit/Adafruit_MQTT_Library

0 commit comments

Comments
 (0)