i want to build gps tracker when you sned an sms containing 'location' to the gsm module it sends back the latitude and longtitude this is the code :
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
SoftwareSerial sim(10, 11); // SIM800L RX, TX pins
SoftwareSerial ss(3, 4); // GPS NEO-7M RX, TX pins
TinyGPSPlus gps;
void setup() {
delay(7000); // Delay for 7 seconds to make sure the modules get the signal
Serial.begin(9600);
sim.begin(9600);
ss.begin(9600); // Start the software serial for GPS
// Set SMS to text mode and configure for automatic notification
sim.println("AT+CMGF=1"); // Set SMS to text mode
delay(1000);
sim.println("AT+CNMI=1,2,0,0,0"); // New SMS Message Indications
delay(1000);
Serial.println("System Initialized. Ready to receive SMS and respond with GPS location.");
}
void loop() {
// Automatically read new SMS messages as they arrive
if (sim.available() > 0) {
String smsText = sim.readString();
Serial.println("Received SMS:");
Serial.println(smsText); // Display received SMS message
// Check if the SMS contains the keyword 'location'
if (smsText.indexOf("location") != -1) {
sendGPSLocation(); // Respond with GPS location
}
}
// Process GPS data
if (ss.available() > 0) {
if (gps.encode(ss.read())) {
// Optionally, display GPS data on Serial for debugging
if (gps.location.isValid()) {
Serial.print("Valid GPS Data: Lat = ");
Serial.print(gps.location.lat(), 6);
Serial.print(", Lon = ");
Serial.println(gps.location.lng(), 6);
}
}
}
}
void sendGPSLocation() {
// Check if we have valid GPS data
if (gps.location.isValid()) {
String lat = String(gps.location.lat(), 6);
String lng = String(gps.location.lng(), 6);
String message = "Lat: " + lat + ", Long: " + lng;
Serial.println("Sending GPS Location: " + message);
sim.print("AT+CMGS=\"");
sim.print("+216xxxxxxxx"); // Replace with the sender's number or a predefined number
sim.println("\"");
delay(1000);
sim.println(message); // GPS coordinates
delay(100);
sim.write(26); // ASCII code of CTRL+Z to send the SMS
delay(1000);
} else {
Serial.println("GPS data not valid, cannot send location.");
}
}
d11---rxsim
d10---txsim
d4---rxgps
d3---txgps
gndsim---gndarduino---(-)battery