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 ******************************/
5164WiFiClient client;
@@ -65,43 +78,6 @@ Adafruit_MQTT_Subscribe time_subscription =
6578unsigned long ping_clk = 0 ;
6679long 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 ******************************/
10682void 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+ }
0 commit comments