LILYGO T-SIM7000G ESP32 Get GPS Data Latitude Longitude Altitude | Random Nerd Tutorials (2024)

In this quick guide, you’ll learn how to get GPS data with the LILYGO T-SIM7000G ESP32 board using Arduino IDE. This tutorial is also compatible with a “regular” ESP32 connected to a SIM7000G module.

LILYGO T-SIM7000G ESP32 Get GPS Data Latitude Longitude Altitude | Random Nerd Tutorials (1)

Compatibility

This board supports 2G, LTE CAT-M1, and NB-IoT protocols. You can go to the following links to check if any of these protocols are supported in your country:

Check network protocols supported in your country;

Check NB-IoT providers in your country.

Introducing the LILYGO T-SIM7000G ESP32

The LILYGO T-SIM7000G is an ESP32 development board with a SIM7000G chip. This adds GPS, GPRS, LTE CAT-M1, and NB-IoT protocols to your board. This means that with this board you can send SMS, get location and time using GPS, and connect it to the internet using a SIM card data plan. This board doesn’t support phone calls.

LILYGO T-SIM7000G ESP32 Get GPS Data Latitude Longitude Altitude | Random Nerd Tutorials (2)

Besides the SIM7000G module, the board also comes with some interesting features like a battery holder for a 18650 battery, a battery charging circuit where you can connect solar panels to recharge the battery, and a microSD card slot that can be useful for data logging projects or to save configuration settings.

For a more in-depth introduction, we recommend following the getting started guide:

  • Getting Started with LILYGO T-SIM7000G ESP32 (LTE, GPRS, and GPS)

Where to buy LILYGO T-SIM7000G ESP32?

Check the following link:

All stores in the previous link should sell the latest version, but double-check the product page, just in case the seller changes something.

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

LILYGO T-SIM7000G ESP32 Get GPS Data Latitude Longitude Altitude | Random Nerd Tutorials (3)

Libraries

The ESP32 communicates with the SIM7000G chip by sending AT commands via serial communication. You don’t need a library, you can simply establish a serial communication with the module and start sending AT commands. There’s a manual with all the SIM7000G AT commands:

However, it might be more practical to use a library. For example, the TinyGSM library knows which commands to send, and how to handle AT responses, and wraps that into the standard Arduino Client interface—that’s the library we’ll use in this tutorial.

Installing the TinyGSM Library

Open your Arduino IDE and go toSketch>Include Library>Manage Libraries. The Library Manager should open. Search forTinyGSM. Select the TinyGSM library by Volodymyr Shymanskyy.

LILYGO T-SIM7000G ESP32 Get GPS Data Latitude Longitude Altitude | Random Nerd Tutorials (4)

You also need to install the StreamDebugger library. Go toSketch>Include Library>Manage Libraries, search for StreamDebugger, and install it.

LILYGO T-SIM7000G ESP32 Get GPS Data Latitude Longitude Altitude | Random Nerd Tutorials (5)

Preparing the LILYGO T-SIM7000G ESP32 Board

To get GPS data with your board, you don’t need to connect a SIM card. You only need to connect the GPS antenna to the board.

LILYGO T-SIM7000G ESP32 Get GPS Data Latitude Longitude Altitude | Random Nerd Tutorials (6)

LILYGO T-SIM7000G ESP32 Board—Get GPS Data

Copy the following code to your Arduino IDE.

/* Rui Santos Complete project details at https://RandomNerdTutorials.com/lilygo-t-sim7000g-esp32-gps-data/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.*/#define TINY_GSM_MODEM_SIM7000#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb#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 Serial1TinyGsm modem(SerialAT);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); // 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);}void loop(){ // 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); 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); SerialMon.println("Disabling GPS"); modem.disableGPS(); // Set SIM7000G GPIO4 LOW ,turn off GPS power // CMD:AT+SGPIO=0,4,1,0 // Only in version 20200415 is there a function to control GPS power modem.sendAT("+SGPIO=0,4,1,0"); if (modem.waitResponse(10000L) != 1) { SerialMon.println(" SGPIO=0,4,1,0 false "); } delay(200); // Do nothing forevermore while (true) { modem.maintain(); }}

View raw code

How the Code Works

Let’s take a quick look at the relevant parts of the code.

First, you need to define the module you’re using. The library is compatible with many different modules. To use the SIM7000G, include the following line:

#define TINY_GSM_MODEM_SIM7000

Include the TinyGSM library.

#include <TinyGsmClient.h>

The following lines set the board pins to control the modem:

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

You need to create two Serial instances. One for the Serial Monitor which we’ll call SerialMon, and another to communicate with the modem via AT commands, which we call SerialAT.

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

Create a TinyGSM instance called modem on the SerialAT.

TinyGsm modem(SerialAT);

Initialize the Serial Monitor at a baud rate of 115200.

SerialMon.begin(115200);

Turn on the modem by setting the power pin to HIGH and LOW at a specific interval.

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

Initialize a Serial communication with the modem on the RX and TX pins we defined earlier.

SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);

Restart or init the modem:

// 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");}

Get some modem info using the getModemName() and getModemInfo() methods. These lines are optional and you don’t actually need them to get GPS data.

// Print modem infoString modemName = modem.getModemName();delay(500);SerialMon.println("Modem Name: " + modemName);String modemInfo = modem.getModemInfo();delay(500);SerialMon.println("Modem Info: " + modemInfo);

There are two versions of the LILYGO SIM7000G ESP32 board. The latest comes with active GPS antenna power control—when the module GPIO 4 is not turned on the antenna consumes only the static current of the LDO. This means we need to turn GPIO 4 on before getting GPS data to power the antenna. That’s what the next lines do:

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

You can start GPS using the enableGPS() method.

modem.enableGPS();

Then, we create variables where we’ll save the GPS data. We’ll get latitude, longitude, speed, altitude, visible satellites, used satellites, accuracy, and date and time.

delay(15000);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;

The following line gets GPS data using the getGPS() method and saves the values on the right variables.

if (modem.getGPS(&lat, &lon, &speed, &alt, &vsat, &usat, &accuracy, &year, &month, &day, &hour, &min, &sec))

Then, we simply print the values on the Serial Monitor. Now that you have the relevant information saved on variables, it’s easy to modify this project for your own needs. For example, a GPS tracker, GPS data logger, etc.

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

You can also get all raw data returned by the GPS using the getGPRSraw() method.

String gps_raw = modem.getGPSraw();SerialMon.println("GPS/GNSS Based Location String: " + gps_raw);

When you’re done using GPS, you can turn it off using the disableGPS() method:

modem.disableGPS();

And finally, turn off the power to the antenna by turning GPIO 4 off:

// Set SIM7000G GPIO4 LOW ,turn off GPS power// CMD:AT+SGPIO=0,4,1,0// Only in version 20200415 is there a function to control GPS powermodem.sendAT("+SGPIO=0,4,1,0");if (modem.waitResponse(10000L) != 1) { SerialMon.println(" SGPIO=0,4,1,0 false ");}

Demonstration

In your Arduino IDE, go to Tools > Boards and select the ESP32 Dev Module. Select the COM port in Tools > Port.

Then, upload the code to your board.

LILYGO T-SIM7000G ESP32 Get GPS Data Latitude Longitude Altitude | Random Nerd Tutorials (7)

Open the Serial Monitor at a baud rate of 115200 and press the on-board RST button to restart the board. Place your board outside or next to a window or door so that it can catch satellite signals.

It may take some time until it can get some GPS data, as you can see in the screenshot of my Serial Monitor.

LILYGO T-SIM7000G ESP32 Get GPS Data Latitude Longitude Altitude | Random Nerd Tutorials (8)

As you can see, it gets latitude, longitude, speed, altitude, visible satellites, number of used satellites to get position, accuracy, and UTC date and time. The longitude and latitude I got were very accurate. So, in my case, it was working pretty well to get the location.

It also outputs the complete GNSS navigation information parsed from NMEA sentences (that you can’t see above because the Serial Monitor window is too small). NMEA stands for National Marine Electronics Association, and in the world of GPS, it is a standard data format supported by GPS manufacturers. The output is as follows. The commas separate different values.

1,1,20220809173458.000,41.12XXXX,-8.52XXXX,140.200,0.00,237.6,1,,2.3,2.5,1.0,,20,5,1,,48,,

Here’s what each value means, in order:

  1. GNSS run status
  2. Fix status
  3. UTC date and time
  4. Latitude
  5. Longitude
  6. MSL altitude
  7. Speed over ground
  8. Course over ground
  9. Fix mode
  10. Reserver1
  11. HDOP
  12. PDOP
  13. VDOP
  14. Reserved2
  15. GNSS Satellites in View
  16. GPS Satellites used
  17. GLONASS Satellites used
  18. Reserver3
  19. C/N0 max
  20. HPA
  21. VPA

You can learn more about these parameters and possible values by checking the AT+CGNSINF AT command on the SIM7000G AT commands manual.

Wrapping Up

In this tutorial, you learned how to use the LILYGO T-SIM7000G ESP32 board to get GPS data. We showed you a simple example that prints the GPS data in the Serial Monitor. The idea is to modify the example and apply it to your own projects. It should also be compatible with a “regular” ESP32 board connected to a separate SIM7000G module.

The ESP32 T-SIM7000G board features will allow you to build a wide variety of projects taking into account that it can connect to the internet in remote locations using a SIM card data plan and send SMS. The fact that it can use a battery and solar panels for charging is also great, and the microSD card can also be handy for datalogging or saving configuration settings.

We hope you found this tutorial useful. Have you developed any projects with this board? Let us know in the comments section below.

You may also like the following tutorials (that with minor changes can be used with the SIM7000G board):

  • Connect ESP32 to Cloud MQTT Broker (TTGO T-Call ESP32 SIM800L)
  • ESP32 SIM800L: Send Text Messages (SMS Alert) with Sensor Readings
  • ESP32 Publish Data to Cloud without Wi-Fi (TTGO T-Call ESP32 SIM800L)

Learn more about the ESP32 with our resources:

  • Learn ESP32 with Arduino IDE
  • Build Web Servers with ESP32 and ESP8266
  • Firebase Web App with ESP32 and ESP8266
  • Free ESP32 Projects and Tutorials

Thanks for reading.

LILYGO T-SIM7000G ESP32 Get GPS Data Latitude Longitude Altitude | Random Nerd Tutorials (2024)

References

Top Articles
Latest Posts
Article information

Author: Gov. Deandrea McKenzie

Last Updated:

Views: 5975

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Gov. Deandrea McKenzie

Birthday: 2001-01-17

Address: Suite 769 2454 Marsha Coves, Debbieton, MS 95002

Phone: +813077629322

Job: Real-Estate Executive

Hobby: Archery, Metal detecting, Kitesurfing, Genealogy, Kitesurfing, Calligraphy, Roller skating

Introduction: My name is Gov. Deandrea McKenzie, I am a spotless, clean, glamorous, sparkling, adventurous, nice, brainy person who loves writing and wants to share my knowledge and understanding with you.