Hardware serial issue please help

Good Evening,

I am doing a project for college which requires me to be able to retrieve a GPS location via SMS when requested by SMS.

So far I am able to retrieve the GPS data using NEOG6 connected through hardware serial to an Arduino Mega and are able to send this data using a gsm module SIM800L also connected through hardware serial using the following code:

// GSM
#define gsmPort Serial2 // GSM TX connected to Arduino RX2 pin 17,
                        // GSM RX connected to Arduino TX2 pin 16

// GPS
#include <NMEAGPS.h>
NMEAGPS gps;
gps_fix fix;
#define gpsPort Serial1 // GPS TX connected to Arduino RX1 pin 19

void setup()
{
  gsmPort.begin( 9600 );
  Serial .begin( 9600 );
  gpsPort.begin( 9600 );
}


void loop()
{
  // For testing, take commands from the Serial Monitor window
  if (Serial.available()) {
    switch(Serial.read()) {
      case 's':
        SendMessage();
        break;
      case 'r':
        RecieveMessage();
        break;
    }
  }

  // Process GPS characters and update the fix structure when
  //    new information arrives.
  if (gps.available( gpsPort )) {
    fix = gps.read();

    // print whatever you want (or nothing)
    Serial.print( F("Loc:") );
    if (fix.valid.location) {
      Serial.print( fix.longitude(), 6 );
      Serial.print( ',' );
      Serial.print( fix.latitude(), 6 );
    }
    Serial.println();
  }
}


void SendMessage()
{
  gsmPort.println( F("AT+CMGF=1") );    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1000 milli seconds or 1 second
  gsmPort.println( F("AT+CMGS=\"+447535668890\"\r") ); // Replace x with mobile number
  delay(1000);
  gsmPort.println( F("Location") );// The SMS text you want to send
  gsmPort.print( fix.latitude(), 5 );
  gsmPort.print( ',' );
  gsmPort.print( fix.longitude(), 5);
  delay(100);
  gsmPort.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
}

void RecieveMessage()
{
  Serial.println ("SIM800L Read an SMS");
  delay (1000);
  gsmPort.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
  delay(1000);
  gsmPort.println("AT+CMGL=4"); // AT Command to List SMS
  delay(1000);
  Serial.write ("Unread Message done");
}

At the moment my code requires me hit 's' in the serial monitor to send the gps data, I need to be able to get this data automatically by sending a SMS to the module and it replying with the gps data. Can anyone help me out?

Thank you!

We have you some suggestions over here.

What did you try? What don't you understand?