Skip to content

Commit d5554b5

Browse files
committed
fix(ping): Cache keepalive interval and round throttle loops
1 parent 36063f3 commit d5554b5

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/Wippersnapper.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,16 +1746,18 @@ void cbThrottleTopic(char *throttleData, uint16_t len) {
17461746
WS_DEBUG_PRINTVAR(throttleDuration);
17471747
WS_DEBUG_PRINTLN("ms and blocking command execution.");
17481748

1749+
const uint32_t pingDelayMs = WS_DEVICE_PING_MS;
1750+
17491751
// If throttle duration is less than the keepalive interval, delay for the
17501752
// full keepalive interval
1751-
if (throttleDuration < WS_DEVICE_PING_MS) {
1752-
delay(WS_DEVICE_PING_MS);
1753+
if (throttleDuration < pingDelayMs) {
1754+
delay(pingDelayMs);
17531755
} else {
1754-
// round to nearest millis to prevent delaying for less time than req'd.
1755-
float throttleLoops = ceil(throttleDuration / WS_DEVICE_PING_MS);
1756+
// Round up so throttling never ends earlier than requested.
1757+
uint32_t throttleLoops = (throttleDuration + pingDelayMs - 1) / pingDelayMs;
17561758
// block the run() loop
17571759
while (throttleLoops > 0) {
1758-
delay(WS_DEVICE_PING_MS);
1760+
delay(pingDelayMs);
17591761
WS.feedWDT();
17601762
WS._mqtt->ping();
17611763
throttleLoops--;
@@ -2474,7 +2476,7 @@ void Wippersnapper::runNetFSM() {
24742476
fsmNetwork = FSM_NET_CHECK_NETWORK;
24752477
break;
24762478
case FSM_NET_ESTABLISH_MQTT:
2477-
WS._mqtt->setKeepAliveInterval(WS_BROKER_KEEPALIVE_MS / 1000);
2479+
WS._mqtt->setKeepAliveInterval(_brokerKeepAliveIntervalSeconds);
24782480
// Attempt to connect
24792481
maxAttempts = 5;
24802482
while (maxAttempts > 0) {
@@ -2794,6 +2796,8 @@ void Wippersnapper::connect() {
27942796
// Dump device info to the serial monitor
27952797
printDeviceInfo();
27962798

2799+
_brokerKeepAliveIntervalSeconds = WS_BROKER_KEEPALIVE_MS / 1000;
2800+
27972801
// Generate device identifier
27982802
if (!generateDeviceUID()) {
27992803
haltError("Unable to generate Device UID");

src/Wippersnapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ class Wippersnapper {
478478
ws_status_t _status = WS_IDLE; /*!< Adafruit IO connection status */
479479
uint32_t _last_mqtt_connect = 0; /*!< Previous time when client connected to
480480
Adafruit IO, in milliseconds. */
481+
uint16_t _brokerKeepAliveIntervalSeconds =
482+
0; /*!< Cached MQTT broker keepalive interval, in seconds. */
481483
uint32_t _prv_ping = 0; /*!< Previous time when client pinged Adafruit IO's
482484
MQTT broker, in milliseconds. */
483485
uint32_t _prvKATBlink = 0; /*!< Previous time when client pinged Adafruit IO's

0 commit comments

Comments
 (0)