GSM 800L and GPS neo 6M connected to Arduino Uno

I have a GSM 800L and GPS Neo 6M all connected to the Arduino Uno

I want to create a code that if i send a sms text to the device example "Location" it will reply the coordinates

The problem is when im combining the receiving the incoming messages and GPS, the GPS will not work and i try to modify the code to prioritize the GPS to work thats when also the GSM 800L not work to receive a incoming message and reply

I have a problem combining that two functions

Code:

#include <SoftwareSerial.h>
#include <TinyGPS++.h>

// Define pins for SIM800L
SoftwareSerial gsmSerial(11, 10); // RX, TX
SoftwareSerial gpsSerial(3, 4);  // GPS Neo-6M: RX, TX

String incomingSMS; // Variable to store incoming SMS
// TinyGPS++ instance
TinyGPSPlus gps;

// Variables to store GPS coordinates
String latitude = "";
String longitude = "";
// Define button pin
const int buttonPin = 7; // Push button connected to D2
int buttonState = 0;     // Variable to store button state

void setup() {

    // Initialize button pin
  pinMode(buttonPin, INPUT_PULLUP); // Using internal pull-up resistor
  // Initialize serial communication for debugging
  Serial.begin(9600);
  
  Serial.println("Initializing...");

  // Initialize SIM800L communication 
  gpsSerial.begin(9600); // GPS module
  delay(1000);
  gsmSerial.begin(9600);
 
  // Give the module time to initialize
  delay(2000);

  // AT commands to configure the SIM800L
  if (sendCommand("AT", "OK")) {
    Serial.println("SIM800L is ready.");
  } else {
    Serial.println("SIM800L failed to respond. Check connections.");
  }

  sendCommand("AT+CMGF=1", "OK");        // Set SMS to text mode
  sendCommand("AT+CNMI=2,2,0,0,0", "OK"); // Configure to show SMS on serial when received

  Serial.println("Setup Complete. Waiting for SMS...");
}

void loop() {
  // Read the button state
  buttonState = digitalRead(buttonPin);
  // Continuously parse GPS data
  while (gpsSerial.available()) {
    char d = gpsSerial.read();
    gps.encode(d);
  }
  // Check for incoming messages from SIM800L
  if (gsmSerial.available()) {
    char c = gsmSerial.read();
    incomingSMS += c;

    if (c == '\n') { // End of the message
      Serial.println("Received SMS:");
      Serial.println(incomingSMS); // Debug: Show the full SMS content

      if (incomingSMS.indexOf("TEST") >= 0) { // Check for keyword "TEST"
        Serial.println("Keyword 'TEST' detected. Sending reply...");
        //sendReply("+639000000000", "Testing reply"); // Replace with your phone number
        if (gps.location.isUpdated()) {
      latitude = String(gps.location.lat(), 6);
      longitude = String(gps.location.lng(), 6);
      // Send the SMS with the location link
      sendSMS("+63900000000", "" + latitude + "N" +" "+ longitude+"E"); // Replace with your phone number
      sendSMS("+639000000000", "e copy paste sa google map"  ); // Replace with your phone number
    }
      else {
        Serial.println("No valid GPS data available.");
      }
      } else {
        Serial.println("No valid keyword found in SMS.");
      }

      incomingSMS = ""; // Clear the buffer for the next message
    }
  }
  // Check if the button is pressed
  if (buttonState == LOW) { // Button pressed (LOW because of pull-up)
    Serial.println("Button pressed, retrieving GPS data...");

    // Check if GPS data is updated
    if (gps.location.isUpdated()) {
      latitude = String(gps.location.lat(), 6);
      longitude = String(gps.location.lng(), 6);
      // Send the SMS with the location link
      sendSMS("+639102189769", "" + latitude + "N" +" "+ longitude+"E"); // Replace with your phone number
      sendSMS("+639102189769", "e copy paste sa google map"  ); // Replace with your phone number
    } else {
      Serial.println("No valid GPS data available.");
    }

    delay(5000); // Wait to avoid multiple triggers
  }
}
// Function to send SMS
void sendSMS(String phoneNumber, String message) {
  gsmSerial.println("AT+CMGF=1"); // Set SMS to text mode
  delay(1000);

  gsmSerial.print("AT+CMGS=\""); 
  gsmSerial.print(phoneNumber);  // Phone number
  gsmSerial.println("\"");
  delay(1000);

  // Send the message
  Serial.println("Sending SMS with content: " + message); // Debugging output
  gsmSerial.print(message);      // Message content
  delay(1000);

  gsmSerial.write(26); // ASCII code for CTRL+Z to send SMS
  delay(5000);

  Serial.println("SMS sent!");
}

void sendReply(String phoneNumber, String message) {
  Serial.println("Preparing to send reply...");
  sendCommand("AT+CMGF=1", "OK"); // Set SMS to text mode

  gsmSerial.print("AT+CMGS=\"");
  gsmSerial.print(phoneNumber);
  gsmSerial.println("\"");
  delay(100);

  gsmSerial.println(message);
  delay(100);

  gsmSerial.write(26); // ASCII code for CTRL+Z to send SMS
  delay(5000);

  Serial.println("Reply sent successfully.");
}

bool sendCommand(String command, String expectedResponse) {
  gsmSerial.println(command); // Send command to SIM800L
  delay(1000);

  String response = "";
  while (gsmSerial.available()) {
    response += (char)gsmSerial.read(); // Read the response from SIM800L
  }

  Serial.print("Command: ");
  Serial.println(command);
  Serial.print("Response: ");
  Serial.println(response);

  if (response.indexOf(expectedResponse) >= 0) {
    Serial.println("Expected response received.");
    return true;
  } else {
    Serial.println("Expected response not received.");
    return false;
  }
}

Serial Monitor
Received SMS:
TEST

Keyword 'TEST' detected. Sending reply...
No valid GPS data available.
Button pressed, retrieving GPS data...
No valid GPS data available.

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

SoftwareSerial gsmSerial(11, 10); // RX, TX
SoftwareSerial gpsSerial(3, 4); // GPS Neo-6M: RX, TX

Only one instance of SoftwareSerial can be used at a time. In theory you can switch between them but in practice this is difficult to the point of being impossible

Consider using an Arduino board with more than one hardware Serial port, such as a Mega

1 Like

Okay thanks i edited my post now

Any suggestions sir? What's the possible fixes?

As I said

There is no obvious solution using a Uno

Sir, also when i try without the receiving the incoming message and reply

Only the push button and it will send the coordinates in the registered number in the code it will work fine

Only have a problem the receiving the incoming message and reply

Remember what I said

You can use 2 instances of SoftwareSerial if you are absolutely certain that only one of them is receiving data at a time and don't want transmission and reception to occur at the same time. Can you guarantee that ?

Details can be found at https://docs.arduino.cc/learn/built-in-libraries/software-serial/

Hello sir thank you for your response i have a last question if i use ESP32 instead of Mega is that okay?

An ESP32 should allow you to use 2 hardware serial interfaces but be aware that the ESP32 runs at 3.3V rather than 5V so you may need to use voltage level shifters between external devices and the board