ESP32

ESP32 GPS Tracker using SIM7600G-H 4G LTE with Google Maps

ESP32 GPS Tracker:

ESP32 GPS Tracker using SIM7600G-H 4G LTE with Google Maps- Today, we are going to use the Lilygo IoT development board as a GPS Tracker. On this development board the ESP32 WiFi + Bluetooth module, the SIM7600G-H 4G LTE module, and GPS antenna are already wired up for you, so you don’t need to do any wiring.

ESP32 GPS Tracker

Not only this, it also has a holder for the Lithium Ion battery which makes this IoT development board perfect for GPS tracking applications and other IoT based projects.




I have already published a getting started article on the ESP32 SIM7600G-H 4G LTE; where I explained its technical specifications, onboard components, and lots of other things.

ESP32 GPS Tracker

In that article, I also practically demonstrated how to control some LEDs and generate feedback messages just to confirm whether that particular load is Turned ON or Turned OFF.

And I also explained, how to monitor a sensor by sending a request command through a text message. So, if you are just getting started with this IoT development board then I highly recommend you should read my previous article. Because, today I won’t explain the technical specifications.

ESP32 GPS Tracker

This is going to be the simplest ESP32 GPS tracking System and it can be build by anyone throughout the world. This is only possible because of the SIM7600G-H 4G LTE module which works with all the GSM networks throughout the world. The SIM7600G-H is also backward compatible with 3G and 2G networks.

The SIM7600G-H module has proven its capability to send and receive text messages flawlessly, and this is a statement I can confidently make based on my personal experience. However, I have yet to explore its GPS connectivity, which raises several intriguing questions.

is it powerful enough that it can connect to a GPS satellite while it’s inside a room or we will have to take it outside like most of the other GPS modules including the Neo 6M GPS module.

How accurate is this GPS antenna? Is the accuracy is in feet or meters? If it doesn’t pinpoint the exact location then what’s the use of a GPS tracker? So, this is what we are going to find out in this video.

But, first let’s get it fixed on this Acrylic sheet, I am doing it because, I don’t want the GPS antenna to short with other components.



Amazon Links:

Lilygo SIM7600G-h Official Page

Lilygo SIM7600G-h on Amazon

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

ESP32 GPS Tracker

It has been securely mounted on the acrylic sheet, and now I can use and move it around without worrying about the GPS antenna making any kind of contact with the onboard components. Now, let’s go ahead and take a look at the programming.



ESP32 GPS Tracker Programming:

#define TINY_GSM_MODEM_SIM7600

#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
#include "Adafruit_FONA.h"
#include 
#include 

// LilyGO T-SIM7000G Pinout
#define UART_BAUD   115200
#define PIN_DTR     25
#define PIN_TX      27
#define PIN_RX      26
#define PWR_PIN     4

#define LED_PIN     12

// Set serial for debug console (to Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands
#define SerialAT  Serial1

TinyGsm modem(SerialAT);
const int FONA_RST = 34;
const int RELAY_PIN = 13;
char replybuffer[255];
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
String smsString = "";
char fonaNotificationBuffer[64];          //for notifications from the FONA
char smsBuffer[250];

HardwareSerial *fonaSerial = &SerialAT;

Adafruit_FONA_3G fona = Adafruit_FONA_3G(FONA_RST);

unsigned long timeout;
char charArray[20];
unsigned char data_buffer[4] = {0};
String mylong = ""; // for storing the longittude value
String mylati = ""; // for storing the latitude value
String textForSMS;
char buff[10];
#define SMS_TARGET  "+923339218213"
void setup(){
  SerialMon.begin(115200);
  SerialMon.println("Place your board outside to catch satelite signal");

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

  //Turn on the modem
  pinMode(PWR_PIN, OUTPUT);
  digitalWrite(PWR_PIN, HIGH);
  delay(300);
  digitalWrite(PWR_PIN, LOW);

  delay(1000);
  
  // Set module baud rate and UART pins
  SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);
 fonaSerial->begin(UART_BAUD,SERIAL_8N1,PIN_RX, PIN_TX, false);
  // Restart takes quite some time
  // To skip it, call init() instead of restart()
  SerialMon.println("Initializing modem...");
  if (!modem.restart()) {
    Serial.println("Failed to restart modem, attempting to continue without restarting");
  }
  
  // Print modem info
  String modemName = modem.getModemName();
  delay(500);
  SerialMon.println("Modem Name: " + modemName);

  String modemInfo = modem.getModemInfo();
  delay(500);
  SerialMon.println("Modem Info: " + modemInfo);

 // Set SIM7000G GPIO4 HIGH ,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");
  if (modem.waitResponse(10000L) != 1) {
    SerialMon.println(" SGPIO=0,4,1,1 false ");
  }

  modem.enableGPS();
  
  delay(15000);

  
   if (! fona.begin(*fonaSerial))
  {
    Serial.println(F("Couldn't find FONA"));
    while(1);
  }
  Serial.println(F("FONA is OK"));
  
  fonaSerial->print("AT+CNMI=2,1\r\n");  //set up the FONA to send a +CMTI notification when an SMS is received
  Serial.println("FONA Ready");
 
}

void loop(){


  
char* bufPtr = fonaNotificationBuffer;    //handy buffer pointer
  
  if (fona.available())      //any data available from the FONA?
  {
    int slot = 0;            //this will be the slot number of the SMS
    int charCount = 0;
    //Read the notification into fonaInBuffer
    do
    {
      *bufPtr = fona.read();
      Serial.write(*bufPtr);
      delay(1);
    } while ((*bufPtr++ != '\n') && (fona.available()) && (++charCount < (sizeof(fonaNotificationBuffer)-1)));

    //Add a terminal NULL to the notification string
    *bufPtr = 0;

    //Scan the notification string for an SMS received notification.
    //  If it's an SMS message, we'll get the slot number in 'slot'
    if (1 == sscanf(fonaNotificationBuffer, "+CMTI: " FONA_PREF_SMS_STORAGE ",%d", &slot)) 
    {
      Serial.print("slot: "); Serial.println(slot);

      char callerIDbuffer[32];  //we'll store the SMS sender number in here

      // Retrieve SMS sender address/phone number.
      if (! fona.getSMSSender(slot, callerIDbuffer, 31)) 
      {
        Serial.println("Didn't find SMS message in slot!");
      }
      Serial.print(F("FROM: ")); Serial.println(callerIDbuffer);

      // Retrieve SMS value.
      uint16_t smslen;
      if (fona.readSMS(slot, smsBuffer, 250, &smslen)) // pass in buffer and max len!
      { 
        smsString = String(smsBuffer);
        Serial.println(smsString);
      }

      if (smsString == "location")  
      {
        gpslocation();
//        modem.sendSMS(SMS_TARGET, textForSMS);
//        //fona.sendSMS(callerIDbuffer,textForSMS );
//        Serial.println("SMS send");
//     textForSMS="";
      }
        if (fona.deleteSMS(slot)) {
        Serial.println(F("OK!"));
      } else {
        Serial.print(F("Couldn't delete SMS in slot ")); 
        Serial.println(slot);
        fona.print(F("AT+CMGD=?\r\n"));
      }
    }  
  } 
}
void gpslocation(){
 
  float lat      = 0;
  float lon      = 0;
  float speed    = 0;
  float alt     = 0;
  int   vsat     = 0;
  int   usat     = 0;
  float accuracy = 0;
  int   year     = 0;
  int   month    = 0;
  int   day      = 0;
  int   hour     = 0;
  int   min      = 0;
  int   sec      = 0;
  
  for (int8_t i = 15; i; i--) {
    SerialMon.println("Requesting current GPS/GNSS/GLONASS location");
    if (modem.getGPS(&lat, &lon, &speed, &alt, &vsat, &usat, &accuracy,
                     &year, &month, &day, &hour, &min, &sec)) {
      SerialMon.println("Latitude: " + String(lat, 8) + "\tLongitude: " + String(lon, 8));
      SerialMon.println("Speed: " + String(speed) + "\tAltitude: " + String(alt));
      SerialMon.println("Visible Satellites: " + String(vsat) + "\tUsed Satellites: " + String(usat));
      SerialMon.println("Accuracy: " + String(accuracy));
      SerialMon.println("Year: " + String(year) + "\tMonth: " + String(month) + "\tDay: " + String(day));
      SerialMon.println("Hour: " + String(hour) + "\tMinute: " + String(min) + "\tSecond: " + String(sec));
      break;
    } 
    else {
      SerialMon.println("Couldn't get GPS/GNSS/GLONASS location, retrying in 15s.");
      delay(15000L);
    }
  }
  SerialMon.println("Retrieving GPS/GNSS/GLONASS location again as a string");
  String gps_raw = modem.getGPSraw();
  SerialMon.println("GPS/GNSS Based Location String: " + gps_raw);

  mylati = dtostrf(lat, 3, 6, buff);
  mylong = dtostrf(lon, 3, 6, buff);
 textForSMS = textForSMS + "http://www.google.com/maps/place/" + mylati + "," + mylong ; 
delay(5000);
modem.sendSMS(SMS_TARGET, textForSMS);
        //fona.sendSMS(callerIDbuffer,textForSMS );
        Serial.println("SMS send");
     textForSMS="";
  }



ESP32 GPS Tracker Code Explanation:

Almost 80% of this code is from the getting started video. This time round, I also added the TinyGsmClient library.

To add this library in the Arduino IDE, simply copy the library name, then go to the Sketch Menu, then to Include Library, and click on the Manage libraries. Paste the Library name in the search box.

ESP32 GPS Tracker

As you can see I have already installed this library.

I defined variables for storing the Longitude and Latitude values.

I also defined a cell number. The Longitude and Latitude values along with the Google map url link will be sent to this number.

In the setup() function, I activated the GPS.

Inside the loop() function, we simply check the received text message. If the received text message has the command “location” then the ESP32 with the help of SIM7600G-H sends a Google’s map URL link consisting of the Latitude and Longitude values. I have already uploaded this program and now let’s start the testing.




ESP32 GPS Tracker Testing:

Let’s Turn on the Switch and see how much time it’s going to take to connect to the GPS Satellites while its inside this room.

ESP32 GPS Tracker

It took less than 2 minutes to connect to the GPS Satellites. This is not a standard time, it may vary depending on your location. It may connect within a few seconds or it may take even 5 to 10 minutes. But, in my case it took less than 2 minutes which is great. Because, I have used a bunch of GPS modules which used to take around 10 to 15 minutes.

Let’s send the “location” command to request the GPS location.

ESP32 GPS Tracker

It sent me the Google’s map URL link consisting of the Latitude and Longitude values.

ESP32 GPS Tracker



Let’s open this URL link.

ESP32 GPS Tracker

Look at the Accuracy, this is just mind blowing, its pointing to the same exact location with around 4 to 6 feet accuracy, this is acceptable. I am really impressed, how easy it is to build such an accurate and highly reliable GPS tracking system without even connecting a single wire. You don’t have to do anything excepting uploading the program.

Next, my brother is going to take this ESP32 GPS Tracker in a Car to some random location and then I am going to track the Car location. If it worked, then you can make a vehicle tracking system, or in fact you can track almost anything you want.

Its been 30 minutes since my brother has gone. Let’s send the command and see if we can find the Car location.

ESP32 GPS Tracker

I know this location, let’s go and find out if the car is at this location.

ESP32 GPS Tracker

I just tracked my Car location within a few minutes with such a high level of accuracy. So, If you guys also want to make such an amazing GPS Tracker then you should definitely try the Lilygo Sim7600G-H IoT development board.

In the next article, we will make a real-time GPS Vehicle tracking system using the New Blynk V2.0. So, consider subscribing if you don’t want to miss that article. So, that’s all for now.



Watch Video Tutorial:

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button