Blynk going of line without any reason

Hello, I need your help.

I want to test the build-in GPS of my device (LILYGO® TTGO T-SIM7000G Module ESP32-WROVER-B Chip WiFi Bluetooth 18560 Battery Holder Solar Charge Development Board)
I uploaded the code to my device, device is connecting to wifi, it is staying connected for only two minutes. (I can share the screenshot).
I could not find reason of disconnecting.
here is my code


// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "TMPLSYSoySsN"
#define BLYNK_DEVICE_NAME "GPS2"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
#define USE_WROVER_BOARD
//#define USE_TTGO_T7
//#define USE_ESP32C3_DEV_MODULE
//#define USE_ESP32S2_DEV_KIT

#include "BlynkEdgent.h"

#define SerialMon Serial

// Set serial for AT commands (to the module)
// Use Hardware Serial on Mega, Leonardo, Micro
#define SerialAT Serial1

#define TINY_GSM_MODEM_SIM7000
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
#define SerialAT Serial1

// See all AT commands, if wanted
// #define DUMP_AT_COMMANDS

// set GSM PIN, if any
#define GSM_PIN ""

// Your GPRS credentials, if any
const char apn[]  = "ucell";     //SET TO YOUR APN
const char gprsUser[] = "";
const char gprsPass[] = "";

String msg1;
String msg2;

#include <TinyGsmClient.h>
#include <SPI.h>
#include <SD.h>
#include <Ticker.h>

#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif

#define uS_TO_S_FACTOR      1000000ULL  // Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP       60          // Time ESP32 will go to sleep (in seconds)

#define UART_BAUD           9600
#define PIN_DTR             25
#define PIN_TX              27
#define PIN_RX              26
#define PWR_PIN             4

#define SD_MISO             2
#define SD_MOSI             15
#define SD_SCLK             14
#define SD_CS               13
#define LED_PIN             12


void enableGPS(void)
{
    // Set SIM7000G GPIO4 LOW ,turn on GPS power
    // CMD:AT+SGPIO=0,4,1,1
    // Only in version 20200415 is there a function to control GPS power
    modem.sendAT("+SGPIO=0,4,1,1");
    modem.enableGPS();


}

void disableGPS(void)
{
    // Set SIM7000G GPIO4 LOW ,turn off GPS power
    // CMD:AT+SGPIO=0,4,1,0
    // Only in version 20200415 is there a function to control GPS power
    modem.sendAT("+SGPIO=0,4,1,0");
    modem.disableGPS();
}

void modemPowerOn()
{
    pinMode(PWR_PIN, OUTPUT);
    digitalWrite(PWR_PIN, LOW);
    delay(1000);    //Datasheet Ton mintues = 1S
    digitalWrite(PWR_PIN, HIGH);
}

void modemPowerOff()
{
    pinMode(PWR_PIN, OUTPUT);
    digitalWrite(PWR_PIN, LOW);
    delay(1500);    //Datasheet Ton mintues = 1.2S
    digitalWrite(PWR_PIN, HIGH);
}


void modemRestart()
{
    modemPowerOff();
    delay(1000);
    modemPowerOn();
}
const byte trig = 18;
const byte echo = 19;
//BlynkTimer timer;
BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V0, V1, V2, V3, V4, V5, V6, V7);
}
void setup()
{
     // Set console baud rate
    SerialMon.begin(115200);

    delay(10);

    // Set LED OFF
    pinMode(LED_PIN, OUTPUT);
    digitalWrite(LED_PIN, HIGH);

    modemPowerOn();

    SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);

    delay(10000);
  BlynkEdgent.begin();
}

void loop() {
  if(Blynk.connected()){
  if (!modem.testAT()) {
    
        Serial.println("Failed to restart modem, attempting to continue without restarting");
        modemRestart();
        return;
    }

    msg1 = "Start positioning . Make sure to locate outdoors. The blue indicator light flashes to indicate positioning.";
   
    Serial.println(msg1);
    Blynk.virtualWrite(V0, msg1);   
    
    enableGPS();

    float lat,  lon;
    while (1) {
        if (modem.getGPS(&lat, &lon)) {
            Serial.println("The location has been locked, the latitude and longitude are:");
            Serial.print("latitude:"); Serial.println(lat);
            Serial.print("longitude:"); Serial.println(lon);
            Blynk.virtualWrite(V1, String(lat, 6));   
            Blynk.virtualWrite(V2, String(lon, 6));
            break;
        }
        digitalWrite(LED_PIN, !digitalRead(LED_PIN));
        delay(2000);
    }

    disableGPS();

    Serial.println("/**********************************************************/");
    Serial.println("After the network test is complete, please enter the  ");
    Serial.println("AT command in the serial terminal.");
    Serial.println("/**********************************************************/\n\n");

    while (1) {
        while (SerialAT.available()) {
            SerialMon.write(SerialAT.read());
        }
        while (SerialMon.available()) {
            SerialAT.write(SerialMon.read());
        }
    }
  }
  BlynkEdgent.run();
  //timer.run();
  
}

I think it’s fair to assume that the disconnections are happening because of all that code you’ve dumped in your void loop, and the fact that you are using a blocking delay() and three blocking while() loops which are most likely causing the connection to the Blynk server to time-out.

Pete.

replaced with

void myDelay(int del) {
  unsigned long myPrevMillis = millis();
  unsigned long myCurrentMillis = myPrevMillis;
  while (myCurrentMillis - myPrevMillis <= del) {
  myCurrentMillis = millis();
  }
}

has replaced with

for (; 1 ;) 

the result is the same

You don’t like BlynkTimer then?

And posting snippets of code tells us nothing about the state of your void loop or the functionality of your sketch.

Pete.

here I tried to use Blynktimer. but result is same
This is my new code


// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "TMPLSYSoySsN"
#define BLYNK_DEVICE_NAME "GPS2"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
#define USE_WROVER_BOARD
//#define USE_TTGO_T7
//#define USE_ESP32C3_DEV_MODULE
//#define USE_ESP32S2_DEV_KIT

#include "BlynkEdgent.h"

#define SerialMon Serial

// Set serial for AT commands (to the module)
// Use Hardware Serial on Mega, Leonardo, Micro
#define SerialAT Serial1

#define TINY_GSM_MODEM_SIM7000
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
#define SerialAT Serial1

// See all AT commands, if wanted
// #define DUMP_AT_COMMANDS

// set GSM PIN, if any
#define GSM_PIN ""

// Your GPRS credentials, if any
const char apn[]  = "ucell";     //SET TO YOUR APN
const char gprsUser[] = "";
const char gprsPass[] = "";

String msg1;
String msg2;

#include <TinyGsmClient.h>
#include <SPI.h>
#include <SD.h>
#include <Ticker.h>

#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif

#define uS_TO_S_FACTOR      1000000ULL  // Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP       60          // Time ESP32 will go to sleep (in seconds)

#define UART_BAUD           9600
#define PIN_DTR             25
#define PIN_TX              27
#define PIN_RX              26
#define PWR_PIN             4

#define SD_MISO             2
#define SD_MOSI             15
#define SD_SCLK             14
#define SD_CS               13
#define LED_PIN             12

void myDelay(int del) {
  unsigned long myPrevMillis = millis();
  unsigned long myCurrentMillis = myPrevMillis;
  while (myCurrentMillis - myPrevMillis <= del) {
  myCurrentMillis = millis();
  }
}

void enableGPS(void)
{
    // Set SIM7000G GPIO4 LOW ,turn on GPS power
    // CMD:AT+SGPIO=0,4,1,1
    // Only in version 20200415 is there a function to control GPS power
    modem.sendAT("+SGPIO=0,4,1,1");
    modem.enableGPS();


}

void disableGPS(void)
{
    // Set SIM7000G GPIO4 LOW ,turn off GPS power
    // CMD:AT+SGPIO=0,4,1,0
    // Only in version 20200415 is there a function to control GPS power
    modem.sendAT("+SGPIO=0,4,1,0");
    modem.disableGPS();
}

void modemPowerOn()
{
    pinMode(PWR_PIN, OUTPUT);
    digitalWrite(PWR_PIN, LOW);
    myDelay(1000);    //Datasheet Ton mintues = 1S
    digitalWrite(PWR_PIN, HIGH);
}

void modemPowerOff()
{
    pinMode(PWR_PIN, OUTPUT);
    digitalWrite(PWR_PIN, LOW);
    myDelay(1500);    //Datasheet Ton mintues = 1.2S
    digitalWrite(PWR_PIN, HIGH);
}


void modemRestart()
{
    modemPowerOff();
    myDelay(1000);
    modemPowerOn();
}
const byte trig = 18;
const byte echo = 19;

BlynkTimer timer;
BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V0, V1, V2, V3, V4, V5, V6, V7);
}
void setup()
{
     // Set console baud rate
    SerialMon.begin(115200);

    myDelay(10);

    // Set LED OFF
    pinMode(LED_PIN, OUTPUT);
    digitalWrite(LED_PIN, HIGH);

    modemPowerOn();

    SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);

    myDelay(10000);
  BlynkEdgent.begin();
}

void loop() {
  if(Blynk.connected()){
  if (!modem.testAT()) {
    
        Serial.println("Failed to restart modem, attempting to continue without restarting");
        modemRestart();
        return;
    }

    msg1 = "Start positioning . Make sure to locate outdoors. The blue indicator light flashes to indicate positioning.";
   
    Serial.println(msg1);
    Blynk.virtualWrite(V0, msg1);   
    
    enableGPS();

    float lat,  lon;
    for (; 1 ;) {
        if (modem.getGPS(&lat, &lon)) {
            Serial.println("The location has been locked, the latitude and longitude are:");
            Serial.print("latitude:"); Serial.println(lat);
            Serial.print("longitude:"); Serial.println(lon);
            Blynk.virtualWrite(V1, String(lat, 6));   
            Blynk.virtualWrite(V2, String(lon, 6));
            break;
        }
        digitalWrite(LED_PIN, !digitalRead(LED_PIN));
        myDelay(2000);
    }

    disableGPS();

    Serial.println("/**********************************************************/");
    Serial.println("After the network test is complete, please enter the  ");
    Serial.println("AT command in the serial terminal.");
    Serial.println("/**********************************************************/\n\n");

   for (; 1 ;) {
        while (SerialAT.available()) {
            SerialMon.write(SerialAT.read());
        }
        while (SerialMon.available()) {
            SerialAT.write(SerialMon.read());
        }
    }
  }
  BlynkEdgent.run();
  timer.run();
  
}

Well, I’m not even going to look at your sketch again until your void loop looks like this…

void loop()
{  
  BlynkEdgent.run();
  timer.run();
}

and you aren’t using blocking while commands in functions that run on a regular basis.

Pete.