How to Use SIM7600 with GPS on ESP32

Protonest IoT
6 min readAug 17, 2024

--

If you’re working on an IoT project that requires GPS functionality, the SIM7600 module is a powerful choice. SIM7600 possesses 4G compatibility and also has GPS access compared to sim800, and sim900.

In this guide, we’ll walk you through the steps to get GPS data using the SIM7600 module on an ESP32, with a sample code that you can easily adapt to your project.

First, let’s make sure you have all the necessary components,

Setting Up the Hardware

Start by connecting your SIM7600 module to the ESP32. Here’s a simple wiring guide,

Circuit diagram

Ensure that your connections are secure, and double-check the wiring before proceeding.

Note: Connect the antenna with SMA to IPEX.

Setup image(sim7600 and antenna)

Initializing the SIM7600 Module

With the hardware ready, let’s move on to the code. The first step is to initialize the SIM7600 module, which involves setting up serial communication and sending a few AT commands to ensure everything is working correctly.

Here’s a snippet of how you can do this,

HardwareSerial sim7600(1); // Use the second serial port of ESP32

void setup() {
Serial.begin(115200);
sim7600.begin(115200, SERIAL_8N1, 16, 17); // Pins 16 (RX2) and 17 (TX2)

// Initialize SIM7600
sendData("AT", 1000, DEBUG);
sendData("AT+CRESET", 20000, DEBUG);
delay(5000);
sendData("AT+CGPS=1,1", 5000, DEBUG); // Enable GPS
}

void loop() {
}

String sendData(String command, const int timeout) {
String response = "";
sim7600.println(command);

long int time = millis();
while ((time + timeout) > millis()) {
while (sim7600.available()) {
char c = sim7600.read();
response += c;
}
}
Serial.println(response);
return response;
}

This part of the code initializes the SIM7600 and enables GPS functionality. The ‘sendData’ function helps in sending AT commands and receiving responses, which is crucial for communication with the SIM7600 module.

Retrieving GPS Data

Once the SIM7600 is initialized, the next step is to fetch GPS data. This data includes the latitude and longitude, which can be used in various applications like tracking or mapping.

Here’s how you can extract the GPS data,

void getGPSData() {
Serial.println("Fetching GPS data...");

String gpsData = sendData("AT+CGPSINFO", 2000, DEBUG);
delay(2000); // Wait for GPS data

Serial.print("Raw GPS Data: ");
Serial.println(gpsData); // Debugging raw GPS data

// Extract latitude and longitude from gpsData
int latIndexStart = gpsData.indexOf(":") + 2;
int latIndexEnd = gpsData.indexOf(",", latIndexStart);
String latitudeStr = gpsData.substring(latIndexStart, latIndexEnd);
char latDirection = gpsData.charAt(latIndexEnd + 1);

Serial.print("Extracted Latitude String: ");
Serial.println(latitudeStr);
Serial.print("Latitude Direction: ");
Serial.println(latDirection);

int lonIndexStart = gpsData.indexOf(",", latIndexEnd + 1) + 1;
int lonIndexEnd = gpsData.indexOf(",", lonIndexStart);
String longitudeStr = gpsData.substring(lonIndexStart, lonIndexEnd);
char lonDirection = gpsData.charAt(lonIndexEnd + 1);

Serial.print("Extracted Longitude String: ");
Serial.println(longitudeStr);
Serial.print("Longitude Direction: ");
Serial.println(lonDirection);

// Convert to decimal degrees
float latitude = convertToDecimalDegrees(latitudeStr, latDirection, true);
float longitude = convertToDecimalDegrees(longitudeStr, lonDirection, false);

Serial.print("Latitude: ");
Serial.println(latitude, 6);
Serial.print("Longitude: ");
Serial.println(longitude, 6);
}

float convertToDecimalDegrees(String coordinate, char direction, bool isLatitude) {
Serial.print("Converting Coordinate: ");
Serial.println(coordinate);

float degrees, minutes;
if (isLatitude) {
degrees = coordinate.substring(0, 2).toFloat(); // Extract first 2 digits for latitude degrees
minutes = coordinate.substring(2).toFloat(); // The rest are minutes
} else {
degrees = coordinate.substring(0, 3).toFloat(); // Extract first 3 digits for longitude degrees
minutes = coordinate.substring(3).toFloat(); // The rest are minutes
}

Serial.print("Degrees: ");
Serial.println(degrees);
Serial.print("Minutes: ");
Serial.println(minutes);

float decimalDegrees = degrees + (minutes / 60.0); // Convert minutes to decimal

// Adjust for direction
if (direction == 'S' || direction == 'W') {
decimalDegrees = -decimalDegrees;
}

Serial.print("Converted Decimal Degrees: ");
Serial.println(decimalDegrees, 6);

return decimalDegrees;
}

In this function, we use the ‘AT+CGPSINFO’ command to request GPS data from the SIM7600 module. The data is then parsed to extract the latitude and longitude, converting them into a more usable format (decimal degrees).

Final code

HardwareSerial sim7600(1); // Use the second serial port of ESP32
#define DEBUG true

void setup() {
Serial.begin(115200);
sim7600.begin(115200, SERIAL_8N1, 16, 17); // Pins 16 (RX2) and 17 (TX2)

// Initialize SIM7600
sendData("AT", 1000, DEBUG);
sendData("AT+CRESET", 20000, DEBUG);
delay(10000);
sendData("AT+CGPS=1,1", 5000, DEBUG); // Enable GPS

delay(5000); // Allow some time for the module to initialize

// Fetch and display GPS data
getGPSData();
}

void loop() {
// Continuously fetch GPS data
getGPSData();
delay(10000); // Fetch GPS data every 10 seconds
}

String sendData(String command, const int timeout, boolean debug) {
String response = "";
sim7600.println(command);

long int time = millis();
while ((time + timeout) > millis()) {
while (sim7600.available()) {
char c = sim7600.read();
response += c;
}
}
if (debug) {
Serial.print(response);
}
return response;
}

void getGPSData() {
Serial.println("Fetching GPS data...");

String gpsData = sendData("AT+CGPSINFO", 2000, DEBUG);
delay(2000); // Wait for GPS data

Serial.print("Raw GPS Data: ");
Serial.println(gpsData); // Debugging raw GPS data

// Extract latitude and longitude from gpsData
int latIndexStart = gpsData.indexOf(":") + 2;
int latIndexEnd = gpsData.indexOf(",", latIndexStart);
String latitudeStr = gpsData.substring(latIndexStart, latIndexEnd);
char latDirection = gpsData.charAt(latIndexEnd + 1);

Serial.print("Extracted Latitude String: ");
Serial.println(latitudeStr);
Serial.print("Latitude Direction: ");
Serial.println(latDirection);

int lonIndexStart = gpsData.indexOf(",", latIndexEnd + 1) + 1;
int lonIndexEnd = gpsData.indexOf(",", lonIndexStart);
String longitudeStr = gpsData.substring(lonIndexStart, lonIndexEnd);
char lonDirection = gpsData.charAt(lonIndexEnd + 1);

Serial.print("Extracted Longitude String: ");
Serial.println(longitudeStr);
Serial.print("Longitude Direction: ");
Serial.println(lonDirection);

// Convert to decimal degrees
float latitude = convertToDecimalDegrees(latitudeStr, latDirection, true);
float longitude = convertToDecimalDegrees(longitudeStr, lonDirection, false);

Serial.print("Latitude: ");
Serial.println(latitude, 6);
Serial.print("Longitude: ");
Serial.println(longitude, 6);
}

float convertToDecimalDegrees(String coordinate, char direction, bool isLatitude) {
Serial.print("Converting Coordinate: ");
Serial.println(coordinate);

float degrees, minutes;
if (isLatitude) {
degrees = coordinate.substring(0, 2).toFloat(); // Extract first 2 digits for latitude degrees
minutes = coordinate.substring(2).toFloat(); // The rest are minutes
} else {
degrees = coordinate.substring(0, 3).toFloat(); // Extract first 3 digits for longitude degrees
minutes = coordinate.substring(3).toFloat(); // The rest are minutes
}

Serial.print("Degrees: ");
Serial.println(degrees);
Serial.print("Minutes: ");
Serial.println(minutes);

float decimalDegrees = degrees + (minutes / 60.0); // Convert minutes to decimal

// Adjust for direction
if (direction == 'S' || direction == 'W') {
decimalDegrees = -decimalDegrees;
}

Serial.print("Converted Decimal Degrees: ");
Serial.println(decimalDegrees, 6);

return decimalDegrees;
}

Displaying GPS Data

Finally, you can display the GPS data on the Serial Monitor or send it to a server for further processing. This data can be extremely useful for a variety of applications, from tracking vehicles to monitoring mobile devices.

The serial monitor data will be as below,

Conclusion

We have come to the end of the article. This guide covered everything from setting up the hardware to extracting and displaying GPS data.

Now, it’s your turn to take this knowledge and build something awesome. Whether you’re tracking a car, creating a geolocation app, or anything in between, the possibilities are endless!

Hope you enjoyed the article. Please comment below or send us an email to info@protonest.co, if you face any issues when implementing.

Contact us for any consultations or projects related to IoT.

Email: info@protonest.co

Protonest for more details.

Protonest specializes in transforming IoT ideas into reality. We offer prototyping services from concept to completion. Our commitment ensures that your visionary IoT concepts become tangible, innovative, and advanced prototypes.

Our Website: https://www.protonest.co/

Cheers!

--

--

Protonest IoT
Protonest IoT

Written by Protonest IoT

We make your IoT ideas a reality

No responses yet