Blynk disconnect randomly

Hi there,

I have a GPS Tracker project wich causes me problem. All seems to work and Blynk connects. But after a moment it disconnect and goes offline. Even if I use a 18650 battery or a lab generator.

I use an ESP32 + SIM800L + NEO6M

Here is my code, if you find something wrong.

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPLS6-Qv61A"
#define BLYNK_DEVICE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "xxx"

// Select your modem:
#define TINY_GSM_MODEM_SIM800
//#define TINY_GSM_MODEM_SIM900
//#define TINY_GSM_MODEM_M590
//#define TINY_GSM_MODEM_A6
//#define TINY_GSM_MODEM_A7
//#define TINY_GSM_MODEM_BG96
//#define TINY_GSM_MODEM_XBEE

// Default heartbeat interval for GSM is 60
// If you want override this value, uncomment and set this option:
//#define BLYNK_HEARTBEAT 15

#include <TinyGPS++.h> //https://github.com/mikalhart/TinyGPSPlus
#include <TinyGsmClient.h> //https://github.com/vshymanskyy/TinyGSM
#include <BlynkSimpleTinyGSM.h> //https://github.com/blynkkk/blynk-library


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxx";

// Your GPRS credentials
// Leave empty, if missing user or pass
char apn[]  = "iot.1nce.net";
//char apn[]  = "symamobile.com";
//char apn[]  = "free";
char user[] = "";
char pass[] = "";


//sender phone number with country code.
//not gsm module phone number
//const String PHONE = "Enter_Your_Phone_Number";

//GSM Module Settings
//GSM Module RX pin to ESP32 2
//GSM Module TX pin to ESP32 4
#define rxPin 16
#define txPin 17
HardwareSerial sim800(1);
TinyGsm modem(sim800);

//GPS Module Settings
//GPS Module RX pin to ESP32 17
//GPS Module TX pin to ESP32 16
#define RXD2 3
#define TXD2 1
HardwareSerial neogps(2);
TinyGPSPlus gps;
BlynkTimer timer;

void setup() {
   
  //Set Serial monitor baud rate
  Serial.begin(115200);
  Serial.println("esp32 serial initialize");
  delay(10);

  //Set GSM module baud rate
  sim800.begin(9600, SERIAL_8N1, rxPin, txPin);
  Serial.println("SIM800L serial initialize");
  delay(3000);
  
 //Set GPS module baud rate
  neogps.begin(9600, SERIAL_8N1, RXD2, TXD2);
  Serial.println("neogps serial initialize");
  delay(10); 
   
  // Restart takes quite some time
  // To skip it, call init() instead of restart()
  Serial.println("Initializing modem...");
  modem.restart();

  // Unlock your SIM card with a PIN
  //modem.simUnlock("1234");

  Blynk.begin(auth, modem, apn, user, pass);
  timer.setInterval(5000L, sendToBlynk);
}


void loop() {
  
 while(neogps.available())
  {
    if (gps.encode(neogps.read()))
    {
      sendToBlynk();
    }
  }

  Blynk.run();
  timer.run();
  
} //main loop ends


void sendToBlynk()
{

  if (gps.location.isValid() )
  {
    //get latitude and longitude
    double latitude = gps.location.lat();
    double longitude = gps.location.lng();
    //get speed
    float speed = gps.speed.kmph();
    //get number of satellites
    float satellites = gps.satellites.value();
    //get altitude
    float altitude = gps.altitude.meters();

    Blynk.virtualWrite(V0, longitude, latitude);
    Blynk.virtualWrite(V1, String(latitude, 6));
    Blynk.virtualWrite(V2, String(longitude, 6));        
    Blynk.virtualWrite(V3, speed);
    Blynk.virtualWrite(V4, satellites);
    Blynk.virtualWrite(V5, altitude);

  }
}

Thanks in advance for your futures answers.
Franck

I specify that my project don’t move from start until it goes offline and I always have network with my SIM card and the NEO6M is still picking up the sattelites

You appear to be using the serial UART0 Tx pin (which is used by Serial.print() commands) for something else as well.

Pete.

Hi Pete and thanks for your answer.
I think I don’t understand too much but I tried to comment all the lines with Serial.println and the problem is still there.
This time the connection goes online and offline multiple times to stay offline after 3-4 times

You should keep your void loop clean, read this
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Hi John93 and thanks for your answer.

The Timer (5000L) is not enough?

We are not taking about the timer.
The ideal blynk void loop should look like this

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

Can’t you use different pins instead of pin 1?

Pete.

Ok. I made this modification. But now I can’t go Online anymore :frowning:

Yes I can use other pins. I only have the NEO6M and the SIM800L. I can use any pins?

It’s not simply a case of deleting all the other code from your void loop, you need to read the “keep your void loop clean” document and understand it, then make the appropriate changes.

I’d suggest that you read this…

Pete.

1 Like

What I understood with the “keep your void loop clean” is that if I have a timer I must to relate it with a function. Mine is sendToBlynk
In this function I have my GPS Code then every 5 seconds my timer send to the sendToBlynk function.
Am I right?

Here is my new code:

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPLS6-Qv61A"
#define BLYNK_DEVICE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "xxx"

// Select your modem:
#define TINY_GSM_MODEM_SIM800
//#define TINY_GSM_MODEM_SIM900
//#define TINY_GSM_MODEM_M590
//#define TINY_GSM_MODEM_A6
//#define TINY_GSM_MODEM_A7
//#define TINY_GSM_MODEM_BG96
//#define TINY_GSM_MODEM_XBEE

// Default heartbeat interval for GSM is 60
// If you want override this value, uncomment and set this option:
//#define BLYNK_HEARTBEAT 15

#include <TinyGPS++.h> //https://github.com/mikalhart/TinyGPSPlus
#include <TinyGsmClient.h> //https://github.com/vshymanskyy/TinyGSM
#include <BlynkSimpleTinyGSM.h> //https://github.com/blynkkk/blynk-library


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxx";

// Your GPRS credentials
// Leave empty, if missing user or pass
char apn[]  = "iot.1nce.net";
//char apn[]  = "symamobile.com";
//char apn[]  = "free";
char user[] = "";
char pass[] = "";

//GSM Module Settings
//GSM Module RX pin to ESP32 2
//GSM Module TX pin to ESP32 4
#define rxPin 18
#define txPin 19
HardwareSerial sim800(1);
TinyGsm modem(sim800);

//GPS Module Settings
//GPS Module RX pin to ESP32 17
//GPS Module TX pin to ESP32 16
#define RXD2 16
#define TXD2 17
HardwareSerial neogps(2);
TinyGPSPlus gps;
BlynkTimer timer;

void setup() {
   
  //Set Serial monitor baud rate
  Serial.begin(115200);
  //Serial.println("esp32 serial initialize");
  delay(10);

  //Set GPS module baud rate
  neogps.begin(9600, SERIAL_8N1, RXD2, TXD2);
  //Serial.println("neogps serial initialize");
  delay(10); 

  //Set GSM module baud rate
  sim800.begin(9600, SERIAL_8N1, rxPin, txPin);
  //Serial.println("SIM800L serial initialize");
  delay(3000);
   
  // 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);
  timer.setInterval(1000L, sendToBlynk);
}


void loop() {

  Blynk.run();
  timer.run();
  
/* while(neogps.available())
  {
    if (gps.encode(neogps.read()))
    {
      sendToBlynk();
    }
  }

  delay(5000);*/
  
} //main loop ends


void sendToBlynk()
{
while(neogps.available())
  {
    if (gps.encode(neogps.read()))
    {
      
    
  if (gps.location.isValid())
  {
    //get latitude and longitude
    double latitude = gps.location.lat();
    double longitude = gps.location.lng();
    //get speed
    float speed = gps.speed.kmph();
    //get number of satellites
    float satellites = gps.satellites.value();
    //get altitude
    float altitude = gps.altitude.meters();

    Blynk.virtualWrite(V0, longitude, latitude);
    Blynk.virtualWrite(V1, String(latitude, 6));
    Blynk.virtualWrite(V2, String(longitude, 6));        
    Blynk.virtualWrite(V3, speed);
    Blynk.virtualWrite(V4, satellites);
    Blynk.virtualWrite(V5, altitude);

  }
  }
  }
}

Did you see anything wrong?

Without some clues from you about how this revised code is working (or not) in practice then it’s difficult to say.

I’m confused about your COM ports, but you havent shared any debug serial output or said any more than:

which doesn’t really help us to help you.

Pete.

Sorry, I try to make it as simple as possible because english is not my mother tongue, I’m French. I try to make my best to make me understand :slight_smile:

The debug serial output is a little problem either… indeed, when I transfer the code to the ESP I have nothing showing in the serial monitor. But when I press the reset button on the ESP I have a little bit of information but not enough to debug.

I only have that:

ets Jun 8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5856
entry 0x400806a8

In fact, after “entry 0x400806a8” I should have the informations about modem init etc. but I have nothing of that and I don’t now why. I missed something certainly…

Okay, so you have a non-Blynk related issue that you need to resolve first.
This may be associated with your ESP_32 Core version, the upload settings (DIO for example) that you’ve chosen, the board type that you’ve chosen etc etc.

You need to fix these issues first before you can hope to get connected to Blynk.

Pete.