From 4dc6f1e486cd02984a93e3d367d952ccb2d601ef Mon Sep 17 00:00:00 2001 From: Todd Treece Date: Thu, 22 Oct 2015 14:13:19 -0400 Subject: [PATCH] adds ping to the end of the main loop in examples --- examples/mqtt_cc3k/mqtt_cc3k.ino | 16 +++---- examples/mqtt_esp8266/mqtt_esp8266.ino | 14 +++--- examples/mqtt_ethernet/mqtt_ethernet.ino | 13 +++--- examples/mqtt_fona/mqtt_fona.ino | 55 +++++++++++++----------- examples/mqtt_yun/mqtt_yun.ino | 14 +++--- 5 files changed, 55 insertions(+), 57 deletions(-) diff --git a/examples/mqtt_cc3k/mqtt_cc3k.ino b/examples/mqtt_cc3k/mqtt_cc3k.ino index 4fa3a26..c7db7ee 100644 --- a/examples/mqtt_cc3k/mqtt_cc3k.ino +++ b/examples/mqtt_cc3k/mqtt_cc3k.ino @@ -104,16 +104,6 @@ void loop() { // connection and automatically reconnect when disconnected). See the MQTT_connect // function definition further below. MQTT_connect(); - - // Try to ping the MQTT server - /* - if (! mqtt.ping(3) ) { - // MQTT pings failed, let's reconnect by forcing a watchdog reset. - Serial.println("Ping fail! Resetting..."); - Watchdog.enable(8000); - delay(10000); - } - */ // this is our 'wait for incoming subscription packets' busy subloop Adafruit_MQTT_Subscribe *subscription; @@ -133,6 +123,12 @@ void loop() { } else { Serial.println(F("OK!")); } + + // ping the server to keep the mqtt connection alive + if(! mqtt.ping()) { + Serial.println(F("MQTT Ping failed.")); + } + } // Function to connect and reconnect as necessary to the MQTT server. diff --git a/examples/mqtt_esp8266/mqtt_esp8266.ino b/examples/mqtt_esp8266/mqtt_esp8266.ino index 75830c3..8defec1 100644 --- a/examples/mqtt_esp8266/mqtt_esp8266.ino +++ b/examples/mqtt_esp8266/mqtt_esp8266.ino @@ -90,14 +90,6 @@ void loop() { // function definition further below. MQTT_connect(); - // Try to ping the MQTT server - /* - if (! mqtt.ping(3) ) { - // MQTT pings failed, lets reconnect - Serial.println("Ping fail!"); - } - */ - // this is our 'wait for incoming subscription packets' busy subloop Adafruit_MQTT_Subscribe *subscription; while ((subscription = mqtt.readSubscription(1000))) { @@ -117,7 +109,13 @@ void loop() { Serial.println(F("OK!")); } + // ping the server to keep the mqtt connection alive + if(! mqtt.ping()) { + mqtt.disconnect(); + } + delay(1000); + } // Function to connect and reconnect as necessary to the MQTT server. diff --git a/examples/mqtt_ethernet/mqtt_ethernet.ino b/examples/mqtt_ethernet/mqtt_ethernet.ino index c27f8a8..ecb1a24 100644 --- a/examples/mqtt_ethernet/mqtt_ethernet.ino +++ b/examples/mqtt_ethernet/mqtt_ethernet.ino @@ -91,13 +91,6 @@ void loop() { // connection and automatically reconnect when disconnected). See the MQTT_connect // function definition further below. MQTT_connect(); - - // Try to ping the MQTT server - if (! mqtt.ping(3) ) { - // MQTT pings failed, let's reconnect by forcing a watchdog reset. - Serial.println("Ping fail! Resetting..."); - delay(10000); - } // this is our 'wait for incoming subscription packets' busy subloop Adafruit_MQTT_Subscribe *subscription; @@ -117,6 +110,12 @@ void loop() { } else { Serial.println(F("OK!")); } + + // ping the server to keep the mqtt connection alive + if(! mqtt.ping()) { + mqtt.disconnect(); + } + } // Function to connect and reconnect as necessary to the MQTT server. diff --git a/examples/mqtt_fona/mqtt_fona.ino b/examples/mqtt_fona/mqtt_fona.ino index 45c41cb..a7b00aa 100644 --- a/examples/mqtt_fona/mqtt_fona.ino +++ b/examples/mqtt_fona/mqtt_fona.ino @@ -113,30 +113,10 @@ void loop() { // Make sure to reset watchdog every loop iteration! Watchdog.reset(); - // check if we're still connected - if (!fona.TCPconnected() || (txfailures >= MAXTXFAILURES)) { - Serial.println(F("Connecting to MQTT...")); - int8_t ret, retries = 5; - while (retries && (ret = mqtt.connect()) != 0) { - Serial.println(mqtt.connectErrorString(ret)); - Serial.println(F("Retrying MQTT connection")); - retries--; - if (retries == 0) halt("Resetting system"); - delay(5000); - } - Serial.println(F("MQTT Connected!")); - txfailures = 0; - } - - - // Try to ping the MQTT server - /* - if (! mqtt.ping(3) ) { - // MQTT pings failed, lets reconnect - Serial.println("Ping fail!"); - } - */ - + // Ensure the connection to the MQTT server is alive (this will make the first + // connection and automatically reconnect when disconnected). See the MQTT_connect + // function definition further below. + MQTT_connect(); // this is our 'wait for incoming subscription packets' busy subloop Adafruit_MQTT_Subscribe *subscription; @@ -158,4 +138,31 @@ void loop() { Serial.println(F("OK!")); txfailures = 0; } + + // ping the server to keep the mqtt connection alive + if(! mqtt.ping()) { + Serial.println(F("MQTT Ping failed.")); + } + +} + +// Function to connect and reconnect as necessary to the MQTT server. +// Should be called in the loop function and it will take care if connecting. +void MQTT_connect() { + int8_t ret; + + // Stop if already connected. + if (mqtt.connected()) { + return; + } + + Serial.print("Connecting to MQTT... "); + + while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected + Serial.println(mqtt.connectErrorString(ret)); + Serial.println("Retrying MQTT connection in 5 seconds..."); + mqtt.disconnect(); + delay(5000); // wait 5 seconds + } + Serial.println("MQTT Connected!"); } diff --git a/examples/mqtt_yun/mqtt_yun.ino b/examples/mqtt_yun/mqtt_yun.ino index 33fa550..b02d06d 100644 --- a/examples/mqtt_yun/mqtt_yun.ino +++ b/examples/mqtt_yun/mqtt_yun.ino @@ -74,14 +74,6 @@ void loop() { // function definition further below. MQTT_connect(); - // Try to ping the MQTT server - /* - if (! mqtt.ping(3) ) { - // MQTT pings failed, lets reconnect - Console.println("Ping fail!"); - } - */ - // this is our 'wait for incoming subscription packets' busy subloop Adafruit_MQTT_Subscribe *subscription; while ((subscription = mqtt.readSubscription(1000))) { @@ -101,7 +93,13 @@ void loop() { Console.println(F("OK!")); } + // ping the server to keep the mqtt connection alive + if(! mqtt.ping()) { + Serial.println(F("MQTT Ping failed.")); + } + delay(1000); + } // Function to connect and reconnect as necessary to the MQTT server.