Sms +GPS tracking system

// Include libraries for SIM800L and Neo 6M modules
#include <SoftwareSerial.h>
#include <TinyGPS.h>

// Define pins for SIM800L module
#define SIM800L_TX 2
#define SIM800L_RX 3

// Define pins for Neo 6M module
#define NEO6M_TX 4
#define NEO6M_RX 5

// Define pin for input
#define INPUT_PIN 6

// Define phone number to send alert SMS to
#define PHONE_NUMBER "+94xxxxxxxxx"

// Define password for GETLOC command
#define PASSWORD "PASSWORD"

// Initialize software serial objects for SIM800L and Neo 6M modules
SoftwareSerial sim800l(SIM800L_TX, SIM800L_RX);
SoftwareSerial neo6m(NEO6M_TX, NEO6M_RX);

// Initialize TinyGPS object
TinyGPS gps;

// Variables to store GPS coordinates
float latitude, longitude;

void setup() {
// Initialize serial communication for debugging
Serial.begin(9600);

// Initialize software serial communication for SIM800L and Neo 6M modules
sim800l.begin(9600);
neo6m.begin(9600);

// Set input pin as input
pinMode(INPUT_PIN, INPUT);
}

void loop() {
// Check if input pin is high
if (digitalRead(INPUT_PIN) == HIGH) {
// Send alert SMS to specified phone number
sendSMS(PHONE_NUMBER, "Alert!");
}

// Check for incoming SMS
if (sim800l.available() > 0) {
// Read SMS
String sms = sim800l.readString();

// Check if SMS contains GETLOC command and password
if (sms.indexOf("GETLOC," + PASSWORD) != -1) {
  // Get GPS coordinates
  getGPS();
  
  // Send SMS with GPS coordinates to sender
  sendSMS(sms.substring(0, sms.indexOf(",")), "Latitude: " + String(latitude) + ", Longitude: " + String(longitude));
}

}
}

// Function to get GPS coordinates
void getGPS() {
// Clear GPS data
gps.f_get_position(&latitude, &longitude);

// Check if GPS data is valid
if (gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES) {
// Print error message
Serial.println("GPS data is invalid!");
}
}

// Function to send SMS
void sendSMS(String number, String message) {
// Set SIM800L module to text mode
sim800l.println("AT+CMGF=1");
delay(100);

// Set phone number
sim800l.println("AT+CMGS="" + number + """);
delay(100);

// Send message
sim800l.println(message);
delay(100);

// Send Ctrl+Z to end message
sim800l.println((char)26);
delay(100);

// Print message sent
Serial.println("Message sent to " + number + ": " + message);
}

Please correct your post and add code tags around your code.

There is a small pencil image below your existing posts.

  • click on this pencil ➜ that will let you edit your post.
  • Select the part of the text that corresponds to the code
  • Click on the <code/> icon in the toolbar to indicate that it is code
  • click image Save Edit

(Also make sure to properly indent the code in the IDE before copying and pasting it here. This can be done by pressing ctrlT on a PC or cmdT on a Mac)


Also do yourself a favour and please read How to get the best out of this forum and post accordingly.


Last but not least, do you have a question?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.