ESP32 GPS Tracker using SIM7600G-H 4G LTE with Google Maps
Table of Contents
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.
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.
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.
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
*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
#define TINY_GSM_MODEM_SIM7600 #define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb #include "Adafruit_FONA.h" #include <HardwareSerial.h> #include <TinyGsmClient.h> // 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.
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.
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.
It sent me the Google’s map URL link consisting of the Latitude and Longitude values.
Let’s open this URL link.
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.
I know this location, let’s go and find out if the car is at this location.
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.
Related article: Real Time GPS Tracker using SIM7600G 4G LTE, ESP32, and Adafruit Map
Watch Video Tutorial: