Skip to content

Commit fef830e

Browse files
committed
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
1 parent 475ec7a commit fef830e

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

examples/mqtt_airlift_subscribe_time_keepalive/mqtt_airlift_subscribe_time_keepalive.ino

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,23 @@
4242
#define ESP32_GPIO0 -1
4343

4444
/*************************** Timing / Params ***************************/
45+
// MQTT KeepAlive interval (seconds). The client must send at least one
46+
// packet (e.g., PINGREQ) within this interval or the MQTT server will
47+
// disconnect the session.
4548
#define AIRLIFT_KEEPALIVE 30
49+
50+
// Safety margin (seconds) to subtract before sending a manual PING.
51+
// AirLift/NINA-FW firmware does not reliably handle automatic MQTT
52+
// KeepAlive packets, and real-world testing shows a 10 second margin
53+
// prevents premature disconnects (“No socket available”).
54+
#define KEEPALIVE_SAFETY_MARGIN 10
55+
4656
#define MAX_MQTT_RETRIES 5
4757
#define SUBSCRIBE_WAIT_MS 1500
48-
#define PING_PERIOD_MS ((AIRLIFT_KEEPALIVE - 10) * 1000)
58+
59+
// Time (milliseconds) between manual MQTT PING operations.
60+
#define PING_PERIOD_MS \
61+
((AIRLIFT_KEEPALIVE - KEEPALIVE_SAFETY_MARGIN) * 1000)
4962

5063
/*************************** MQTT Objects ******************************/
5164
WiFiClient client;
@@ -65,43 +78,6 @@ Adafruit_MQTT_Subscribe time_subscription =
6578
unsigned long ping_clk = 0;
6679
long ping_cnt = 0;
6780

68-
/*************************** Function Prototypes ***********************/
69-
void initWiFi();
70-
void MQTT_connect();
71-
void MQTT_ping();
72-
void printCurrentNet();
73-
void printWiFiData();
74-
void printMacAddress(byte mac[]);
75-
76-
/*************************** Setup *************************************/
77-
void setup() {
78-
Serial.begin(115200);
79-
while (!Serial) delay(10);
80-
81-
Serial.println("MQTT AirLift Subscribe + KeepAlive Example");
82-
83-
initWiFi();
84-
85-
mqtt.setKeepAliveInterval(AIRLIFT_KEEPALIVE);
86-
mqtt.subscribe(&time_subscription);
87-
88-
MQTT_connect();
89-
}
90-
91-
/*************************** Main Loop *********************************/
92-
void loop() {
93-
94-
MQTT_ping();
95-
96-
Adafruit_MQTT_Subscribe *subscription =
97-
mqtt.readSubscription(SUBSCRIBE_WAIT_MS);
98-
99-
if (subscription == &time_subscription) {
100-
Serial.print("TIME FEED: ");
101-
Serial.println((char *)time_subscription.lastread);
102-
}
103-
}
104-
10581
/*************************** MQTT Connect ******************************/
10682
void MQTT_connect() {
10783

@@ -241,3 +217,32 @@ void printMacAddress(byte mac[]) {
241217
}
242218
Serial.print(" ");
243219
}
220+
221+
/*************************** Setup *************************************/
222+
void setup() {
223+
Serial.begin(115200);
224+
while (!Serial) delay(10);
225+
226+
Serial.println("MQTT AirLift Subscribe + KeepAlive Example");
227+
228+
initWiFi();
229+
230+
mqtt.setKeepAliveInterval(AIRLIFT_KEEPALIVE);
231+
mqtt.subscribe(&time_subscription);
232+
233+
MQTT_connect();
234+
}
235+
236+
/*************************** Main Loop *********************************/
237+
void loop() {
238+
239+
MQTT_ping();
240+
241+
Adafruit_MQTT_Subscribe *subscription =
242+
mqtt.readSubscription(SUBSCRIBE_WAIT_MS);
243+
244+
if (subscription == &time_subscription) {
245+
Serial.print("TIME FEED: ");
246+
Serial.println((char *)time_subscription.lastread);
247+
}
248+
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Adafruit MQTT Library
2-
version=2.6.1
2+
version=2.6.2
33
author=Adafruit
44
maintainer=Adafruit <[email protected]>
55
sentence=MQTT library that supports the FONA, ESP8266, ESP32, Yun, and generic Arduino Client hardware.

0 commit comments

Comments
 (0)