BlynkTimer and GPS connection problems

I have an Arduino Uno with a Sim808 GRPS + GPS shield. I can get GRPS and GPS to function and connect to Blynk normally until I include BlynkTimer. When I include, I cannot connect to Blynk. If I do not enable GPS and include BlynkTimer, I get a normal connection. For some reason these two are interfering with each other. This is my first Blynk project, love the app. Any ideas? Thanks for the help.


#define BLYNK_PRINT Serial

#define TINY_GSM_MODEM_SIM800
#define FONA_RX 3
#define FONA_TX 2
#define FONA_RST 4
// Default heartbeat interval for GSM is 60
// If you want override this value, uncomment and set this option:
//#define BLYNK_HEARTBEAT 30

#include <TinyGsmClient.h>
#include <BlynkSimpleSIM800.h>
#include "Adafruit_FONA.h"
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

// Your GPRS credentials
// Leave empty, if missing user or pass
char apn[]  = "wholesale";
char user[] = "";
char pass[] = "";

// Hardware Serial on Mega, Leonardo, Micro
//#define SerialAT Serial




float centerlat = 34.2;     //Find Heading
float centerlon = -101.7;    //Find Heading

float latitude, longitude, speed_kph, heading, speed_mph, altitude;
//float GPSlat=latitude;
//float GPSlon=longitude;
int sprinklerdegree;
int startnow;
int stopnow;
int onforat;
int onrevat;
int forat;
int revat;
int fordegree;
int revdegree;
int forRev;
int safetyout;
// or Software Serial on Uno, Nano
#include <SoftwareSerial.h>
SoftwareSerial SerialAT(3, 2); // RX, TX
SoftwareSerial fonaSS = SoftwareSerial(FONA_RX, FONA_TX); //for FONA808 GPS
SoftwareSerial *fonaSerial = &fonaSS;
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
TinyGsm modem(SerialAT);
BlynkTimer timer; 

//#include<simpletimer.h>
BLYNK_CONNECTED()
{
  Blynk.syncAll();
}

void getHeading()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
 float y = sin(centerlon - longitude) * cos(latitude);
    float x = cos(centerlat) * sin(latitude) - sin(centerlat)*cos(latitude)*cos(centerlon-longitude);
    float b = atan2(y,x);
   const float pi = 3.14159267;
   float  bearingdeg = b *(180 / pi);
  // int sprinklerdegree;
   if (bearingdeg < 0) {                              //turn negative into correct degree bearing
    sprinklerdegree = (360 + bearingdeg);
   }
  Blynk.virtualWrite(V2, sprinklerdegree);
}
 

 void turnongps()
 {
  Serial.begin(9600);

  delay(10);
  while (! Serial);

  Serial.begin(9600);
  Serial.println(F("Adafruit FONA 808 & 3G GPS demo"));
  Serial.println(F("Initializing FONA... (May take a few seconds)"));

  fonaSerial->begin(9600);
  if (! fona.begin(*fonaSerial)) {
    Serial.println(F("Couldn't find FONA"));
    while(1);
  }
  Serial.println(F("FONA is OK"));
  // Try to enable GPRS
  

  Serial.println(F("Enabling GPS..."));
 fona.enableGPS(true);
 fona.getGPS(&latitude, &longitude, &speed_kph, &heading, &altitude);
 while(!(fona.GPSstatus() == 3)) //fona.getGPS is run until fona.GPSstatus() == 3 
 { 
    boolean gps_success = fona.getGPS(&latitude, &longitude, &speed_kph, &heading, &altitude);
    fona.getGPS(&latitude, &longitude, &speed_kph, &heading, &altitude);
    delay(1000);
  }
  if(fona.GPSstatus() == 3) //when fona.GPSstatus() == 3, coordinates have been extracted
  {
    
    boolean gps_success = fona.getGPS(&latitude, &longitude, &speed_kph, &heading, &altitude);
    if (gps_success){

      Serial.print("GPS lat:");
     // GPSlat = latitude, 6; 
     Serial.println(latitude,4); //Serial.print() only limits to show 2 d.p
      Serial.print("GPS long:");
   //   GPSlon = longitude, 6;
      Serial.println(longitude,4);
      Serial.print("GPS speed KPH:");
      Serial.println(speed_kph);
      Serial.print("GPS speed MPH:");
      speed_mph = speed_kph * 0.621371192;
      Serial.println(speed_mph);
      Serial.print("GPS heading:");
      Serial.println(heading);
      Serial.print("GPS altitude:");
      Serial.println(altitude);
    }
 }  
 
 
 fonaSerial->end(); //end serial communication with GPS to avoid conflict
 }
void setup()
{
Serial.begin(9600);
 turnongps();  
 
 
  // Debug console
  
  
  // Set GSM module baud rate
  SerialAT.begin(9600);                      //Begin GSM

  // Restart takes quite some time
  // To skip it, call init() instead of restart()
  Serial.println("Initializing modem...");


  modem.restart();
 Blynk.begin(auth, modem, apn, user, pass);
//Blynk.config(modem,auth,"blynk-cloud.com");
//Blynk.connect();
Serial.print("done");
 
}

void loop()
{
  if(!Blynk.connected())
 {
 // Serial.print("Blynk has disconnected");
  modem.restart();
  Blynk.begin(auth, modem, apn, user, pass);
   }
  Blynk.run();
  // timer.run();

}

This is is totally unnecessary now with Blynk, just delete it.

Do you mean you can’t connect after uncommenting timer.run()? becasue I don’t see any actual timers in setup() or anywhere else in your code, commented or not, so am unsure what could possibly be the issue… timer.run() does not affect Blynk’s connection… it can even be used without Blynk in the first place (well… then you do need to include the Simpletimer library, but that is not relevant here)

Hmmm, of course I have never tried to have timer.run() without any actual timers… perhaps this is the issue?

Here is code with timer included. I am basically trying to turn on relay for a percent of a minute and cycle continuously. The reason for the <simpletimer.h> was to try other options to see if I could get a connection. I still get:
[24776] Modem init…
[26032] Connecting to network…
[29310] Network: T-Mobile USA
[29312] Connecting to wholesale …
[36259] Connected to GPRS
[36324] Connecting to blynk-cloud.com:80
[39683] Ready (ping: 755ms).
[45699] Connecting to blynk-cloud.com:80
[61786] Connecting to blynk-cloud.com:80
[64107] Ready (ping: 864ms).
[70124] Connecting to blynk-cloud.com:80
[86233] Connecting to blynk-cloud.com:80
[88236] Ready (ping: 542ms).
[94252] Connecting to blynk-cloud.com:80
[110694] Connecting to blynk-cloud.com:80
[113324] Ready (ping: 1173ms).
[119340] Connecting to blynk-cloud.com:80
[135146] Connecting to blynk-cloud.com:80
[137147] Ready (ping: 541ms).
Until I remove the BlynkTimer or dont enable GPS.


#define BLYNK_PRINT Serial

#define TINY_GSM_MODEM_SIM800
#define FONA_RX 3
#define FONA_TX 2
#define FONA_RST 4
// Default heartbeat interval for GSM is 60
// If you want override this value, uncomment and set this option:
//#define BLYNK_HEARTBEAT 30

#include <TinyGsmClient.h>
#include <BlynkSimpleSIM800.h>
#include "Adafruit_FONA.h"
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

// Your GPRS credentials
// Leave empty, if missing user or pass
char apn[]  = "wholesale";
char user[] = "";
char pass[] = "";

// Hardware Serial on Mega, Leonardo, Micro
//#define SerialAT Serial


long percentInput;
long percentRun;
//float  on=percentInput;                       // percent timer
long off;                       // percent timer
float centerlat = 34.2;     //Find Heading
float centerlon = -101.7;    //Find Heading

float latitude, longitude, speed_kph, heading, speed_mph, altitude;
//float GPSlat=latitude;
//float GPSlon=longitude;
int sprinklerdegree;
int startnow;
int stopnow;
int onforat;
int onrevat;
int forat;
int revat;
int fordegree;
int revdegree;
int forRev;
int safetyout;
// or Software Serial on Uno, Nano
#include <SoftwareSerial.h>
SoftwareSerial SerialAT(3, 2); // RX, TX
SoftwareSerial fonaSS = SoftwareSerial(FONA_RX, FONA_TX); //for FONA808 GPS
SoftwareSerial *fonaSerial = &fonaSS;
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
TinyGsm modem(SerialAT);
BlynkTimer timer; 

//#include<simpletimer.h>
BLYNK_CONNECTED()
{
  Blynk.syncAll();
}


BLYNK_WRITE(V1)
{
  
  pinMode(8, OUTPUT);
  percentInput = param.asInt(); // assigning incoming value from pin V1 to a variable
 

  percentRun = ((percentInput / 100) * 60000);
  off = 60000-percentRun;
 Blynk.virtualWrite(V3, off);
digitalWrite(8, HIGH); 
timer.setTimeout(off, stoptire);
}

void stoptire()
{
digitalWrite(8, LOW);  
}



void getgps()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
 float y = sin(centerlon - longitude) * cos(latitude);
    float x = cos(centerlat) * sin(latitude) - sin(centerlat)*cos(latitude)*cos(centerlon-longitude);
    float b = atan2(y,x);
   const float pi = 3.14159267;
   float  bearingdeg = b *(180 / pi);
  // int sprinklerdegree;
   if (bearingdeg < 0) {                              //turn negative into correct degree bearing
    sprinklerdegree = (360 + bearingdeg);
   }
  Blynk.virtualWrite(V2, sprinklerdegree);
}
 

 void turnongps()
 {
  Serial.begin(9600);

  delay(10);
  while (! Serial);

  Serial.begin(9600);
  Serial.println(F("Adafruit FONA 808 & 3G GPS demo"));
  Serial.println(F("Initializing FONA... (May take a few seconds)"));

  fonaSerial->begin(9600);
  if (! fona.begin(*fonaSerial)) {
    Serial.println(F("Couldn't find FONA"));
    while(1);
  }
  Serial.println(F("FONA is OK"));
  // Try to enable GPRS
  

  Serial.println(F("Enabling GPS..."));
 fona.enableGPS(true);
 fona.getGPS(&latitude, &longitude, &speed_kph, &heading, &altitude);
 while(!(fona.GPSstatus() == 3)) //fona.getGPS is run until fona.GPSstatus() == 3 
 { 
    boolean gps_success = fona.getGPS(&latitude, &longitude, &speed_kph, &heading, &altitude);
    fona.getGPS(&latitude, &longitude, &speed_kph, &heading, &altitude);
    delay(1000);
  }
  if(fona.GPSstatus() == 3) //when fona.GPSstatus() == 3, coordinates have been extracted
  {
    
    boolean gps_success = fona.getGPS(&latitude, &longitude, &speed_kph, &heading, &altitude);
    if (gps_success){

      Serial.print("GPS lat:");
     // GPSlat = latitude, 6; 
     Serial.println(latitude,4); //Serial.print() only limits to show 2 d.p
      Serial.print("GPS long:");
   //   GPSlon = longitude, 6;
      Serial.println(longitude,4);
      Serial.print("GPS speed KPH:");
      Serial.println(speed_kph);
      Serial.print("GPS speed MPH:");
      speed_mph = speed_kph * 0.621371192;
      Serial.println(speed_mph);
      Serial.print("GPS heading:");
      Serial.println(heading);
      Serial.print("GPS altitude:");
      Serial.println(altitude);
    }
 }  
 
 
 fonaSerial->end(); //end serial communication with GPS to avoid conflict
 }
void setup()
{
Serial.begin(9600);
 turnongps();  
 
 
  // Debug console
  
  
  // Set GSM module baud rate
  SerialAT.begin(9600);                      //Begin GSM

  // Restart takes quite some time
  // To skip it, call init() instead of restart()
  Serial.println("Initializing modem...");


  modem.restart();
 Blynk.begin(auth, modem, apn, user, pass);
//Blynk.config(modem,auth,"blynk-cloud.com");
//Blynk.connect();
Serial.print("done");
 
}

void loop()
{
  if(!Blynk.connected())
 {
 // Serial.print("Blynk has disconnected");
  modem.restart();
  Blynk.begin(auth, modem, apn, user, pass);
   }
  Blynk.run();
   timer.run();

}

I would say that between all your delays, your while waits for serial data and your high ping rates with the modem, that you’re just simply running into too much too fast for too long… and that is causing Blynk disconnects.

Try breaking things into smaller timer and logic managed chunks, blocking no more than a second or so between void loop() calls.