#include <SoftwareSerial.
h>
#include <TinyGPS++.h>
#include <LiquidCrystal.h>
// LCD Pins
const int rs = 2, en = 4, d4 = 5, d5 = 6, d6 = 7, d7 = 8;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// GPS and GSM
SoftwareSerial gpsSerial(9, 10); // GPS: TX->9, RX->10
SoftwareSerial gsmm(11, 12); // GSM: TX->11, RX->12
TinyGPSPlus gps; // GPS instance
// Pin Configurations
const int buttonSMS = A0; // Button to send SMS
const int buttonPin = A5; // General button
const int ledPin = A4; // LED indicator
// State Variables
float latitude = 0.0, longitude = 0.0;
int buttonStateSMS = 0;
int buttonStateGeneral = 0;
int ledState = LOW;
int lastButtonState = LOW;
void setup() {
// Initialize pins
pinMode(buttonSMS, INPUT);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// Initialize serial communication
Serial.begin(9600);
gpsSerial.begin(9600);
gpsSerial.listen();
gsmm.begin(9600);
// LCD Initialization
initializeLCD();
}
void loop() {
// Check GPS for location updates
if (fetchGPSData()) {
displayCoordinates(); // Update LCD with GPS data
handleSMSButton(); // Handle SMS sending functionality
}
manageGeneralButton(); // Optional additional functionality
}
// Function to initialize the LCD with welcome message
void initializeLCD() {
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("WOMEN SAFETY");
delay(2000);
lcd.clear();
}
// Function to fetch GPS data until updated location is available
bool fetchGPSData() {
bool locationUpdated = false;
while (!locationUpdated) {
// Process incoming GPS data
while (gpsSerial.available() > 0) {
gps.encode(gpsSerial.read());
}
// Check if GPS location data is valid
if (gps.location.isUpdated()) {
latitude = gps.location.lat();
longitude = gps.location.lng();
locationUpdated = true;
} else {
delay(500); // Wait before retrying
}
}
return locationUpdated;
}
// Function to display latitude and longitude on LCD
void displayCoordinates() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LAT: ");
lcd.print(latitude, 6);
lcd.setCursor(0, 1);
lcd.print("LON: ");
lcd.print(longitude, 6);
}
// Function to handle SMS button input and send location via SMS
void handleSMSButton() {
buttonStateSMS = digitalRead(buttonSMS);
if (buttonStateSMS == LOW) {
sendSMS();
}
}
// Function to send SMS with GPS coordinates
void sendSMS() {
gsmm.print("\r");
delay(1000);
gsmm.print("AT+CMGF=1\r"); // Set GSM to SMS text mode
delay(1000);
gsmm.print("AT+CMGS=\"+919026361571\"\r"); // Recipient phone number
delay(1000);
gsmm.print("https://www.google.com/maps/place/");
gsmm.print(latitude, 6);
gsmm.print(",");
gsmm.print(longitude, 6);
delay(1000);
gsmm.write(0x1A); // End SMS with CTRL+Z
delay(3000); // Wait for message to send
}
// Function to manage a general button for additional features
void manageGeneralButton() {
buttonStateGeneral = digitalRead(buttonPin);
if (buttonStateGeneral == HIGH && lastButtonState == LOW) {
ledState = !ledState; // Toggle LED state
digitalWrite(ledPin, ledState); // Update LED state
delay(50); // Debounce delay
}
lastButtonState = buttonStateGeneral;
}