settable keepalive interval

This commit is contained in:
brentru 2021-05-18 16:41:26 -04:00
parent ea2fb46de3
commit a1e64c039e
3 changed files with 31 additions and 4 deletions

View File

@ -132,6 +132,8 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port, const char *cid,
will_qos = 0; will_qos = 0;
will_retain = 0; will_retain = 0;
keepAliveInterval = MQTT_CONN_KEEPALIVE;
packet_id_counter = 0; packet_id_counter = 0;
} }
@ -153,6 +155,8 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port,
will_qos = 0; will_qos = 0;
will_retain = 0; will_retain = 0;
keepAliveInterval = MQTT_CONN_KEEPALIVE;
packet_id_counter = 0; packet_id_counter = 0;
} }
@ -384,6 +388,25 @@ bool Adafruit_MQTT::will(const char *topic, const char *payload, uint8_t qos,
return true; 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) { bool Adafruit_MQTT::subscribe(Adafruit_MQTT_Subscribe *sub) {
uint8_t i; uint8_t i;
// see if we are already subscribed // see if we are already subscribed
@ -649,9 +672,9 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
p[0] |= MQTT_CONN_PASSWORDFLAG; p[0] |= MQTT_CONN_PASSWORDFLAG;
p++; p++;
p[0] = MQTT_CONN_KEEPALIVE >> 8; p[0] = keepAliveInterval >> 8;
p++; p++;
p[0] = MQTT_CONN_KEEPALIVE & 0xFF; p[0] = keepAliveInterval & 0xFF;
p++; p++;
if (MQTT_PROTOCOL_LEVEL == 3) { if (MQTT_PROTOCOL_LEVEL == 3) {

View File

@ -184,6 +184,9 @@ public:
bool will(const char *topic, const char *payload, uint8_t qos = 0, bool will(const char *topic, const char *payload, uint8_t qos = 0,
uint8_t retain = 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 // Publish a message to a topic using the specified QoS level. Returns true
// if the message was published, false otherwise. // if the message was published, false otherwise.
bool publish(const char *topic, const char *payload, uint8_t qos = 0); bool publish(const char *topic, const char *payload, uint8_t qos = 0);
@ -250,6 +253,7 @@ protected:
const char *will_payload; const char *will_payload;
uint8_t will_qos; uint8_t will_qos;
uint8_t will_retain; uint8_t will_retain;
uint16_t keepAliveInterval; // MQTT KeepAlive time interval, in seconds
uint8_t buffer[MAXBUFFERSIZE]; // one buffer, used for all incoming/outgoing uint8_t buffer[MAXBUFFERSIZE]; // one buffer, used for all incoming/outgoing
uint16_t packet_id_counter; uint16_t packet_id_counter;

View File

@ -1,8 +1,8 @@
name=Adafruit MQTT Library name=Adafruit MQTT Library
version=2.3.0 version=2.4.0
author=Adafruit author=Adafruit
maintainer=Adafruit <info@adafruit.com> maintainer=Adafruit <info@adafruit.com>
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. paragraph=Simple MQTT library that supports the bare minimum to publish and subscribe to topics.
category=Communication category=Communication
url=https://github.com/adafruit/Adafruit_MQTT_Library url=https://github.com/adafruit/Adafruit_MQTT_Library