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