ESP32 GPS Tracker- IoT based Vehicle Tracking System

ESP32 GPS Tracker- IoT based Vehicle Tracking System

GPS stands for Global Positioning System, which is a worldwide radio-navigation system. To track the location of the device, the GPS tracking system uses the Global Navigation Satellite System (GNSS) Network. This network consists of a range of satellites that uses microwave signals to transmit the data which will be received by the GPS receiver module. 

Previously we used GPS with NodeMCU ESP8266 to build a Vehicle Tracking System and Accident alert system. In this project, we are going to build an IoT based GPS Vehicle Tracking System using ESP32 where we will display the latitude and longitude values on OLED Display as well as on Blynk App so that it can be monitored from anywhere in the world. 

 

Components Required

  • ESP32
  • GPS Module
  • OLED Display Module
  • Jumper Wires
  • Breadboard

 

OLED Display

OLED Display

The OLED displays are one of the most common and easily available displays for a microcontroller. This display can easily be interfaced with microcontroller using IIC or using SPI communication and has a good view angle and pixel density which makes it reliable for displaying small level graphics. It is compatible with any 3.3V-5V microcontroller, such as Arduino. The OLED display comes with a powerful single-chip CMOS OLED driver controller – SSD1306 that handles the entire RAM buffering. The SSD1306 driver has a built-in 1KB Graphic Display Data RAM (GDDRAM). We previously interfaced OLED with ESP32

Specifications:

  • OLED Driver IC: SSD1306
  • Resolution: 128 x 64
  • Visual Angle: >160°
  • Input Voltage: 3.3V ~ 6V
  • Pixel Colour: Blue
  • Working temperature: -30°C ~ 70°C

 

Neo 6M GPS Module

The NEO-6M module comes with a dimension of 16 x 12.2 x 2.4 mm package. It has 6 Ublox positioning engines offering unmatched performance. It is a good performance GPS receiver with a compact architecture, low power consumption, and reliable memory options. It is ideal for battery-operated mobile devices considering its architecture and power demands. The Time-to-First-Fix is less than 1 second and it enables it to find the satellites almost instantly. The output is in the format of NMEA standards, which can be decoded to find the coordinates and Time of the location.

Neo 6M GPS Module

  • Power supply: 2.8V to 5V
  • Interface: RS232 TTL
  • Built-in EEPROM and external antenna
  • Default baud rate: 9600 bps

We previously used GPS module Neo 6M with NodeMCU and displayed the location coordinates on a web-page, check all the GPS based IoT projects here.

 

Circuit Diagram

Circuit Diagram for ESP32 GPS NEO 6M is given below.

Circuit Diagram For ESP32 GPS NEO 6M

Here we are interfacing the ESP32 with GPS Module and OLED Display. Vcc and GND pin of GPS Module is connected to 3.3V and GND of ESP32 while the RX and TX pins are connected to TX2 and RX2 pins of ESP32. I2C mode is used to connect the OLED display Module (SSD1306) with ESP32. Connections between ESP32 and OLED Display are given as:

OLED Pin

ESP32 Pin

Vcc

3.3v

GND

GND

SCL

D22

SDA

D21

 

Configuring Blynk App for ESP32 GPS Tracker

Download the Blynk app from the Google Play Store and create a new account or Login into your existing account. 

Now click on ‘New Project’ to start a new project.

Blynk App

 

Now give a name for your project. Select ‘ESP32 Dev Board’ in the CHOOSE DEVICE option and ‘Wi-Fi’ in CONNECTION TYPE. Then click on ‘Create.’

After this, Blynk will send an Authorization to the registered Email id. Note down the Auth Token Code. It will be used in the Program. 

Configuring Blynk App for ESP32 GPS Tracker

 

Now in the next window, click on the “+” sign to add a widget. Inside the Widget box, select the ‘Map’ widget. 

Configuring Blynk App

 

After this, click on the MAP widget and select virtual pin ‘V0’ as INPUT.

ESP32 GPS Tracker using Blynk

With this final step, you are ready to use your app. By pressing the ‘Play’ button, you can switch your app from EDIT mode to PLAY mode where you can interact with the hardware. 

 

Code Explanation

The complete code for ESP32 GPS Tracking System is given at the end of the page. Here we are explaining some important parts of code.

In this program, we are going to use the Wire.hTinyGPS++.hSH1106.h and BlynkSimpleEsp32.h libraries. These libraries can be downloaded from below links:

 

So as usual, start the code by including all the required libraries. SH1106.h is especially created for ESP modules. 

#include <TinyGPS++.h>
#include <HardwareSerial.h>
#include <WiFi.h>
#include <Wire.h>               
#include<SH1106.h> 
#include <BlynkSimpleEsp32.h>

 

After that define the variables to store the Latitude and Longitude values.

float latitude, longitude;
String lat_str, lng_str;

 

In the next lines, enter your Wi-Fi name and password and also enter the Blynk Authorization key.

const char *ssid = "Wi-Fi Name"; // Enter your Wi-Fi Name
const char *pass = "Wi-Fi Password"; // Enter your Wi-Fi Password
char auth[] = "Blynk Key";

 

After that, create an instance for the OLED display that includes the Address and pins where the display is connected.

SH1106 display(0x3c, 21, 22);

 

Inside the setup() function, initialize the Serial Monitor at a baud rate of 115200 for debugging purposes and also initialize the OLED display, GPS module, and Blynk with the begin() method.

void setup()
{
  Serial.begin(115200);
  Serial.println(ssid);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED)
  {
  }
  Serial.println("WiFi connected");
  display.init();
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_10);
  SerialGPS.begin(9600, SERIAL_8N1, 16, 17);
  Blynk.begin(auth, ssid, pass);
  Blynk.virtualWrite(V0, "clr"); 
}

 

Inside the loop() function, check if there is incoming data from the GPS module. If the data is available, the code encodes the data and checks if the encoded data is valid or not, if the data is valid, then the further calculation is done to transform the NMEA data to the understandable data. 

while (SerialGPS.available() > 0) {
    if (gps.encode(SerialGPS.read()))
    {
      if (gps.location.isValid())
      {
        latitude = gps.location.lat();
        lat_str = String(latitude , 6);
        longitude = gps.location.lng();
        lng_str = String(longitude , 6);

 

After this, the GPS data is sent to the Blynk app and OLED display. Blynk app uses the Map widget to pinpoint the user's location using the latitude and longitude data.

        display.setTextAlignment(TEXT_ALIGN_LEFT);
        display.setFont(ArialMT_Plain_16);
        display.drawString(0, 23, "Lat:");
        display.drawString(45, 23, lat_str);
        display.drawString(0, 38, "Lng:");
        display.drawString(45, 38, lng_str);
        Blynk.virtualWrite(V0, 1, latitude, longitude, "Location");

 

Working of ESP32 GPS Tracking System

Once the hardware and the program are ready, upload the GPS tracking program into your ESP32 Board. Here Arduino IDE is used to upload the ESP32 GPS NEO 6M code to ESP32 board, so connect the ESP32 to your laptop with a Micro USB Cable and hit the upload button. Once the code is uploaded, the OLED will display the Latitude and Longitude values. It will also show the location in the Blynk app on Map like this:

ESP32 GPS Tracker

A working video and code for this IoT based ESP32 GPS Tracker are given below.

Code

#include <TinyGPS++.h>
#include <HardwareSerial.h>
#include <WiFi.h>
#include <Wire.h>               // Only needed for Arduino 1.6.5 and earlier
#include<SH1106.h> 
#include <BlynkSimpleEsp32.h>
float latitude , longitude;
String  lat_str , lng_str;
const char *ssid =  "Galaxy-M20";     // Enter your WiFi Name
const char *pass =  "ac312129"; // Enter your WiFi Password
char auth[] = "loPrSaL0eQFY9clcQ518R1SmYsRVC0eV"; 
WidgetMap myMap(V0); 
SH1106 display(0x3c, 21, 22);
WiFiClient client;
TinyGPSPlus gps;
HardwareSerial SerialGPS(1);
void setup()
{
  Serial.begin(115200);
  Serial.println("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");              // print ... till not connected
  }
  Serial.println("");
  Serial.println("WiFi connected");
  display.init();
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_10);
  SerialGPS.begin(9600, SERIAL_8N1, 16, 17);
  Blynk.begin(auth, ssid, pass);
  Blynk.virtualWrite(V0, "clr"); 
}
void loop()
{
  while (SerialGPS.available() > 0) {
    if (gps.encode(SerialGPS.read()))
    {
      if (gps.location.isValid())
      {
        latitude = gps.location.lat();
        lat_str = String(latitude , 6);
        longitude = gps.location.lng();
        lng_str = String(longitude , 6);
        Serial.print("Latitude = ");
        Serial.println(lat_str);
        Serial.print("Longitude = ");
        Serial.println(lng_str);
        display.clear();
        display.setTextAlignment(TEXT_ALIGN_LEFT);
        display.setFont(ArialMT_Plain_16);
        display.drawString(0, 23, "Lat:");
        display.drawString(45, 23, lat_str);
        display.drawString(0, 38, "Lng:");
        display.drawString(45, 38, lng_str);
        Blynk.virtualWrite(V0, 1, latitude, longitude, "Location");
        display.display();
      }
     delay(1000);
     Serial.println();  
    }
  }  
  Blynk.run();
}

Video

15 Comments

i'm beginner, somehow mine it's now working, esp32 red led on, neo 6m not blinking, the oled is not display anything, when i try to measure dc voltage with multimeter, from esp32 plugin to breadboard, then connected to oled, it shows 0,8 - 0,9 dc volt. is that correct?

in Blynk app, it shows New Device Online. when i turn off esp32, few second later, its shows Offline Location. it mean its working, but in the oled not show anything, and the GPS not blinking.

I was so excited to get this project working. Unfortunately, I cant get the code to compile. I've Googled for hours and cant get past the 'esp8266-oled-ssd1306-master\src/SH1106Wire.h:49:7: error: 'TwoWire' does not name a type
TwoWire* _wire = NULL;' and the 'blynk-library-master\src/BlynkSimpleEsp32.h:37:14: error: 'class WiFiClass' has no member named 'mode'
WiFi.mode(WIFI_STA);' errors

Shouldn't you add a SIM card, which allows the ESP32 to connect to WiFi? Otherwise, how can I possibly track my car if I'm leaving my car and try to connect 'from anywhere in the world'. The GPS receiver lets the ESP32 know its position. But without connection to the WiFi , this won't work.
I hope there is something that escapes my notice. But I am afraid, that KIMMSUSU's unanswered comment is actually pointing to the same issue. Maybe you could create another project, with some SIM card breakout board.

Exactly why I came to this project... I wonder how they are keeping the gps position updated when out of wifi, this could be interesting... only to find out that the project doesn't do this. It is effectively useless unless it is within range of wifi that it has access to. I can see that the last known location might be transmitted through the phone of the vehicle driver, if the phone was in hotspot mode and the esp32 was connected, but otherwise your gps position may be 100% accurate, but that doesn't mean you have access to that information. Cool project, but very disappointed that the implementation misses the mark.