From fef830edc259124b51807d95bd8d6714c7ce1507 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Mon, 24 Nov 2025 07:27:36 -0800 Subject: [PATCH] feedback fixes Thx @brentru . Changes have been made and tested. - rev library 2.6.1 -> 2.6.2 - move setup() / loop() to bottom - drop prototypes - -10 safety margin added as #define --- .../mqtt_airlift_subscribe_time_keepalive.ino | 81 ++++++++++--------- library.properties | 2 +- 2 files changed, 44 insertions(+), 39 deletions(-) diff --git a/examples/mqtt_airlift_subscribe_time_keepalive/mqtt_airlift_subscribe_time_keepalive.ino b/examples/mqtt_airlift_subscribe_time_keepalive/mqtt_airlift_subscribe_time_keepalive.ino index 3fa49f7..3e19805 100644 --- a/examples/mqtt_airlift_subscribe_time_keepalive/mqtt_airlift_subscribe_time_keepalive.ino +++ b/examples/mqtt_airlift_subscribe_time_keepalive/mqtt_airlift_subscribe_time_keepalive.ino @@ -42,10 +42,23 @@ #define ESP32_GPIO0 -1 /*************************** Timing / Params ***************************/ +// MQTT KeepAlive interval (seconds). The client must send at least one +// packet (e.g., PINGREQ) within this interval or the MQTT server will +// disconnect the session. #define AIRLIFT_KEEPALIVE 30 + +// Safety margin (seconds) to subtract before sending a manual PING. +// AirLift/NINA-FW firmware does not reliably handle automatic MQTT +// KeepAlive packets, and real-world testing shows a 10 second margin +// prevents premature disconnects (“No socket available”). +#define KEEPALIVE_SAFETY_MARGIN 10 + #define MAX_MQTT_RETRIES 5 #define SUBSCRIBE_WAIT_MS 1500 -#define PING_PERIOD_MS ((AIRLIFT_KEEPALIVE - 10) * 1000) + +// Time (milliseconds) between manual MQTT PING operations. +#define PING_PERIOD_MS \ + ((AIRLIFT_KEEPALIVE - KEEPALIVE_SAFETY_MARGIN) * 1000) /*************************** MQTT Objects ******************************/ WiFiClient client; @@ -65,43 +78,6 @@ Adafruit_MQTT_Subscribe time_subscription = unsigned long ping_clk = 0; long ping_cnt = 0; -/*************************** Function Prototypes ***********************/ -void initWiFi(); -void MQTT_connect(); -void MQTT_ping(); -void printCurrentNet(); -void printWiFiData(); -void printMacAddress(byte mac[]); - -/*************************** Setup *************************************/ -void setup() { - Serial.begin(115200); - while (!Serial) delay(10); - - Serial.println("MQTT AirLift Subscribe + KeepAlive Example"); - - initWiFi(); - - mqtt.setKeepAliveInterval(AIRLIFT_KEEPALIVE); - mqtt.subscribe(&time_subscription); - - MQTT_connect(); -} - -/*************************** Main Loop *********************************/ -void loop() { - - MQTT_ping(); - - Adafruit_MQTT_Subscribe *subscription = - mqtt.readSubscription(SUBSCRIBE_WAIT_MS); - - if (subscription == &time_subscription) { - Serial.print("TIME FEED: "); - Serial.println((char *)time_subscription.lastread); - } -} - /*************************** MQTT Connect ******************************/ void MQTT_connect() { @@ -241,3 +217,32 @@ void printMacAddress(byte mac[]) { } Serial.print(" "); } + +/*************************** Setup *************************************/ +void setup() { + Serial.begin(115200); + while (!Serial) delay(10); + + Serial.println("MQTT AirLift Subscribe + KeepAlive Example"); + + initWiFi(); + + mqtt.setKeepAliveInterval(AIRLIFT_KEEPALIVE); + mqtt.subscribe(&time_subscription); + + MQTT_connect(); +} + +/*************************** Main Loop *********************************/ +void loop() { + + MQTT_ping(); + + Adafruit_MQTT_Subscribe *subscription = + mqtt.readSubscription(SUBSCRIBE_WAIT_MS); + + if (subscription == &time_subscription) { + Serial.print("TIME FEED: "); + Serial.println((char *)time_subscription.lastread); + } +} diff --git a/library.properties b/library.properties index a1a600d..fa285d0 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Adafruit MQTT Library -version=2.6.1 +version=2.6.2 author=Adafruit maintainer=Adafruit sentence=MQTT library that supports the FONA, ESP8266, ESP32, Yun, and generic Arduino Client hardware.