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

@Gunner
I need a sketch for reporting arduino or esp8266 GPS location to Blynk Maps widget. please help me. this project is very important for me :((

What you keep asking for is NOT Blynk specific… so just like I and others have already suggested… find an Arduino/ESP8266 sketch, on this thing called GOOGLE, that works for your GPS module (do you even have one?) and Arduino, ESP, NodeMCU, or whatever it is… The sketch will most likely have a “final” output of the LAT and LON going to a print statement (or a LCD display).

Confirm it works perfectly with your hardware, without Blynk, then start working on the Blynkified format mods required (WiFi, Auth, Timers, etc), and simply output the LAT/LON data to a Display Widget or the Map Widget, etc.

Only then can we further assist you with advice and perhaps even some assistance with tricky code issues (once you have something of your own to work with), but you must get this started at your end. You are a Network engineer, so this shouldn’t be all that difficult :wink:

1 Like

this code worked with my GPS module:

#include <SoftwareSerial.h>
 
static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;
 
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
 
void setup()
{
  Serial.begin(115200);
  ss.begin(GPSBaud);
}
 
void loop()
{
  // Output raw GPS data to the serial monitor
  while (ss.available() > 0){
    Serial.write(ss.read());
  }
}

and this code repor LAT LON on LCD

#include <LiquidCrystal.h>
#include <string.h>
#include <ctype.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int rxPin = 0; // RX pin
int txPin = 1; // TX pin
int byteGPS=-1;
char cmd[7] = "$GPRMC";
int counter1 = 0; // counts how many bytes were received (max 300)
int counter2 = 0; // counts how many commas were seen
int offsets[13];
char buf[300] = "";

/**
 * Setup display and gps
 */
void setup() {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  Serial.begin(4800);
  lcd.begin(16, 2);
  lcd.print("waiting for gps");
  offsets[0] = 0;
  reset();
}

void reset() {
  counter1 = 0;
  counter2 = 0;
}

int get_size(int offset) {
  return offsets[offset+1] - offsets[offset] - 1;
}

int handle_byte(int byteGPS) {
  buf[counter1] = byteGPS;
  Serial.print((char)byteGPS);
  counter1++;
  if (counter1 == 300) {
    return 0;
  }
  if (byteGPS == ',') {
    counter2++;
    offsets[counter2] = counter1;
    if (counter2 == 13) {
      return 0;
    }
  }
  if (byteGPS == '*') {
    offsets[12] = counter1;
  }

  // Check if we got a <LF>, which indicates the end of line
  if (byteGPS == 10) {
    // Check that we got 12 pieces, and that the first piece is 6 characters
    if (counter2 != 12 || (get_size(0) != 6)) {
      return 0;
    }

    // Check that we received $GPRMC
    for (int j=0; j<6; j++) {
      if (buf[j] != cmd[j]) {
        return 0;
      }
    }

    // Check that time is well formed
    if (get_size(1) != 10) {
      return 0;
    }

    // Check that date is well formed
    if (get_size(9) != 6) {
      return 0;
    }

    // TODO: compute and validate checksum

    // TODO: handle timezone offset

    // print time
    lcd.clear();
    for (int j=0; j<6; j++) {
      lcd.print(buf[offsets[1]+j]);
      if (j==1) {
        lcd.print("h");
      } else if (j==3) {
        lcd.print("m");
      } else if (j==5) {
        lcd.print("s UTC");
      }
    }

    // print date
    lcd.setCursor(0, 1);
    for (int j=0; j<6; j++) {
      lcd.print(buf[offsets[9]+j]);
      if (j==1 || j==3) {
        lcd.print(".");
      }
    }
    return 0;
  }
  return 1;
}

/**
 * Main loop
 */
void loop() {
  byteGPS=Serial.read();         // Read a byte of the serial port
  if (byteGPS == -1) {           // See if the port is empty yet
    delay(100);
  } else {
    if (!handle_byte(byteGPS)) {
      reset();
    }
  }
}

how to add this to Blynk ?

OK, now you are just pulling random GPS sketches out of your… computer…

What make/model MCU? What make/model GPS module?

Please don’t expect me to waste any more time guessing and prying info from you :unamused:

1 Like

I’m using UBLOX 7 GPS module but any others GPS module like NEMA worked on this code

Well both of “your” sketches are talking to your GPS at different baud rates, so I am going to guess that you haven’t actually gotten these to work yet.

When you do, determine the data format. If it is NMEA:

Then break it out into LAT and LON in decimal degrees (I believe that is the format the Map Widget is looking for):

Decimal Degrees (D.D°)
45.7811111° N 108.5038888° W

Then merge the required GPS commands into something like this sketch, configured for your hardware, to send that data to the Map widget.

http://docs.blynk.cc/#widgets-interface-map

https://examples.blynk.cc/?board=NodeMCU&shield=ESP8266%20WiFi&example=Widgets%2FMap

1 Like

@ErfanDL saw your post on GitHub asking Blynk for a “GPS module” example.

Do you have any Arduino skills?
Do you now understand how to convert Arduino sketches to Blynk sketches from the comments that have been provided to you previously?

You really need to acquire the Arduino and Blynk skill sets if you don’t already have them.

P.S. copying in @rshonk

1 Like

No That’s the reality I’m noob in coding :((

Please explain what a noob is as there are lots of different levels. When did you start, what have you done, what do you understand and what don’t you understand?

Since you asked for the YL-69 sketch back in March you should have had time to get to grips with converting Arduino sketches to Blynk sketches, although that might be a little difficult if you have no understanding of Arduino sketches.

You can’t simply stop by every few months and ask for Blynkers to build your project. It’s like me asking you to change the engine of my car or harvest 500 olive trees.

There are lots of sites that give you a basic understanding of Arduino sketches and you really need to learn the basics unless all you want to do in Blynk is flash an LED.

1 Like

You are right. But I do not know where to start Arduino programming. Sorry I took your times
:rose:

@Gunner
I found a code for particle photon device. Can you convert it to ESP8266 ?
This code does the same thing as I want
source: https://www.hackster.io/bigred/blynk-gps-tracker-396a96

//Include necessary libraries
#include <TinyGPS++.h>    //GPS library
#include <blynk.h>        //Access to Blynk app
char auth[] = "xxxxx";    //Blynk app authorization code
TinyGPSPlus gps;           //declare gps as a TinyGPS object
//declaration of variables
String msg5;
float spd;
float lat;
float lon;
float sats;
float hdop;
String bearing;
String slocation;
float startTime; 

void setup()
{
 Serial1.begin(4800);      //hardware connection to gps
 Blynk.begin(auth);
 startTime = millis();
}

void loop()
{
   Blynk.run();
    //send the location data to Particle cloud every 1 minute
   if (millis() > startTime + 60000) {    
       startTime = millis();
       Particle.publish("GPS1 Location", slocation);
   }
   // This sketch displays information every time a new sentence is correctly encoded.
   while (Serial1.available() > 0)
       if (gps.encode(Serial1.read())) {
           displayInfo();
           delay(2000);
       }
   if (millis() > 5000 && gps.charsProcessed() < 10) {
       msg5 = "No GPS detected";
       Blynk.virtualWrite(V5, msg5);
       while(true);
   }
}
void displayInfo() {
   if (gps.location.isValid()) {
       lat = gps.location.lat();    //get latitude and longitude
       lon = gps.location.lng();
       Blynk.virtualWrite(V0, 1, lat, lon, "car");    //write to Blynk map
       //create location string which can be used directly in Google maps 
       slocation = String(lat) + "," + String(lon);
       spd = gps.speed.kmph();    //get speed
       Blynk.virtualWrite(V1, spd);
       bearing = TinyGPSPlus::cardinal(gps.course.value()); //direction
       Blynk.virtualWrite(V2, bearing);
       sats = gps.satellites.value();    //get number of satellites
       Blynk.virtualWrite(V3, sats);
       hdop = gps.hdop.value() / 10;    //get accuracy of location
       Blynk.virtualWrite(V4, hdop);
       msg5 = "GPS Good";
       Blynk.virtualWrite(V5, msg5);
   }
   else
   {
       msg5 = "Invalid Data";
       Blynk.virtualWrite(V5, msg5);
   }
}
1 Like

@ErfanDL, regarding “I do not know where to start Arduino programming” …beleive it or not, but the best place to start learning arduino programing is at www.arduino.cc

…and if you are interested in a somewhat more advanced level, google the excelent free book “how to think like a computer scientist”. this is what i used to learn the coding basics and it helped me a lot!

2 Likes

Sorry, NO.

This is not a code factory nor are we on demand code monkeys. We are a group of Blynk users who may or may not be able to assist others in differing situations.

Please STOP simply asking for code or for someone else to do it for you. Your profile states Network Engineer… that generally implies some technical knowledge, so I am sure you can learn to code enough to setup your own sketches :wink:

1 Like

OK. Im working with some codes from blynk example. and add tinygps++ library to blynk example but it can’t show correct location I’m in IRAN but widget map show me in nigeria ! whats the wrong with code?

/*************************************************************
  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;

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[] = "mywifipass";
char server[] = "192.168.1.104";

WidgetMap myMap(V1);

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);

  // If you want to remove all points:
  //myMap.clear();

  int index = 1;
  float lat = (gps.location.lat(), 5);
  float lon = (gps.location.lng(), 4);
  myMap.location(index, lat, lon, "value");
}

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

Update: I disconnect my GPS module but blynk is keep show my location in nigeria !

@Gunner
@Costas

i’m not an expert with map widget, but i think, the problem is with above part.
what you put in the setup, it will execute only ONCE, at startup.
probably the first received gps coordinates were in nigeria. the gps units needs some time and wide sky surface visibility to compute first location on cold start. (if you have travelled long distance with unit turned off, or was used a long time ago, it can take 5-10 minutes to get a precise location)

to get the precise location, you should ask periodically the coordinates in main loop, say every second or so.

timer.setInterval(1000L, getCoordinates);  // put this line in the void setup function

BlynkTimer timer;  // put this after the line "TinyGPSPlus gps;"

timer.run(); // put this in void loop, after blynk run. (Initiates BlynkTimer)


void getCoordinates()
{
  myMap.clear();
  
  float lat = (gps.location.lat(), 5);
  float lon = (gps.location.lng(), 4);

  myMap.location(1, lat, lon, "value");
}

try these modifications, and give feedback, what happens.

2 Likes

thanks for reply. I modify code and again locating position in nigeria. My GPS module fix LED is on its mean gps is fixed.

/*************************************************************
  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[] = "miwifipass";
char server[] = "192.168.1.104";

WidgetMap myMap(V1);

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 getCoordinates()
{
  myMap.clear();
  
  float lat = (gps.location.lat(), 5);
  float lon = (gps.location.lng(), 4);

  myMap.location(1, lat, lon, "value");
}

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

of course you’ve got error, because you put the getcoordinates function in the setup function!

1 Like

I edit my code and fix error. check again above post

but it gives always the same coordinate in nigeria, or it is changing?

maybe you send the values in different units than what the map widget waits?

1 Like

yes its permanently show location in nigeria without changes.