From f8518bc6bdccbf3840c1ef531093b3b3d2d5adf9 Mon Sep 17 00:00:00 2001 From: ladyada Date: Sun, 10 Jan 2016 21:17:26 -0500 Subject: [PATCH] more delays - works with feather fona nicely --- examples/mqtt_fona/fonahelper.cpp | 9 +++++++-- examples/mqtt_fona/mqtt_fona.ino | 23 ++++++++++++++++------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/examples/mqtt_fona/fonahelper.cpp b/examples/mqtt_fona/fonahelper.cpp index d808604..822413e 100644 --- a/examples/mqtt_fona/fonahelper.cpp +++ b/examples/mqtt_fona/fonahelper.cpp @@ -1,4 +1,5 @@ #include +#include #include "Adafruit_FONA.h" #define halt(s) { Serial.println(F( s )); while(1); } @@ -7,7 +8,6 @@ extern Adafruit_FONA fona; extern SoftwareSerial fonaSS; boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password) { - Watchdog.enable(8000); Watchdog.reset(); Serial.println(F("Initializing FONA....(May take 3 seconds)")); @@ -25,6 +25,9 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u while (fona.getNetworkStatus() != 1) { delay(500); } + + Watchdog.reset(); + delay(5000); // wait a few seconds to stabilize connection Watchdog.reset(); fona.setGPRSNetworkSettings(apn, username, password); @@ -33,6 +36,9 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u fona.enableGPRS(false); Watchdog.reset(); + delay(5000); // wait a few seconds to stabilize connection + Watchdog.reset(); + Serial.println(F("Enabling GPRS")); if (!fona.enableGPRS(true)) { Serial.println(F("Failed to turn GPRS on")); @@ -42,4 +48,3 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u return true; } - diff --git a/examples/mqtt_fona/mqtt_fona.ino b/examples/mqtt_fona/mqtt_fona.ino index a7b00aa..ba8f389 100644 --- a/examples/mqtt_fona/mqtt_fona.ino +++ b/examples/mqtt_fona/mqtt_fona.ino @@ -88,22 +88,28 @@ uint8_t txfailures = 0; void setup() { while (!Serial); + // Watchdog is optional! + //Watchdog.enable(8000); + Serial.begin(115200); Serial.println(F("Adafruit FONA MQTT demo")); - mqtt.subscribe(&onoffbutton); + Watchdog.reset(); + delay(5000); // wait a few seconds to stabilize connection + Watchdog.reset(); + // Initialise the FONA module while (! FONAconnect(F(FONA_APN), F(FONA_USERNAME), F(FONA_PASSWORD))) { - halt("Retrying FONA"); + Serial.println("Retrying FONA"); } Serial.println(F("Connected to Cellular!")); Watchdog.reset(); - delay(3000); // wait a few seconds to stabilize connection + delay(5000); // wait a few seconds to stabilize connection Watchdog.reset(); } @@ -118,6 +124,8 @@ void loop() { // function definition further below. MQTT_connect(); + Watchdog.reset(); + // this is our 'wait for incoming subscription packets' busy subloop Adafruit_MQTT_Subscribe *subscription; while ((subscription = mqtt.readSubscription(5000))) { @@ -127,6 +135,7 @@ void loop() { } } + Watchdog.reset(); // Now we can publish stuff! Serial.print(F("\nSending photocell val ")); Serial.print(x); @@ -139,10 +148,10 @@ void loop() { txfailures = 0; } - // ping the server to keep the mqtt connection alive - if(! mqtt.ping()) { - Serial.println(F("MQTT Ping failed.")); - } + // ping the server to keep the mqtt connection alive, only needed if we're not publishing + //if(! mqtt.ping()) { + // Serial.println(F("MQTT Ping failed.")); + //} }