more delays - works with feather fona nicely

This commit is contained in:
ladyada 2016-01-10 21:17:26 -05:00
parent 72b3ce172e
commit f8518bc6bd
2 changed files with 23 additions and 9 deletions

View File

@ -1,4 +1,5 @@
#include <Adafruit_SleepyDog.h> #include <Adafruit_SleepyDog.h>
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h" #include "Adafruit_FONA.h"
#define halt(s) { Serial.println(F( s )); while(1); } #define halt(s) { Serial.println(F( s )); while(1); }
@ -7,7 +8,6 @@ extern Adafruit_FONA fona;
extern SoftwareSerial fonaSS; extern SoftwareSerial fonaSS;
boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password) { boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password) {
Watchdog.enable(8000);
Watchdog.reset(); Watchdog.reset();
Serial.println(F("Initializing FONA....(May take 3 seconds)")); 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) { while (fona.getNetworkStatus() != 1) {
delay(500); delay(500);
} }
Watchdog.reset();
delay(5000); // wait a few seconds to stabilize connection
Watchdog.reset(); Watchdog.reset();
fona.setGPRSNetworkSettings(apn, username, password); fona.setGPRSNetworkSettings(apn, username, password);
@ -33,6 +36,9 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u
fona.enableGPRS(false); fona.enableGPRS(false);
Watchdog.reset(); Watchdog.reset();
delay(5000); // wait a few seconds to stabilize connection
Watchdog.reset();
Serial.println(F("Enabling GPRS")); Serial.println(F("Enabling GPRS"));
if (!fona.enableGPRS(true)) { if (!fona.enableGPRS(true)) {
Serial.println(F("Failed to turn GPRS on")); Serial.println(F("Failed to turn GPRS on"));
@ -42,4 +48,3 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u
return true; return true;
} }

View File

@ -88,22 +88,28 @@ uint8_t txfailures = 0;
void setup() { void setup() {
while (!Serial); while (!Serial);
// Watchdog is optional!
//Watchdog.enable(8000);
Serial.begin(115200); Serial.begin(115200);
Serial.println(F("Adafruit FONA MQTT demo")); Serial.println(F("Adafruit FONA MQTT demo"));
mqtt.subscribe(&onoffbutton); mqtt.subscribe(&onoffbutton);
Watchdog.reset();
delay(5000); // wait a few seconds to stabilize connection
Watchdog.reset();
// Initialise the FONA module // Initialise the FONA module
while (! FONAconnect(F(FONA_APN), F(FONA_USERNAME), F(FONA_PASSWORD))) { while (! FONAconnect(F(FONA_APN), F(FONA_USERNAME), F(FONA_PASSWORD))) {
halt("Retrying FONA"); Serial.println("Retrying FONA");
} }
Serial.println(F("Connected to Cellular!")); Serial.println(F("Connected to Cellular!"));
Watchdog.reset(); Watchdog.reset();
delay(3000); // wait a few seconds to stabilize connection delay(5000); // wait a few seconds to stabilize connection
Watchdog.reset(); Watchdog.reset();
} }
@ -118,6 +124,8 @@ void loop() {
// function definition further below. // function definition further below.
MQTT_connect(); MQTT_connect();
Watchdog.reset();
// this is our 'wait for incoming subscription packets' busy subloop // this is our 'wait for incoming subscription packets' busy subloop
Adafruit_MQTT_Subscribe *subscription; Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) { while ((subscription = mqtt.readSubscription(5000))) {
@ -127,6 +135,7 @@ void loop() {
} }
} }
Watchdog.reset();
// Now we can publish stuff! // Now we can publish stuff!
Serial.print(F("\nSending photocell val ")); Serial.print(F("\nSending photocell val "));
Serial.print(x); Serial.print(x);
@ -139,10 +148,10 @@ void loop() {
txfailures = 0; txfailures = 0;
} }
// ping the server to keep the mqtt connection alive // ping the server to keep the mqtt connection alive, only needed if we're not publishing
if(! mqtt.ping()) { //if(! mqtt.ping()) {
Serial.println(F("MQTT Ping failed.")); // Serial.println(F("MQTT Ping failed."));
} //}
} }