[SOLVED] Arduino or NodeMCU GPS tracking system on Map widget

Look through all the comments in this sketch and see how you go with it.

/* BlynkifyGPS.ino for https://community.blynk.cc/t/arduino-or-nodemcu-gps-tracking-system-on-map-widget/15739
  Sketch for physical GPS device connected to a decent ESP
  In map widget set SHOW MY LOCATION to NO unless you also want Smartphone actual location in addition to GPS device + ESP
*/
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;

TinyGPSPlus gps; // The TinyGPS++ object

SoftwareSerial ss(RXPin, TXPin);  // The serial connection to the GPS device

BlynkTimer timer;

char auth[] = "4bb191e6da5a438bb071ecca688732f3";
char ssid[] = "MikroTik Home";
char pass[] = "";
char server[] = "192.168.1.104";

//unsigned int move_index;         // moving index, to be used later
unsigned int move_index = 1;       // fixed location for now
bool simulation = false;           // set to true for simulation mode, else false. If IranHack is true this must be false
                                   // simulation location is in Egypt
                                   
bool IranHack = false;             // set to true if GPS is faulty, else false. If simulation is true this must be false

WidgetMap myMap(V5);

void setup()
{
  Serial.begin(115200);
  Serial.println();
  ss.begin(GPSBaud);
  Blynk.begin(auth, ssid, pass, server);
  timer.setInterval(5000L, checkGPS); // every 5s check if GPS is connected, only really needs to be done once
}

void checkGPS(){
  if (gps.charsProcessed() < 10)
  {
    Serial.println(F("No GPS detected: check wiring."));
    Blynk.virtualWrite(V10, "GPS ERROR");  // Value Display widget set as PUSH frequency on V10
    Blynk.virtualWrite(V7, "--------");    // Value Display widget set as PUSH frequency on V7 LATITUDE
    Blynk.virtualWrite(V8, "--------");    // Value Display widget set as PUSH frequency on V8 LONGITUDE
  }
}

void loop()
{
  delay(1000);  // NOT RECOMENDED
  if(simulation == true || IranHack == true)
  {
    displayInfo();  
  }
  else
  {
    while (ss.available() > 0)  // sketch displays information every time a new sentence is correctly encoded.
      if (gps.encode(ss.read()))
        displayInfo();
  }
  Blynk.run();
  timer.run();
}

void displayInfo()
{ 

  if (gps.location.isValid() || simulation == true || IranHack == true) 
  {
    //move_index++;                        // moving index, maybe use later
    Blynk.virtualWrite(V10, "GPS OK");
    float latitude = (gps.location.lat());  // removed , 6 as it can't be used for creating the float
    float longitude = (gps.location.lng()); // removed , 6 as it can't be used for creating the float

    if(latitude == 0 && longitude == 0 )    // checking to see if the 2 floats variables are correct
    {
      Blynk.virtualWrite(V7, "--------");   
      Blynk.virtualWrite(V8, "--------");
      Blynk.virtualWrite(V10, "0,0");
    }
 
    if(simulation == true)           // location in Egypt
    {
      latitude  = 30.000123;       // also used for debugging
      longitude = 30.000987;       // also used for debugging  
    }
    
    if(IranHack == true)
    {
      latitude  = 32.644824;
      longitude = 51.681746;
    }    
    Serial.print("LAT:  ");
    Serial.println(latitude, 6);  // float to x decimal places
    Serial.print("LONG: ");
    Serial.println(longitude, 6);
      
    Blynk.virtualWrite(V7, String(latitude, 6));   
    Blynk.virtualWrite(V8, String(longitude, 6));  

    myMap.location(move_index, latitude, longitude, "GPS_Location");
  }
  else
  {
    Serial.print(F("INVALID"));
    Blynk.virtualWrite(V10, "GPS INVALID");
    Blynk.virtualWrite(V7, "--------");   
    Blynk.virtualWrite(V8, "--------");  
  }

  Serial.println();
}
4 Likes

@Costas @Gunner
thanks a lot. the problem is solved :rose: I will never forget this great favor

update: the NodeMCU disconnecting from AP and cant keep connection

That will be because the main loop() is not really suitable for Blynk.
I just wanted to ensure your GPS and the Map widget were working OK.
Now you know the basic system works the fine tuning can begin.

1 Like

thanks. which line is need tuning ?

@ErfanDL this area in loop() is what needs to be fine tuned but it’s a little difficult for me without a GPS device, hence some of the simulation code in the sketch:

  delay(1000);  // NOT RECOMENDED
  if(simulation == true || IranHack == true)
  {
    displayInfo();  
  }
  else
  {
    while (ss.available() > 0)  // sketch displays information every time a new sentence is correctly encoded.
      if (gps.encode(ss.read()))
        displayInfo();
  }
1 Like

with uncommenting delay(1000); device status rapidly online and offline but location updating on map widget

delay() should not normally be used at all in a Blynk loop() and you should call the GPS function at timed intervals. This might be a little difficult for you as a noob to Arduino and Blynk as you need to be sure the GPS has the full stream before processing the data etc.

With trial and error you could try reducing the 1000 to see if that helps.

If you had more Blynk experience you could change the delay() time with a slider rather than changing the sketch.

1 Like

Can I create new topic and ask question about disconnecting problem ? ( Of course if you permit me )

You could but really it’s likely to be very specific to the GPS sketch I provided for you.

1 Like

@ErfanDL I have created a 30s timed interval to read the GPS data and removed everything from loop().
See how this goes:

/* BlynkifyGPSv2.ino for https://community.blynk.cc/t/arduino-or-nodemcu-gps-tracking-system-on-map-widget/15739
  Sketch for physical GPS device connected to a decent ESP
  In map widget set SHOW MY LOCATION to NO unless you also want Smartphone actual location in addition to GPS device + ESP
*/
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;

TinyGPSPlus gps; // The TinyGPS++ object

SoftwareSerial ss(RXPin, TXPin);  // The serial connection to the GPS device

BlynkTimer timer;

char auth[] = "4bb191e6da5a438bb071ecca688732f3";
char ssid[] = "MikroTik Home";
char pass[] = "";
char server[] = "192.168.1.104";

//unsigned int move_index;         // moving index, to be used later
unsigned int move_index = 1;       // fixed location for now
bool simulation = false;           // set to true for simulation mode, else false. If IranHack is true this must be false
                                   // simulation location is in Egypt
                                   
bool IranHack = false;             // set to true if GPS is faulty, else false. If simulation is true this must be false

WidgetMap myMap(V5);

void setup()
{
  Serial.begin(115200);
  Serial.println();
  ss.begin(GPSBaud);
  Blynk.begin(auth, ssid, pass, server);

  // disable GPS device checking for now
  //timer.setInterval(5000L, checkGPS); // every 5s check if GPS is connected, only really needs to be done once

  timer.setInterval(30000L, readGPS); // read GPS data every 30s
  readGPS();                          // read GPS data on each reboot after connection to Blynk server
}

void readGPS(){
  if(simulation == true || IranHack == true)
  {
    displayInfo();  
  }
  else
  {
    while (ss.available() > 0)  // sketch displays information every time a new sentence is correctly encoded.
      if (gps.encode(ss.read()))
        displayInfo();
  }  
}

void checkGPS(){
  if (gps.charsProcessed() < 10)
  {
    Serial.println(F("No GPS detected: check wiring."));
    Blynk.virtualWrite(V10, "GPS ERROR");  // Value Display widget set as PUSH frequency on V10
    Blynk.virtualWrite(V7, "--------");    // Value Display widget set as PUSH frequency on V7 LATITUDE
    Blynk.virtualWrite(V8, "--------");    // Value Display widget set as PUSH frequency on V8 LONGITUDE
  }
}

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

void displayInfo()
{ 

  if (gps.location.isValid() || simulation == true || IranHack == true) 
  {
    //move_index++;                        // moving index, maybe use later
    Blynk.virtualWrite(V10, "GPS OK");
    float latitude = (gps.location.lat());  // removed , 6 as it can't be used for creating the float
    float longitude = (gps.location.lng()); // removed , 6 as it can't be used for creating the float

    if(latitude == 0 && longitude == 0 )    // checking to see if the 2 floats variables are correct
    {
      Blynk.virtualWrite(V7, "--------");   
      Blynk.virtualWrite(V8, "--------");
      Blynk.virtualWrite(V10, "0,0");
    }
  
    if(simulation == true)         // location in Egypt
    {
      latitude  = 30.000123;       // also used for debugging
      longitude = 30.000987;       // also used for debugging  
    }
    
    if(IranHack == true)
    {
      latitude  = 32.644824;
      longitude = 51.681746;
    }    
    Serial.print("LAT:  ");
    Serial.println(latitude, 6);  // float to x decimal places
    Serial.print("LONG: ");
    Serial.println(longitude, 6);
      
    Blynk.virtualWrite(V7, String(latitude, 6));   
    Blynk.virtualWrite(V8, String(longitude, 6));  

    myMap.location(move_index, latitude, longitude, "GPS_Location");
  }
  else
  {
    Serial.print(F("INVALID"));
    Blynk.virtualWrite(V10, "GPS INVALID");
    Blynk.virtualWrite(V7, "--------");   
    Blynk.virtualWrite(V8, "--------");  
  }

  Serial.println();
}
1 Like

thanks. the connection problem is solved but there is no GPS location and there is nothing show on V7 V8 V10

Send me a GPS device then.

2 Likes

Was this joke or serious ?:joy:

whats different between checkgps and readgps ?

If you are not able to spend time debugging your project yourself you need someone that has similar hardware. Blynkers can offer suggestions based on their Arduino, Blynk and GPS experiences but without sending you dozens and dozens of different sketch versions to test it is very time consuming without the hardware.[quote=“ErfanDL, post:75, topic:15739, full:true”]
whats different between checkgps and readgps ?
[/quote]

checkGPS() was basically to see if you had a working GPS device. readGPS() processes the data from the GPS device.

1 Like

OK. for now I’m using below sketch. it’s disconnect and reconnecting rapidly but it’s can running my job

/* BlynkifyGPS.ino for https://community.blynk.cc/t/arduino-or-nodemcu-gps-tracking-system-on-map-widget/15739
  Sketch for physical GPS device connected to a decent ESP
  In map widget set SHOW MY LOCATION to NO unless you also want Smartphone actual location in addition to GPS device + ESP
*/
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;

TinyGPSPlus gps; // The TinyGPS++ object

SoftwareSerial ss(RXPin, TXPin);  // The serial connection to the GPS device

BlynkTimer timer;

char auth[] = "6075f93a98734b3abddb3c3ba6babf08";
char ssid[] = "";
char pass[] = "00471666302047PLX";
char server[] = "192.168.1.104";

//unsigned int move_index;         // moving index, to be used later
unsigned int move_index = 1;       // fixed location for now
bool simulation = false;           // set to true for simulation mode, else false. If IranHack is true this must be false
                                   // simulation location is in Egypt
                                   
bool IranHack = false;             // set to true if GPS is faulty, else false. If simulation is true this must be false

WidgetMap myMap(V5);

void setup()
{
  Serial.begin(115200);
  Serial.println();
  ss.begin(GPSBaud);
  Blynk.begin(auth, ssid, pass, server);
  timer.setInterval(5000L, checkGPS); // every 5s check if GPS is connected, only really needs to be done once
}

void checkGPS(){
  if (gps.charsProcessed() < 10)
  {
    Serial.println(F("No GPS detected: check wiring."));
    Blynk.virtualWrite(V10, "GPS ERROR");  // Value Display widget set as PUSH frequency on V10
    Blynk.virtualWrite(V7, "--------");    // Value Display widget set as PUSH frequency on V7 LATITUDE
    Blynk.virtualWrite(V8, "--------");    // Value Display widget set as PUSH frequency on V8 LONGITUDE
  }
}

void loop()
{
  //delay(1000);  // NOT RECOMENDED
  if(simulation == true || IranHack == true)
  {
    displayInfo();  
  }
  else
  {
    while (ss.available() > 0)  // sketch displays information every time a new sentence is correctly encoded.
      if (gps.encode(ss.read()))
        displayInfo();
  }
  Blynk.run();
  timer.run();
}

void displayInfo()
{ 

  if (gps.location.isValid() || simulation == true || IranHack == true) 
  {
    //move_index++;                        // moving index, maybe use later
    Blynk.virtualWrite(V10, "GPS OK");
    float latitude = (gps.location.lat());  // removed , 6 as it can't be used for creating the float
    float longitude = (gps.location.lng()); // removed , 6 as it can't be used for creating the float

    if(latitude == 0 && longitude == 0 )    // checking to see if the 2 floats variables are correct
    {
      Blynk.virtualWrite(V7, "--------");   
      Blynk.virtualWrite(V8, "--------");
      Blynk.virtualWrite(V10, "0,0");
    }
 
    if(simulation == true)           // location in Egypt
    {
      latitude  = 30.000123;       // also used for debugging
      longitude = 30.000987;       // also used for debugging  
    }
    
    if(IranHack == true)
    {
      latitude  = 32.644824;
      longitude = 41.681746;
    }    
    Serial.print("LAT:  ");
    Serial.println(latitude, 6);  // float to x decimal places
    Serial.print("LONG: ");
    Serial.println(longitude, 6);
      
    Blynk.virtualWrite(V7, String(latitude, 6));   
    Blynk.virtualWrite(V8, String(longitude, 6));  

    myMap.location(move_index, latitude, longitude, "GPS_Location");
  }
  else
  {
    Serial.print(F("INVALID"));
    Blynk.virtualWrite(V10, "GPS INVALID");
    Blynk.virtualWrite(V7, "--------");   
    Blynk.virtualWrite(V8, "--------");  
  }

  Serial.println();
}

@ErfanDL reading the spec of your hardware suggests data can be read at 1s intervals.

Go back to the previous sketch where you get no disconnects but no data and try this readGPS():
Paste SM data.

void readGPS(){
  long startTime = millis();
  long counter = 0;
  Serial.print("Started reading GPS data at ");  
  Serial.println(startTime); 
   
  if(simulation == true || IranHack == true)
  {
    long loopmax = 100L;  // max 2,147,483,647 or double this if usigned
    while (counter < loopmax)   
    { 
      counter++;
      Serial.print(".");
      delay(20);           // pause 10ms (1s total) 20ms (2s total)
    }
    Serial.println();
    //counter = 0;   // not required as counter is redefined each time readGPS() is called
    displayInfo();  
  }
  else
  {
    while (ss.available() > 0)  // sketch displays information every time a new sentence is correctly encoded.
      if (gps.encode(ss.read()))
        displayInfo();
  }
  Serial.print("Finished reading GPS data at ");
  Serial.print(millis());
  Serial.print(" and it took ");
  Serial.print(millis() - startTime);
  Serial.println("ms.");
  Serial.println("-----------------------------------------------------------");
  Serial.println();   
}

The constantly reconnecting sketch is not really a solution.

1 Like

I’m do it and get error in line 75 when compile

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  Output any data on Map widget!

  App project setup:
    Map widget on V1
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

static const int RXPin = 12, TXPin = 13;
static const uint32_t GPSBaud = 9600;

TinyGPSPlus gps;
BlynkTimer timer;

SoftwareSerial ss(RXPin, TXPin);

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "MikroTik Home";
char pass[] = "";
char server[] = "192.168.1.104";

WidgetMap myMap(V5);

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, server);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
  timer.setInterval(1000L, getCoordinates);
  Blynk.virtualWrite(V5, millis() / 1000);
  myMap.clear();
}

void readGPS(){
  long startTime = millis();
  long counter = 0;
  Serial.print("Started reading GPS data at ");  
  Serial.println(startTime); 
   
  if(simulation == true || IranHack == true)
  {
long loopmax = 100L;  // max 2,147,483,647 or double this if usigned
while (counter < loopmax)   
{ 
  counter++;
  Serial.print(".");
  delay(20);           // pause 10ms (1s total) 20ms (2s total)
}
Serial.println();
//counter = 0;   // not required as counter is redefined each time readGPS() is called
displayInfo();  
  }
  else
  {
while (ss.available() > 0)  // sketch displays information every time a new sentence is correctly encoded.
  if (gps.encode(ss.read()))
    displayInfo();
  }
  Serial.print("Finished reading GPS data at ");
  Serial.print(millis());
  Serial.print(" and it took ");
  Serial.print(millis() - startTime);
  Serial.println("ms.");
  Serial.println("-----------------------------------------------------------");
  Serial.println();   
}

void getCoordinates()
{
myMap.location(1, gps.location.lat(), gps.location.lng(), "value");
}

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

You need to use my version of the sketch as yours is missing several variables.

1 Like

it’s your sketch version below error in line 73

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  Output any data on Map widget!

  App project setup:
    Map widget on V1
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

static const int RXPin = 12, TXPin = 13;
static const uint32_t GPSBaud = 9600;

TinyGPSPlus gps;
BlynkTimer timer;

SoftwareSerial ss(RXPin, TXPin);

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "MikroTik Home";
char pass[] = "";
char server[] = "192.168.1.104";

WidgetMap myMap(V5);

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, server);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
  timer.setInterval(1000L, getCoordinates);
}

void readGPS(){
  long startTime = millis();
  long counter = 0;
  Serial.print("Started reading GPS data at ");  
  Serial.println(startTime); 
   
  if(simulation == true || IranHack == true)
  {
long loopmax = 100L;  // max 2,147,483,647 or double this if usigned
while (counter < loopmax)   
{ 
  counter++;
  Serial.print(".");
  delay(20);           // pause 10ms (1s total) 20ms (2s total)
}
Serial.println();
//counter = 0;   // not required as counter is redefined each time readGPS() is called
displayInfo();  
  }
  else
  {
while (ss.available() > 0)  // sketch displays information every time a new sentence is correctly encoded.
  if (gps.encode(ss.read()))
    displayInfo();
  }
  Serial.print("Finished reading GPS data at ");
  Serial.print(millis());
  Serial.print(" and it took ");
  Serial.print(millis() - startTime);
  Serial.println("ms.");
  Serial.println("-----------------------------------------------------------");
  Serial.println();   
}

unsigned int move_index;

void getCoordinates()
{
   Blynk.virtualWrite(V6, millis() / 1000);
   move_index++;
   float latitude = (gps.location.lat(), 5);
   float longitude = (gps.location.lng(), 4);
   Serial.println(String(latitude, 5));
   Serial.println(String(longitude, 4));
   Blynk.virtualWrite(V7, String(latitude, 5));
   Blynk.virtualWrite(V8, String(latitude, 4));
   myMap.location(move_index, latitude, longitude, "Iran");
}

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