Constant reconnection Arduino+GSM+GPS

Hello!

I am making a project that shows the blynk the position of the person. For this I use an Arduino (for the test an Arduino Uno), a SIM800L and a Neo-6M. The connection with SIM800L only works fine, but when I add the Neo-6M GPS to it, it is constantly trying to reconnect to Blynk, and I don’t know where the fault may be.

This is my code:

#define BLYNK_PRINT Serial


// Select your modem:
#define TINY_GSM_MODEM_SIM800


#include <TinyGsmClient.h>
#include <BlynkSimpleTinyGSM.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h> 

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

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

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

// or Software Serial on Uno, Nano
SoftwareSerial SerialAT(2, 3); // RX, TX
TinyGsm modem(SerialAT);


SoftwareSerial Serial2(4, 5 ); // RX, TX
TinyGPSPlus gps;

WidgetMap myMap(V0);
BlynkTimer timer;
int pointIndex = 1;

void setup()
{
  // Debug console
  Serial.begin(115200);
  delay(10);

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

  // Set GSM module baud rate
  SerialAT.begin(9600);
  delay(3000);

  // 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("0244");

  Blynk.begin(auth, modem, apn, user, pass, "MYPRIVATEIP", MYPORT);
}

void loop()
{
  while(Serial2.available())
  {
    if (gps.encode(Serial2.read()))
    {
      sendToBlynk();
    }
  }
  Blynk.run();

  Blynk.virtualWrite(V4, "KAIXO!!!");
  
}

void sendToBlynk()
{

  if (gps.location.isValid() )
  {
    //get latitude and longitude
    float latitude = (gps.location.lat());
    float longitude = (gps.location.lng());
    //get
    float speed = gps.speed.kmph();
    //get number of satellites
    float satellites = gps.satellites.value();
    
    //Serial.print("Latitude:  ");
    //Serial.println(latitude, 6);
    //Serial.print("Longitude: ");
    //Serial.println(longitude, 6);
    //Serial.print("Speed: ");
    //Serial.println(speed, 6);    
    
    Blynk.virtualWrite(V1, String(latitude, 6));
    Blynk.virtualWrite(V2, String(longitude, 6));
    myMap.location(pointIndex, latitude, longitude, "GPS_Location");
    
    Blynk.virtualWrite(V3, speed);

  }
}

A few comments…

99.9% of users of this forum are now using Blynk IoT. When you create a Legacy local server post you really need to flag this up but choosing the appropriate key words for your post, and spelling this out at the very beginning of your post.

Revealing your public static IP address and custom port on a public forum probably isn’t sensible. Revealing your auth token isn’t normally an issue with a local server, but it is when you also give away all of your other secrets!

Posting screenshots of your serial monitor output is a bad idea. Teach yourself how to copy the text from your serial monitor and to paste it the same as you would with code - with triple backticks at the beginning and end.

I’f you are using Blynk Legacy you should be using version 0.6.1 of the Blynk C++ library.

Using the name Serial2 for a SoftwareSerial port is a really bad idea. Serial2 is a reserved keyword for boards with more hardware serial poets, so the compiler is likely to get confused by this (this may be the cause of your problem).

Using blocking while commands with Blynk is generally a bad idea (this may also be the cause of your problem).

You should NEVER put Blynk.virtualWrite() commands in your void loop, or in a function that is called directly from your void loop - unless you are using some method to limit the frequency at which this function is called (this may also be the cause of your problem).

Pete.

1 Like

Thank you very much for your help. I have made the changes in the post and right now I am going to try to make the changes in the program.

Again, thank you so much.

Hello!

Sorry, I don’t know what is the difference between Blynk Legacy and IoT, I use the one I was taught in school :frowning: I have made the changes, although I don’t understand where I shouldn’t put the Blynk.virtualWrite() command, if in Void loop or in the created function of “sentToBlynk” I have removed the one that had in void loop.
However, despite the changes, it continues to do the same.

#define BLYNK_PRINT Serial


// Select your modem:
#define TINY_GSM_MODEM_SIM800


#include <TinyGsmClient.h>
#include <BlynkSimpleTinyGSM.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h> 

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

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

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

// or Software Serial on Uno, Nano
SoftwareSerial SerialAT(2, 3); // RX, TX
TinyGsm modem(SerialAT);


SoftwareSerial SerialGPS(4, 5 ); // RX, TX
TinyGPSPlus gps;

WidgetMap myMap(V0);
BlynkTimer timer;
int pointIndex = 1;

void setup()
{
  // Debug console
  Serial.begin(115200);
  delay(10);

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

  // Set GSM module baud rate
  SerialAT.begin(9600);
  delay(3000);

  // 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("0244");

  Blynk.begin(auth, modem, apn, user, pass, "MyIPDirection", MyPort);
}

void loop()
{
   if (SerialGPS.available())
  {
    if (gps.encode(SerialGPS.read()))
    {
      sendToBlynk();
    }
  }
  Blynk.run();
  
}

void sendToBlynk()
{

  if (gps.location.isValid() )
  {
    //get latitude and longitude
    float latitude = (gps.location.lat());
    float longitude = (gps.location.lng());
    //get
    float speed = gps.speed.kmph();
    //get number of satellites
    float satellites = gps.satellites.value();
    
    //Serial.print("Latitude:  ");
    //Serial.println(latitude, 6);
    //Serial.print("Longitude: ");
    //Serial.println(longitude, 6);
    //Serial.print("Speed: ");
    //Serial.println(speed, 6);    
    
    Blynk.virtualWrite(V1, String(latitude, 6));
    Blynk.virtualWrite(V2, String(longitude, 6));
    myMap.location(pointIndex, latitude, longitude, "GPS_Location");
    
    Blynk.virtualWrite(V3, speed);

  }
}
neogps serial initialize
neogps serial initialize
Initializing modem...
[7953] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.2.0 on Arduino Uno

 #StandWithUkraine    https://bit.ly/swua


[7964] Modem init...
[8171] Connecting to network...
[15540] Network: Retevision Movil
[15540] Connecting to inet.es ...
[23171] Connected to GPRS
[23243] Connecting to MYIPDIRECTION:MYPORT
[24644] Ready (ping: 562ms).
[31799] Connecting to MYIPDIRECTION:MYPORT
[108948] Connecting to MYIPDIRECTION:MYPORT
[110339] Ready (ping: 369ms).
[117493] Connecting to MYIPDIRECTION:MYPORT

This is my montage at the moment, for testing.

Blynk Legacy is retired and no longer supported. The Legacy apps have been removed from the app/play stores. The local server code is no longer being updated to fix security vulnerabilities and Java compatibility issues.
You are using Blynk Legacy.

But you are still using version 1.x.x of the Blynk library, not 0.6.1
And you are still calling a function with Blynk.virtualWrites in it directly from your void loop without any code to prevent server flooding.

You should read this…

Also, breadboards are extremely unreliable ways of testing hardware setups. It’s very easy for connections to become dislodged, or the high resistance of the connections to cause issues.

Pete.

1 Like

Sorry for being so tedious…

I have version 0.6.1 of the Blynk library installed, but 1.2.0 jumps and I don’t know how to change it to only run the old version.
I think I’ve already made the necessary changes using the timer function to get the data out of the loop, but it’s still the same.

#define BLYNK_PRINT Serial


// Select your modem:
#define TINY_GSM_MODEM_SIM800


#include <TinyGsmClient.h>
#include <BlynkSimpleTinyGSM.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h> 

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

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

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

// or Software Serial on Uno, Nano
SoftwareSerial SerialAT(2, 3); // RX, TX
TinyGsm modem(SerialAT);


SoftwareSerial SerialGPS(4, 5 ); // RX, TX
TinyGPSPlus gps;

WidgetMap myMap(V0);
BlynkTimer timer;
int pointIndex = 1;

void setup()
{
  // Debug console
  Serial.begin(115200);
  delay(10);

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

  // Set GSM module baud rate
  SerialAT.begin(9600);
  delay(3000);

  // 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("0244");

  Blynk.begin(auth, modem, apn, user, pass, "MyIPAddress", MyPort);

  timer.setInterval(1000L, LeerGPS); //timer will run every sec 

  
}

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

void LeerGPS()
{
  if (SerialGPS.available())
  {
    if (gps.encode(SerialGPS.read()))
    {
      sendToBlynk();
    }
  }
}

void sendToBlynk()
{

  if (gps.location.isValid() )
  {
    //get latitude and longitude
    float latitude = (gps.location.lat());
    float longitude = (gps.location.lng());
    //get
    float speed = gps.speed.kmph();
    //get number of satellites
    float satellites = gps.satellites.value();
    
    //Serial.print("Latitude:  ");
    //Serial.println(latitude, 6);
    //Serial.print("Longitude: ");
    //Serial.println(longitude, 6);
    //Serial.print("Speed: ");
    //Serial.println(speed, 6);    
    
    Blynk.virtualWrite(V1, String(latitude, 6));
    Blynk.virtualWrite(V2, String(longitude, 6));
    myMap.location(pointIndex, latitude, longitude, "GPS_Location");
    
    Blynk.virtualWrite(V3, speed);
  }
}
neogps serial initialize
Initializing modem...
[14199] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.2.0 on Arduino Uno

 #StandWithUkraine    https://bit.ly/swua


[14211] Modem init...
[14417] Connecting to network...
[20623] Network: Retevision Movil
[20623] Connecting to inet.es ...
[29178] Connected to GPRS
[29250] Connecting to MYIPDIRECTION:MYPORT
[30395] Ready (ping: 385ms).
[35431] Connecting to MYIPDIRECTION:MYPORT
[36959] Ready (ping: 370ms).
[41995] Connecting to MYIPDIRECTION:MYPORT
[43438] Ready (ping: 295ms).
[48474] Connecting to MYIPDIRECTION:MYPORT
[49934] Ready (ping: 293ms).
[54970] Connecting to MYIPDIRECTION:MYPORT
[56464] Ready (ping: 369ms).
[61500] Connecting to MYIPDIRECTION:MYPORT
[63000] Ready (ping: 369ms).
[68036] Connecting to MYIPDIRECTION:MYPORT
[69478] Ready (ping: 288ms).
[74514] Connecting to MYIPDIRECTION:MYPORT
[76051] Ready (ping: 385ms).
[81087] Connecting to MYIPDIRECTION:MYPORT
[82589] Ready (ping: 368ms).
[87625] Connecting to MYIPDIRECTION:MYPORT
[89160] Ready (ping: 363ms).
[94196] Connecting to MYIPDIRECTION:MYPORT
[95668] Ready (ping: 296ms).

Delete the library folder.

I’m not sure if your new sketch will actually read the data from the GPS in the way you hope. Maybe you should add some serial print commands to check that you are actually receiving data, and to understand more about the program flow.

Pete.

1 Like

Thank you very much for your help, from the bottom of my heart. I have managed to remove the most current library and now it is with version 0.6.1 and it works! There are times when it disconnects but immediately reconnects. As for the program in question, you’re right, it doesn’t work as I would like, but I already think about it and look for solutions.

Again, thank you very much!

1 Like