#include <SoftwareSerial.h>
//#include <TinyGPS++.h>
// Define GSM and GPS pins
#define GSM_TX_PIN 4
#define GSM_RX_PIN 2
#define GSM_RST_PIN 0
// #define GPS_TX_PIN 16
// #define GPS_RX_PIN 17
// Define button pin
#define BUTTON_PIN 18
SoftwareSerial gsmSerial(GSM_TX_PIN, GSM_RX_PIN); // RX, TX for GSM
//SoftwareSerial gpsSerial(GPS_TX_PIN, GPS_RX_PIN); // RX, TX for GPS
//TinyGPSPlus gps;
void updateSerial() {
while (gsmSerial.available()) {
Serial.write(gsmSerial.read());
}
while (Serial.available()) {
gsmSerial.write(Serial.read());
}
}
void sendSMS(const String& message) {
gsmSerial.println("AT+CMGF=1"); // Set GSM module to text mode
delay(1000);
gsmSerial.println("AT+CMGS=\"+919820443686\""); // Replace with the recipient's phone number
delay(1000);
gsmSerial.print(message);
delay(100);
gsmSerial.write(26); // ASCII code for CTRL+Z to send the message
delay(1000);
}
void setup() {
Serial.begin(115200);
gsmSerial.begin(115200); // Initialize communication with SIM800L
//gpsSerial.begin(9600); // Initialize communication with GPS module
pinMode(BUTTON_PIN, INPUT_PULLUP); // Initialize button pin with internal pull-up resistor
Serial.println("Initializing");
gsmSerial.println("AT"); // Check if SIM800L is responding
updateSerial();
delay(1000);
Serial.println("Ready to send SMS when button is pressed.");
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
Serial.println("Button pressed!");
String message = "HELP! My current location is:\n";
// // Get GPS data
// while (gpsSerial.available() > 0) {
// gps.encode(gpsSerial.read());
// if (gps.location.isUpdated()) {
// message += "Latitude: " + String(gps.location.lat(), 6) + "\n";
// message += "Longitude: " + String(gps.location.lng(), 6) + "\n";
// break;
// }
// }
// // If no GPS data, indicate that location is unavailable
// if (!gps.location.isValid()) {
// message += "GPS location not available.";
// }
// Send SMS with help message and location
sendSMS(message);
Serial.println("SMS sent: " + message);
Serial.println("Message sucessfully sent to +91 9820443686 " );
// Debounce delay to prevent multiple triggers from a single press
delay(2000);
}
updateSerial(); // To keep the serial communication updated
}