Real Time GPS Tracker using SIM7600G 4G LTE, ESP32, and Adafruit Map
Table of Contents
Real Time GPS Tracker:
Real Time GPS Tracker using SIM7600G 4G LTE, ESP32, and Adafruit Map- Before making the real time GPS tracker, I had thought about using the Blynk application, you can see I have purchased the Maker Plan.
However, even in this plan, the Map widget is not available. The Map widget is available in the Pro Plan. If you click on the “Compare All Plans” and then click on the ‘i button’, you will see that the Map is a Pro widget and it is only available in the PRO plan. And the PRO plan’s monthly subscription is 99 dollars per month, which is very expensive. No sensible person would ever purchase such an expensive plan just for GPS tracking unless he is a businessman.
If you provide services to people, then you can purchase this plan. Because with the Pro Plan, you can set up an entire organization. But for personal use, it’s way too expensive. So, I dropped the idea of using the Blynk application for real-time GPS tracking. Because we can do real time GPS tracking in Adafruit as well, and that too for free.
Amazon Links:
ESP32 WiFi + Bluetooth Module(Recommended)
*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
Real Time GPS Tracker Circuit Diagram:
I recommend you should read my previous article on the ESP32 Development board. In that article I have already explained everything. Anyway, here is a basic circuit diagram.
- Connect the VCC of the SIM7600G with 5V
- Connect the Ground of the SIM7600G with Ground of the ESP32
- Connect the TX pin of the SIM7600G with GPIO 4 pin of the ESP32
- Connect the RX pin of the SIM7600G with GPIO 2 pin of the ESP32
My hardware setup is completely ready. In the previous article and video, I explained in great detail; how to make this ESP32 WiFi + Bluetooth based development board for the SIM7600G 4G LTE Module. In that article, I practically demonstrated how to control a 220Vac bulb; each time I would send a command to turn ON or Turn OFF the Bulb, the ESP32 would also send me a feedback message obviously with the help of SIM7600G 4G LTE module.
I made this board with certain things in mind. Let me explain it one by one, and then you can decide for yourself whether you should make this board or not.
First, with this board, you can remove the SIM7600G if you want to use this board in a location where WiFi is already available. You can use it in your office or home to control different loads or to remotely monitor sensors. I will explain this in one of my upcoming videos. Today, we will use it as a real-time GPS tracker.
Second, the power supply on this board is quite powerful. It’s a 5V and 3A power supply. Using this power supply, we can easily power up all these onboard components including the SIM7600G GSM module. And not only that, we can also power up other sensors and breakout boards using the same power supply.
Third, you can see, on the left and right sides of the ESP32, I have added female headers, in case someone wants to monitor some sensors along with GPS tracking or wants to control their car’s doors or ignition.
Fourth, on this board, you can see that I have not used any DC socket; you can attach 12V and GND wires over here. The advantage of this is that you can easily use it in any waterproofed enclosure.
The power supply on this board can take up to 28 volts input, which means you can power it up using your car’s battery. I will practically demonstrate this when I track my car. Anyway, you can also power up this board with a solar panel.
Fifth, I have also added a relay on this board which you can use to completely disconnect your car or any vehicle’s computer system if someone tries to steal your car; to be more specific, you can use it as an anti-theft switch.
6th, on this board, I have also added terminals for the A02YYUW Waterproof Ultrasonic Sensor. So, you can use the Ultrasonic sensor for car parking and also as an accident avoidance system. When a car comes too close, you can turn on an indicator or a buzzer. I have added these female headers so that you can add sensors and output devices according to your preference. This is basically a development board that you can use to test thousands of projects.
How do you like this board? Let me know in a comment. I have to make many projects using this board; so consider subscribing if you don’t want to miss any of my upcoming articles and videos.
If you don’t want to make this board, you can use a ready-made IoT development board, which is based on the ESP32 and SIM7600G-H 4G LTE module.
I have already used it as a GPS tracker in my previous project. In that project, I used to request the GPS location through SMS. The controller would send me Google’s map URL.
I was amazed by its GPS tracking and accuracy. It pinpointed my car’s location accurately, and then I used my cell phone’s GPS to track and find my car’s location in just a few minutes. Its GPS antenna is so powerful that it can connect to the GPS satellites within 2 minutes even when it’s inside a room.
Today, we will test the GPS antenna of the SIM7600G 4G LTE module in the same way. We will see if it can connect to the GPS satellites from inside a room.
And this time, instead of requesting the GPS location through SMS, I will track the car location in realtime and then I will go and find it. But, before doing all this, first, let’s set up the Adafruit map.
Adafruit Map setup:
While you are logged in into your registered account on the io.adafruit.com and while the IO tab is selected; click on the Feeds and then click on the New Feed.
Write the name and then click on the Create button.
Goto the Dashboards and click on New Dashboard.
Write the Dashboard name.
Open the dashboard.
Click on Dashboard Settings and Create New Block.
Search for the MAP block and add it.
Select the Feed Name and click on the Next Step button.
Write the Block Title.
Select the Light Mode Map Type as per your needs, in my case I selected Satellite Imagery and select the Dark Mode Map Type. Finally, click on the Create Block button.
Again go to the Dashboard settings and this time click on the Edit Layout…resize the Map… Click on the Save Layout.
Then copy the adafruit io credentials
Paste the credentials in the code.
Real Time GPS Tracker Program:
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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
#define TINY_GSM_MODEM_SIM7600 // Set serial for debug console (to the Serial Monitor, default speed 115200) #define SerialMon Serial #define rxPin 4 //connect the RX pin of the sim7600 with pin 2 and TX with pin 4 #define txPin 2 HardwareSerial SerialAT(1); // Define the serial console for debug prints, if needed #define TINY_GSM_DEBUG SerialMon // Define how you're planning to connect to the internet // These defines are only for this example; they are not needed in other code. #define TINY_GSM_USE_GPRS true #define TINY_GSM_USE_WIFI false // Your GPRS credentials, if any const char apn[] = "ufone.pinternet"; // Change this to your Provider details const char gprsUser[] = ""; const char gprsPass[] = ""; // MQTT details const char *broker = "io.adafruit.com"; const char *GPSTopic = "electroclinic/feeds/gpsloc/csv"; #include <TinyGsmClient.h> #include <PubSubClient.h> // Just in case someone defined the wrong thing.. #if TINY_GSM_USE_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS #undef TINY_GSM_USE_GPRS #undef TINY_GSM_USE_WIFI #define TINY_GSM_USE_GPRS false #define TINY_GSM_USE_WIFI true #endif #if TINY_GSM_USE_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI #undef TINY_GSM_USE_GPRS #undef TINY_GSM_USE_WIFI #define TINY_GSM_USE_GPRS true #define TINY_GSM_USE_WIFI false #endif #ifdef DUMP_AT_COMMANDS #include <StreamDebugger.h> StreamDebugger debugger(SerialAT, SerialMon); TinyGsm modem(debugger); #else TinyGsm modem(SerialAT); #endif TinyGsmClient client(modem); PubSubClient mqtt(client); #define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds #define TIME_TO_SLEEP 5 // Time ESP32 will go to sleep (in seconds) #define UART_BAUD 9600 #define PIN_DTR 25 #define PIN_TX 4 #define PIN_RX 2 #define PWR_PIN 16 #define BAT_ADC 35 #define LED_PIN 12 float speed_kph = 0; float heading = 0; float speed_mph = 0; float altitude = 0; float lat = 0; float lon = 0; char Lat[20]; char Lon[20]; char sendbuffer[120]; const char* Bat_value; RTC_DATA_ATTR int bootCount = 0; int ledStatus = LOW; uint32_t lastReconnectAttempt = 0; void transCoordinates() { while (lat <= 0 || lon <= 0) { modem.sendAT("+SGPIO=0,4,1,1"); if (modem.waitResponse(10000L) != 1) { Serial.println(" SGPIO=0,4,1,1 false "); } modem.enableGPS(); Serial.println("Requesting current GPS/GNSS/GLONASS location"); if (modem.getGPS(&lat, &lon)) { Serial.println("Latitude: " + String(lat, 8) + "\tLongitude: " + String(lon, 8)); } } char *p = sendbuffer; // add speed value dtostrf(speed_mph, 2, 6, p); p += strlen(p); p[0] = ','; p++; // concat latitude dtostrf(lat, 2, 6, p); p += strlen(p); p[0] = ','; p++; // concat longitude dtostrf(lon, 3, 6, p); p += strlen(p); p[0] = ','; p++; // concat altitude dtostrf(altitude, 2, 6, p); p += strlen(p); // null terminate p[0] = 0; Serial.print("Sending: "); Serial.println(sendbuffer); // (Speed,Latitude,Longitude,Altitude) mqtt.publish(GPSTopic, sendbuffer); } void mqttCallback(char *topic, byte *payload, unsigned int len) { SerialMon.print("Message arrived ["); SerialMon.print(topic); SerialMon.print("]: "); SerialMon.write(payload, len); SerialMon.println(); } boolean mqttConnect() { SerialMon.print("Connecting to "); SerialMon.print(broker); // Connect to MQTT Broker //boolean status = mqtt.connect("GsmClientTest"); // Or, if you want to authenticate MQTT: boolean status = mqtt.connect("GsmClientName", "electroclinic", "aio_fUmK170nMGdEhsE9NEmh2eyMdunX"); if (status == false) { SerialMon.println(" fail"); return false; } SerialMon.println(" success"); //mqtt.publish(topicInit, "GsmClientTest started"); return mqtt.connected(); } void setup() { // Set console baud rate Serial.begin(115200); delay(10); // Set LED OFF pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, HIGH); pinMode(PWR_PIN, OUTPUT); digitalWrite(PWR_PIN, HIGH); delay(300); digitalWrite(PWR_PIN, LOW); Serial.println("\nWait..."); delay(1000); SerialAT.begin(115200,SERIAL_8N1,rxPin, txPin, false); if (bootCount == 0) { // Restart takes quite some time // To skip it, call init() instead of restart() Serial.println("Initializing modem..."); if (!modem.restart()) { Serial.println("Failed to restart modem, attempting to continue without restarting"); } bootCount++; } String name = modem.getModemName(); DBG("Modem Name:", name); String modemInfo = modem.getModemInfo(); DBG("Modem Info:", modemInfo); #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE // The XBee must run the gprsConnect function BEFORE waiting for network! modem.gprsConnect(apn, gprsUser, gprsPass); #endif SerialMon.print("Waiting for network..."); if (!modem.waitForNetwork()) { SerialMon.println(" fail"); delay(10000); return; } SerialMon.println(" success"); if (modem.isNetworkConnected()) { SerialMon.println("Network connected"); } #if TINY_GSM_USE_GPRS // GPRS connection parameters are usually set after network registration SerialMon.print(F("Connecting to ")); SerialMon.print(apn); if (!modem.gprsConnect(apn, gprsUser, gprsPass)) { SerialMon.println(" fail"); delay(10000); return; } SerialMon.println(" success"); if (modem.isGprsConnected()) { SerialMon.println("GPRS connected"); } #endif // MQTT Broker setup mqtt.setServer(broker, 1883); mqtt.setCallback(mqttCallback); } void loop() { // Make sure we're still registered on the network if (!modem.isNetworkConnected()) { SerialMon.println("Network disconnected"); if (!modem.waitForNetwork(180000L, true)) { SerialMon.println(" fail"); delay(10000); return; } if (modem.isNetworkConnected()) { SerialMon.println("Network re-connected"); } #if TINY_GSM_USE_GPRS // and make sure GPRS/EPS is still connected if (!modem.isGprsConnected()) { SerialMon.println("GPRS disconnected!"); SerialMon.print(F("Connecting to ")); SerialMon.print(apn); if (!modem.gprsConnect(apn, gprsUser, gprsPass)) { SerialMon.println(" fail"); delay(10000); return; } if (modem.isGprsConnected()) { SerialMon.println("GPRS reconnected"); } } #endif } if (!mqtt.connected()) { SerialMon.println("=== MQTT NOT CONNECTED ==="); // Reconnect every 10 seconds uint32_t t = millis(); if (t - lastReconnectAttempt > 10000L) { lastReconnectAttempt = t; if (mqttConnect()) { lastReconnectAttempt = 0; } } delay(100); return; } mqtt.loop(); transCoordinates(); esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); esp_deep_sleep_start(); } |
Finally, you can upload this program. In my case, I have already uploaded this program so let’s go ahead and start the practical demonstration.
Note: You will also need to install the ESP32 in the Arduino IDE. I highly recommend you should read my getting started article on the ESP32 and how to install it in the Arduino IDE.
Practical Demonstration:
Our hardware setup is already complete. As you can see, I have inserted the SIM card, the GPS Antenna is connected, and the Full band LTE antenna is also connected. And as you know, I am using the ESP32 WiFi + Bluetooth Module. So, let’s go ahead and power up this GPS tracking system and see if the GPS antenna will connect to the GPS satellites from inside this room. Let’s go ahead and connect it to the laptop.
The Green LED started to blink, which means the SIM7600G GSM module is now connected to the Network, and now we will see how much time the GPS antenna will take to connect to the GPS satellites. For this, I am going to open the Serial monitor on my laptop.
It’s been two minutes and still, I can’t see the Longitude and latitude values. Let’s wait for another 5 to 8 minutes.
It took almost 5 to 6 minutes to connect to the GPS Satellites and now you can see the Latitude and Longitude values. Last time when I used the SIM7600G-H 4G LTE module; it connected to the GPS satellites within 2 minutes whereas the SIM7600G module took around 6 minutes which still I think is great. Because I have tested other GPS Modules which even take more than 30 minutes and some of them would not even connect to the satellites.
Anyway, the ESP32 sends the Latitude and Longitude values to the Adafruit map and then enters into the sleep mode and then again wakes up and sends the Latitude and longitude values to the Adafruit Map. You can change the sleep duration in the programming. Right now, the sleep duration is 5 seconds.
This is the Adafruit Map and you can see we have successfully tracked the GPS location. This is the exact location where I am using this GPS Antenna. Now with the help of this Map, we can track the location of this GPS tracker in real-time.
I am using the Car’s battery to power up this tracker. It’s already connected to the network and GPS satellites as I can see the current location of my car. Now I will stand here and watch where my brother takes the car.
This is also creating a track which makes it quite easy to track and locate the car; because in real-time we can see which routes the car is taking.
So as you can see, the Car has stopped. And my brother is now waiting at this location just to check if I can find him or not. So let’s go.
There’s my Car. Using this GPS tracker, we can track anything in real-time. With this Real Time GPS Tracker you can track a stolen car.
Next, we will use the SIM7600G 4G LTE module with the Blynk application for monitoring a sensor and for controlling some loads. So, consider subscribing if you don’t want to miss that video. So, that’s all for now.
Watch Video Tutorial: