#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
static const int gpsTXPin = 4, gpsRXPin = 3; // Define GPS module pins
static const int gsmTXPin = 8, gsmRXPin = 7; // Define GSM module pins
static const uint32_t GPSBaud = 9600; // Baud rate for GPS
static const uint32_t GSMBaud = 115200; // Baud rate for GSM
static const int buttonPin = 2; // Define button pin
TinyGPSPlus gps;
SoftwareSerial gpsSerial(gpsRXPin, gpsTXPin); // RX, TX for GPS
SoftwareSerial gsmSerial(gsmRXPin, gsmTXPin); // RX, TX for GSM
// Initialize the LCD with I2C address 0x27 (20x4)
LiquidCrystal_I2C lcd(0x27, 20, 4);
unsigned long gpsTimer = 0;
const unsigned long gpsInterval = 120000; // 2 minutes
unsigned long displayTimer = 0;
const unsigned long refreshInterval = 60000; // 1 minute
bool displayRequested = false;
void setup() {
Serial.begin(9600); // Start serial communication for debugging
gpsSerial.begin(GPSBaud); // Set GPS module baud rate
gsmSerial.begin(GSMBaud); // Set GSM module baud rate
// Initialize LCD
lcd.init();
lcd.backlight(); // Turn on the backlight
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0, 0);
lcd.print("Initializing...");
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with internal pull-up
Serial.println("Initializing GSM module...");
// Give time for GSM module to initialize
delay(10000);
// Check communication with the GSM module
Serial.println("Testing GSM communication...");
delay(1000);
gsmSerial.println("AT");
delay(1000);
if (gsmSerial.available()) {
Serial.println("GSM module communication established.");
while (gsmSerial.available()) {
Serial.write(gsmSerial.read());
}
} else {
Serial.println("No response from GSM module.");
}
// Initialize GPS module
Serial.println("Connecting to GPS module...");
lcd.setCursor(0, 1);
lcd.print("Connecting to GPS");
// Try to get initial GPS fix
unsigned long start = millis();
bool gpsFixAcquired = false;
while (millis() - start < 10000) { // Try for 10 seconds
while (gpsSerial.available() > 0) {
char c = gpsSerial.read();
gps.encode(c);
if (gps.location.isUpdated()) {
gpsFixAcquired = true;
break;
}
}
if (gpsFixAcquired) {
break;
}
}
if (gpsFixAcquired) {
Serial.println("GPS fix acquired.");
lcd.setCursor(0, 2);
lcd.print("GPS fix acquired");
} else {
Serial.println("GPS fix not acquired.");
lcd.setCursor(0, 2);
lcd.print("GPS fix not acquired");
}
}
void loop() {
// Check if the button is pressed to request GPS data display
if (digitalRead(buttonPin) == LOW) { // Button pressed (LOW because of internal pull-up)
displayRequested = true;
// Collect GPS data before sending SMS
collectGPSData();
// Send SMS with GPS location to phone number 123456789
if (gps.location.isValid()) {
String message = "https://maps.google.com/maps?q=";
message += String(gps.location.lat(), 6);
message += ",";
message += String(gps.location.lng(), 6);
sendSMS("3852411508", message);
Serial.println("Button pressed. Sent location to 3852411508.");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sent location to");
lcd.setCursor(0, 1);
lcd.print("123456789");
} else {
sendSMS("3852411508", "GPS location not available.");
Serial.println("Button pressed. GPS location not available.");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("GPS location not");
lcd.setCursor(0, 1);
lcd.print("available.");
}
// Wait for the button to be released
while (digitalRead(buttonPin) == LOW) {
delay(50);
}
// Debounce delay
delay(50);
}
// Check if it's time to collect GPS data
if (millis() - gpsTimer > gpsInterval) {
collectGPSData();
}
// Read GPS data from SoftwareSerial
while (gpsSerial.available() > 0) {
char c = gpsSerial.read();
Serial.print(c); // Print raw GPS data for debugging
gps.encode(c);
}
// Check if it's time to refresh the display
if (millis() - displayTimer > refreshInterval) {
refreshDisplay();
}
// Check if display is requested
if (displayRequested) {
refreshDisplay();
displayRequested = false; // Reset display request after displaying once
}
}
void collectGPSData() {
// Reset the GPS timer
gpsTimer = millis();
}
void refreshDisplay() {
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0, 0);
lcd.print("Current Location:");
if (gps.location.isValid()) {
lcd.setCursor(0, 1);
lcd.print("Lat: ");
lcd.print(gps.location.lat(), 6);
lcd.setCursor(0, 2);
lcd.print("Lng: ");
lcd.print(gps.location.lng(), 6);
lcd.setCursor(0, 3);
lcd.print("Google Maps:");
lcd.setCursor(13, 3);
lcd.print("https://maps.google.com/maps?q=");
lcd.print(gps.location.lat(), 6);
lcd.print(",");
lcd.print(gps.location.lng(), 6);
} else {
lcd.setCursor(0, 1);
lcd.print("Location data");
lcd.setCursor(0, 2);
lcd.print("not available");
}
// Reset the display timer
displayTimer = millis();
}
void sendSMS(String phoneNumber, String message) {
// Set SMS text mode
gsmSerial.println("AT+CMGF=1");
delay(1000);
// Send SMS command
gsmSerial.print("AT+CMGS="");
gsmSerial.print(phoneNumber);
gsmSerial.println(""");
delay(1000);
// Send message content
gsmSerial.print(message);
delay(1000);
// End message with CTRL+Z
gsmSerial.write(26);
delay(1000);
// Print debug info
Serial.print("Sent SMS to ");
Serial.print(phoneNumber);
Serial.print(": ");
Serial.println(message);
}
I am trying to use the provided code to get a GPS location and then send it over sms to a desire number however when connecting the GPS module to the system it can not connect and find its location