// Define the pins for the GPS and GSM modules
#define RX1 3 // GPS RX
#define TX1 1 // GPS TX
#define RX2 16 // GSM RX
#define TX2 17 // GSM TX
void setup() {
// Start serial communication
Serial.begin(115200); // Debugging via Serial (UART0)
Serial1.begin(9600, SERIAL_8N1, RX1, TX1); // GPS Serial (UART1)
Serial2.begin(9600, SERIAL_8N1, RX2, TX2); // GSM Serial (UART2)
// Initialize the modules
Serial.println("Initializing...");
}
void loop() {
// Inject fake NMEA data
String fakeNMEA = "$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47";
parseNMEA(fakeNMEA);
// Simulate a delay
delay(5000); // Wait 5 seconds before the next iteration
}
void parseNMEA(String nmea) {
// Example NMEA sentence: $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
// Split the string by commas
int index = 0;
String parts[15];
while (nmea.length() > 0) {
int commaIndex = nmea.indexOf(',');
if (commaIndex == -1) {
parts[index++] = nmea;
break;
} else {
parts[index++] = nmea.substring(0, commaIndex);
nmea = nmea.substring(commaIndex + 1);
}
}
if (index >= 6) {
// Parse latitude and longitude
String latStr = parts[2];
String latDir = parts[3];
String lngStr = parts[4];
String lngDir = parts[5];
float latitude = convertToDecimalDegrees(latStr, latDir);
float longitude = convertToDecimalDegrees(lngStr, lngDir);
Serial.print("Latitude: ");
Serial.println(latitude, 6);
Serial.print("Longitude: ");
Serial.println(longitude, 6);
sendData(latitude, longitude);
}
}
float convertToDecimalDegrees(String rawCoord, String direction) {
float degrees = rawCoord.substring(0, 2).toFloat();
float minutes = rawCoord.substring(2).toFloat();
float decimalDegrees = degrees + (minutes / 60.0);
if (direction == "S" || direction == "W") {
decimalDegrees *= -1;
}
return decimalDegrees;
}
void sendData(float lat, float lng) {
String lat_str = String(lat, 6);
String lng_str = String(lng, 6);
// Fake SIM800L setup using Serial2
Serial2.println("AT");
delay(1000);
Serial2.println("AT+CPIN?"); // Check SIM card presence
delay(1000);
Serial2.println("AT+CREG?"); // Check network registration
delay(1000);
Serial2.println("AT+CGATT?"); // Check GPRS status
delay(1000);
Serial2.println("AT+CIPSHUT"); // Reset IP session
delay(1000);
Serial2.println("AT+CIPMUX=0"); // Single connection mode
delay(1000);
Serial2.println("AT+CSTT=\"your_apn\",\"\",\"\""); // Setup APN
delay(1000);
Serial2.println("AT+CIICR"); // Bring up wireless connection
delay(3000);
Serial2.println("AT+CIFSR"); // Get IP address
delay(2000);
Serial2.println("AT+CIPSTART=\"TCP\",\"your.server.com\",\"80\""); // Start connection to the server
delay(5000);
// Prepare HTTP POST data
String httpRequest = "GET /track?lat=" + lat_str + "&lng=" + lng_str + " HTTP/1.1\r\nHost: your.server.com\r\n\r\n";
Serial2.print("AT+CIPSEND=");
Serial2.println(httpRequest.length());
delay(100);
Serial2.print(httpRequest);
delay(1000);
Serial2.println("AT+CIPCLOSE"); // Close the connection
delay(1000);
}