How to receive gps location by sending sms to arduino?

It only can display gps location in serial monitor but when i send Location to arduino by sms it does not reply me gps location. How to solve this problem?

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

SoftwareSerial GPS(10, 11); // RX, TX (GPS)[hr]
SoftwareSerial SMS(2, 3); // RX, TX (SMS)
TinyGPS neo6m;

String _buffer;
int _timeout;
String txt;
String password;
String yourPassword = "Location";
void setup()
{
  Serial.begin(9600);
  _buffer.reserve(50);
  delay(100);
  SMS.begin(9600);
  delay(500);
  GPS.begin(9600);
   SMS.println("AT+CMGR=\"+601121534157\"\r");
  delay(500);
}

void loop()
{
  while (GPS.available())
  {
    if (neo6m.encode(GPS.read()))
    {
      Serial.println("----------------------------------------");

      float latitude, longitude;
      neo6m.f_get_position(&latitude, &longitude);
      float lat = latitude;
      float lon = longitude;

      float veloc;
      veloc = neo6m.f_speed_kmph();   //km/h

      String slat = String(lat);
      String slon = String(lon);
      String svel = String(veloc);
      String clat = " La ";
      String clon = " Lo ";
      String cvel = " Ve ";
      String txt = clat+slat+clon+slon+cvel+svel;
      Serial.println(txt);
   
  if (SMS.available()) // if a text has been recieved
  {
 
    SMS.println("AT+CNMI=2,2,0,0,0");
    password = ""; // flush the temporary variable

    char c;
    
    while(c=SMS.read())
    {
      password += c; // append the sms to the "password" variable
    }

    Serial.println(password); // print the contents of the sms
    Serial.println("\nEND OF MESSAGE"); // print to the computer

    SMS.flush(); // delete message from modem buffer
    Serial.println("MESSAGE DELETED"); // print to the computer

    if (password == yourPassword) // if the sms contains the correct password
    {
      Serial.println("\nPASSWORD VALID"); // print to the computer
      SMS.println("AT+CMGF=1");
      delay(1000);

      SMS.println("AT+CMGS=\"+xxxxxxxxxx\"\r");
      delay(1000);
      SMS.println(txt);
      delay(1000);

      SMS.println((char)26);
      delay(1000);
      SMS.println();

    }
    else Serial.println("\nPASSWORD NOT VALID"); // print to the computer
  }
 }
  }
}

How to solve this problem?

The reading of the SMS needs to be COMPLETELY independent of reading from the GPS.

You need to read from the GPS, and store the results in GLOBAL variables, NOT local variables, and NOT Strings.

You need, on every pass through loop(), see if there is an SMS requesting the location. If there is, AND there is stored data, send the stored data.

I redo it but still cannot receive sms.

#include <SoftwareSerial.h>
#include <TinyGPS++.h>
TinyGPSPlus gps;
 
SoftwareSerial ss(10, 11);
SoftwareSerial SIM(2, 3);
 
char text[180];
char latitude[12];
char longitude[12];
 
String phone = "+xxxxxxxxx";
char inchar;
boolean gpsUpdated = false;
boolean smsRequest = false;
 
void setup()
{
  Serial.begin(9600);
 
  SIM.begin(9600);
  delay(1000);
  ss.begin(9600);
  delay(1000);
}
 
void loop()
{
 
  gpsUpdate();
 
  readSMS();
 
  sendSMS();
 
}
 
void gpsUpdate()
{
  ss.listen();
  delay(500);  

     while (ss.available())
  
    (gps.encode(ss.read()));
    
    if (gps.location.isUpdated())
    {
      strcpy(text, "comgooglemaps://?q=");
        dtostrf(gps.location.lat(), 1, 6, latitude);
        strcat(text,latitude);
        strcat(text,",");
        dtostrf(gps.location.lng(), 1, 6, longitude);
        strcat(text,longitude);
        Serial.println(text);
        delay(100);
        gpsUpdated = true;
    }
}

void readSMS()
{
   SIM.listen();  
 
  if (SIM.available() >0) {
    delay(50);
   inchar = SIM.read();
   }
      if (inchar == 'Location')
       {
        smsRequest = true;
    
       }
     
     
}
 
void sendSMS()
{
  if ((gpsUpdated == true) && (smsRequest == true)) 
  {
  Serial.println("sending SMS");
  SIM.listen();  
  delay(300);
  SIM.print("AT+CMGF=1\r\n");                                                      
  delay(100);
  SIM.println("AT+CMGS=\""+phone+"\"\r\n");                                    
  delay(100);
  SIM.println(text);        
  delay(100);
  SIM.println((char)26);                      
  delay(100);
  SIM.println();
  delay(100);
  gpsUpdated = false;
  smsRequest = false;
  }
}

You can not use two instances of SoftwareSerial to listen at the same time. If you are listening to the GPS, you can't receive SMSs. if you are listening for SMSs, you can't get data from the GPS.

You need hardware with more HardwareSerial ports. A Mega or a Due.

What if you use separate libraries (SoftwareSerial and AltSoftSerial) to send information? For example, GSM reads to one library, while GPS reads to the other...

enstrum:
What if you use separate libraries (SoftwareSerial and AltSoftSerial) to send information? For example, GSM reads to one library, while GPS reads to the other...

I checked the AltSoftSerial web page, and the answer appears to be yes, but with limitations;

https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html

enstrum:
What if you use separate libraries (SoftwareSerial and AltSoftSerial) to send information? For example, GSM reads to one library, while GPS reads to the other...

You cannot use cannot use SoftwareSerial with any other software serial library (AltSoftSerial or NeoSWSerial). SoftwareSerial disables interrupts for the entire character time, both sending or receiving. This prevents other libraries from receiving or transmitting. Also, SoftwareSerial cannot transmit and receive simultaneously. Sometimes, you cannot use SoftwareSerial with HardwareSerial (depends on baud rates).

AltSoftSerial is the best software serial library, because it uses the Input Capture pin to record when received bits arrive (actually, the transitions from 0-1 or 1-0). This is more immune to the processor load (e.g., other interrupts).

The second best software serial library is my NeoSWSerial. It uses Pin Change Interrupts to receive characters, so it is more susceptible to the processor load. If other interrupts take too long, received bits will be lost. For transmitting characters, it also disables interrupts for most of the character time. For the OP, NeoSWSerial could be used for the GPS, since it will not be transmitting to the GPS device, just receiving.

The possible combinations for the OP are:

1) GPS on Serial and GSM on AltSoftSerial
2) GPS on AltSoftSerial and GSM on Serial
3) GPS on NeoSWSerial and GSM on Serial
4) GPS on NeoSWSerial and GSM on AltSoftSerial

Choice 1 would be completely reliable, and the sketch could still use Serial for debug prints.
Choice 2 would be completely reliable, but the sketch could not use Serial for debug prints.
Choice 3 would be completely reliable, but the sketch could not use Serial for debug prints.
Choice 4 would be fairly reliable, and the sketch could still use Serial for debug prints.

All of these choices require configuring/modifying the GSM library to use a different serial port.

Like PaulS said, buying a different Arduino can also make life easier:

1) Buy a Mega, Due or Teensy. Use Serial1 and Serial2. Serial can be used for debug prints.

2) Buy a Micro or Leonardo. Use Serial1 and AltSoftSerial. Serial can be used for debug prints.

There is a similar project here. That is a fairly simply sketch. User itmoto eventually developed it into a larger project, with sleep states to increase the battery life. I would suggest reading through those posts to see what kind of problems you are going to have, and How To Fix Them.