GPS module GT-U& not connecting need help with code

#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

pls format code as code

#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 3852411508
    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("3852411508");
    } 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 using an Arduino Uno for this project but i am not sure if i will have to change arduino in order to make these two modules work together

two softwareSerials will not work, if I not misstaken

I am using an GT-U7 gps module and a SIM7600G-H GSM shield module for this project

static const uint32_t GSMBaud = 115200;
and SS dont work with such big baud
maybe you try to stop one connection while other working?
and then start first and stop other...

i can get the system to send the sms to the number with the 115200 baud rate but the GPS does not connect so i am unable to get the gps location for the system.

is it work without GSM?

Yes i can get the gps to work alone at the 9600 baud rate

do you know Serial.end() command?

i do not i am really new to this all

start connection
and
gpsSerial.end();
will stop it, same with other connections. so you can start one, do their work and then stop, in order to do this to other module now.

so i have gotten it now to pull the GPS location however it ends the GSM before allowing it time to actually send the sms out

show, maybe i will understand what you mean.

void setup() {
  Serial.begin(9600);  // Start serial communication for debugging
  gpsSerial.begin(GPSBaud); // Set GPS 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

  // Attempt to collect GPS data
  collectGPSData();
}

void loop() {
  // Continuously read GPS data
  while (gpsSerial.available() > 0) {
    char c = gpsSerial.read();
    gps.encode(c);
  }

  // Check if GPS data has been collected
  if (!gpsDataCollected && gps.location.isValid()) {
    gpsDataCollected = true;
    Serial.println("GPS data collected.");
    lcd.setCursor(0, 1);
    lcd.print("GPS Data Collected");

    // End GPS serial communication
    gpsSerial.end();
  }

  // Check if the button is pressed to request GPS data display
  if (digitalRead(buttonPin) == LOW && gpsDataCollected) {  // Button pressed and GPS data collected
    // Initialize GSM communication
    gsmSerial.begin(GSMBaud);
    
    // Collect GPS data again before sending SMS
    String message;
    if (gps.location.isValid()) {
      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("3852411508");
    } 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);

    // End GSM communication and wait for response before restarting GPS
    gsmSerial.end();
    waitForGSMResponse();

    // Reset the GPS data collection status
    gpsDataCollected = false;

    // Restart GPS serial communication
    gpsSerial.begin(GPSBaud);
  }

  // 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() {
  Serial.println("Collecting GPS data...");
  unsigned long start = millis();
  while (millis() - start < 10000) { // Try for 10 seconds
    while (gpsSerial.available() > 0) {
      char c = gpsSerial.read();
      gps.encode(c);
      if (gps.location.isUpdated()) {
        gpsDataCollected = true;
        return;
      }
    }
  }
  Serial.println("GPS data collection timeout.");
}

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

what i am now struggling with is getting the AT commands in the right spot so they send before the gsmSerial.end happens

your sketch is not complete, so i took only necessary piece:

void loop() {
  collectGPSData();

  // Check if the button is pressed to request GPS data display
  if (digitalRead(buttonPin) == LOW && gpsDataCollected) {  // Button pressed and GPS data collected

    // Collect GPS data again before sending SMS
    String message;
    if (gps.location.isValid()) {
      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.print("Sent location to");
      lcd.setCursor(0, 1);
      lcd.print("3852411508");
    } else {
      sendSMS("3852411508", "GPS location not available.");
      Serial.println("Button pressed. GPS location not available.");
      lcd.clear();
      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);

    // End GSM communication and wait for response before restarting GPS
   // gsmSerial.end();
    waitForGSMResponse();

    // Reset the GPS data collection status
    gpsDataCollected = false;

    // Restart GPS serial communication
    //gpsSerial.begin(GPSBaud);
  }

  // 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() {
  gsmSerial.end();
  gpsSerial.begin(9600); // Set GPS module baud rate
  Serial.println("Collecting GPS data...");
  unsigned long start = millis();
  while (millis() - start < 10000) { // Try for 10 seconds
    while (gpsSerial.available() > 0) {
      char c = gpsSerial.read();
      gps.encode(c);
      if (gps.location.isUpdated()) {
        gpsDataCollected = true;
        return;
      }
    }
  }
  Serial.println("GPS data collection timeout.");
  gpsSerial.end();
  gsmSerial.begin(9600); 
}

Thank you for your help. I have it now working. Now it's onto the next thing for the project

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