GPS module + ethernet module enc28j60

hey I have project and i have to connect ethernet module and gps to arduino mega
each code works perfectly when i run it alone
but when i merge the 2 codes i get nothing from the gps at serial monitor
so how can i merge both of these codes?
GPS module code

/*
 Example code for connecting a Parallax GPS module to the Arduino
 Igor Gonzalez Martin. 05-04-2007
 igor.gonzalez.martin@gmail.com
 English translation by djmatic 19-05-2007
 Listen for the $GPRMC string and extract the GPS location data from this.
 Display the result in the Arduino's serial monitor.
 */ 
 #include <string.h>
 #include <ctype.h>
 #include <SoftwareSerial.h>
 SoftwareSerial GPS(11,20);
 int ledPin = 13;                  // LED test pin
 //int rxPin = 15;                    // RX PIN 
 //int txPin = 14;                    // TX TX
 int byteGPS=-1;
 char linea[300] = "";
 char comandoGPR[7] = "$GPRMC";
 int cont=0;
 int bien=0;
 int conta=0;
 int indices[13];
 void setup() {
   pinMode(ledPin, OUTPUT);       // Initialize LED pin
   //pinMode(rxPin, INPUT);
   //pinMode(txPin, OUTPUT);
   Serial.begin(9600);
   GPS.begin(9600);
   for (int i=0;i<300;i++){       // Initialize a buffer for received data
     linea[i]=' ';
   }   
 }
 void loop() {
   digitalWrite(ledPin, HIGH);
   byteGPS=GPS.read();         // Read a byte of the serial port
   if (byteGPS == -1) {           // See if the port is empty yet
     delay(100); 
   } else {
     // note: there is a potential buffer overflow here!
     linea[conta]=byteGPS;        // If there is serial port data, it is put in the buffer
     conta++;                      
    // Serial.print(byteGPS, BYTE); 
     if (byteGPS==13){            // If the received byte is = to 13, end of transmission
       // note: the actual end of transmission is <CR><LF> (i.e. 0x13 0x10)
       digitalWrite(ledPin, LOW); 
       cont=0;
       bien=0;
       // The following for loop starts at 1, because this code is clowny and the first byte is the <LF> (0x10) from the previous transmission.
       for (int i=1;i<7;i++){     // Verifies if the received command starts with $GPR
         if (linea[i]==comandoGPR[i-1]){
           bien++;
         }
       }
       if(bien==6){               // If yes, continue and process the data
         for (int i=0;i<300;i++){
           if (linea[i]==','){    // check for the position of the  "," separator
             // note: again, there is a potential buffer overflow here!
             indices[cont]=i;
             cont++;
           }
           if (linea[i]=='*'){    // ... and the "*"
             indices[12]=i;
             cont++;
           }
         }
         Serial.println("");      // ... and write to the serial port
         Serial.println("");
         Serial.println("---------------");
         for (int i=0;i<12;i++){
           switch(i){
             case 0 :Serial.print("Time in UTC (HhMmSs): ");break;
             case 1 :Serial.print("Status (A=OK,V=KO): ");break;
             case 2 :Serial.print("Latitude: ");break;
             case 3 :Serial.print("Direction (N/S): ");break;
             case 4 :Serial.print("Longitude: ");break;
             case 5 :Serial.print("Direction (E/W): ");break;
             case 6 :Serial.print("Velocity in knots: ");break;
             case 7 :Serial.print("Heading in degrees: ");break;
             case 8 :Serial.print("Date UTC (DdMmAa): ");break;
             case 9 :Serial.print("Magnetic degrees: ");break;
             case 10 :Serial.print("(E/W): ");break;
             case 11 :Serial.print("Mode: ");break;
             case 12 :Serial.print("Checksum: ");break;
           }
           for (int j=indices[i];j<(indices[i+1]-1);j++){
             Serial.print(linea[j+1]); 
           }
           Serial.println("");
         }
         Serial.println("---------------");
       }
       conta=0;                    // Reset the buffer
       for (int i=0;i<300;i++){    //  
         linea[i]=' ';             
       }                 
     }
   }
 }

Ethernet module code

#include <EtherCard.h>

static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
static byte myip[] = {192, 168, 1, 177};
byte Ethernet::buffer[500];
int LineHorz = 0;
int Linevirt = 0;
BufferFiller bfill;
static word HeadPage() {
  bfill.emit_p(PSTR(
                 "HTTP/1.0 200 OK\r\n"
                 "Content-Type: text/html\r\n"
                 "Pragma: no-cache\r\n"
                 "\r\n"
               ));  // appear on the page
}

static word StreamPage() {
  bfill = ether.tcpOffset();
  HeadPage();
  bfill.emit_p(PSTR(
                 //"<meta http-equiv='refresh' content='1'/>" //or refresh the page
                 "<title>StreamPage</title>" // title of home page
                 "<h1>Led</h1>"
                 "<form method=get>" // check box to let led on or off
                 "<p><input type=checkbox name=MotorMode value=Forward>Forward 
"
                 "<input type=checkbox name=MotorMode value=Backward>Backward 
"
                 "<input type=checkbox name=MotorMode value=Left>Left 
"
                 "<input type=checkbox name=MotorMode value=Right>Right 
"
                 //"<input type=checkbox name=MotorMode value=Off>Off </p>"
                 "<h1>Horizontal = $D 
"
                 "Virtical \t = $D</h1>"
                 "<input type=submit value=Submit>"
                 "</form>"
               ), LineHorz, Linevirt); // appear on the page
  return bfill.position();
}

void setup() {
  Serial.begin(9600);
  Serial.println("[Test]");
  if ( ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)
    Serial.println( "Faild to access Ethernet controller");
  else
    Serial.println( "Ethernet controller Accessed !!!! ");
  ether.staticSetup(myip);
  /////////////////////////////////////////////////
}

void loop() {
  // put your main code here, to run repeatedly:



  word pos = ether.packetLoop( ether.packetReceive() );

  if (pos) { // check if valid tcp data is received
    // data received from ethernet
    ///////////////////////////////////////////////////////////////////////////

    if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Forward") != 0) {
      Serial.println("Received ON command");

    }
    else if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Backward") != 0) {
      //Serial.println("Received OFF command");

    }
    else if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Left") != 0) {
      //Serial.println("Received OFF command");

    }
    else if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Right") != 0) {
      //Serial.println("Received OFF command");

    }
    else if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Stop") != 0) {
      //Serial.println("Received OFF command");
 
    }
    ////////////////////////////////////////////////////////////////////////////
    word n;
    n = StreamPage() ;
    ether.httpServerReply(n);
    delay(500);
  }
}

Forget that wasteful GPS code. Use a GPS library instead, like my NeoGPS. It's smaller, faster and more accurate than other libraries, and it uses much less than 300 bytes of RAM. o_O

And there's no reason to use SoftwareSerial on a Mega. Use one of the extra serial ports, like Serial1 (pins 18 & 19).

Just add a few lines to the ethernet sketch:

#include <NMEAGPS.h>
NMEAGPS gps;            // parses GPS characters
gps_fix fix;            // structure with parsed fields, like lat/lon and dateTime
#define gpsPort Serial1 // Connect GPS TX to Arduino pin 19

#include <EtherCard.h>

static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
static byte myip[] = {192, 168, 1, 177};
byte Ethernet::buffer[500];
int LineHorz = 0;
int Linevirt = 0;
BufferFiller bfill;
static word HeadPage() {
  bfill.emit_p(PSTR(
                 "HTTP/1.0 200 OK\r\n"
                 "Content-Type: text/html\r\n"
                 "Pragma: no-cache\r\n"
                 "\r\n"
               ));  // appear on the page
}

static word StreamPage() {
  bfill = ether.tcpOffset();
  HeadPage();
  bfill.emit_p(PSTR(
                 //"<meta http-equiv='refresh' content='1'/>" //or refresh the page
                 "<title>StreamPage</title>" // title of home page
                 "<h1>Led</h1>"
                 "<form method=get>" // check box to let led on or off
                 "<p><input type=checkbox name=MotorMode value=Forward>Forward 
"
                 "<input type=checkbox name=MotorMode value=Backward>Backward 
"
                 "<input type=checkbox name=MotorMode value=Left>Left 
"
                 "<input type=checkbox name=MotorMode value=Right>Right 
"
                 //"<input type=checkbox name=MotorMode value=Off>Off </p>"
                 "<h1>Horizontal = $D 
"
                 "Virtical \t = $D</h1>"
                 "<input type=submit value=Submit>"
                 "</form>"
               ), LineHorz, Linevirt); // appear on the page
  return bfill.position();
}

void setup() {
  Serial.begin(9600);
  Serial.println("[Test]");
  gpsPort.begin(9600);
  if ( ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)
    Serial.println( "Faild to access Ethernet controller");
  else
    Serial.println( "Ethernet controller Accessed !!!! ");
  ether.staticSetup(myip);
  /////////////////////////////////////////////////
}

void loop() {
  // put your main code here, to run repeatedly:

  if (gps.available( gpsPort )) {
    fix = gps.read();
    
    // What to do with GPS data when it's ready?
    Serial.println( fix.dateTime.seconds ); // print the current second

    // Or, you can use 'fix' anywhere in the sketch, like in StreamPage().
  }

  word pos = ether.packetLoop( ether.packetReceive() );

  if (pos) { // check if valid tcp data is received
    // data received from ethernet
    ///////////////////////////////////////////////////////////////////////////

    if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Forward") != 0) {
      Serial.println("Received ON command");

    }
    else if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Backward") != 0) {
      //Serial.println("Received OFF command");

    }
    else if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Left") != 0) {
      //Serial.println("Received OFF command");

    }
    else if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Right") != 0) {
      //Serial.println("Received OFF command");

    }
    else if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Stop") != 0) {
      //Serial.println("Received OFF command");
 
    }
    ////////////////////////////////////////////////////////////////////////////
    word n;
    n = StreamPage() ;
    ether.httpServerReply(n);
    //delay(500);
  }
}

And don't use delay. That will make it lose GPS characters.

If you want to try it, NeoGPS is available from the Arduino IDE Library Manager, under the menu Sketch -> Include Library -> Manage Libraries. Even if you don't try it, be sure to read the Troubleshooting page for other tips for GPS sketches.

-dev:
Forget that wasteful GPS code. Use a GPS library instead, like my NeoGPS. It's smaller, faster and more accurate than other libraries, and it uses much less than 300 bytes of RAM. o_O

And there's no reason to use SoftwareSerial on a Mega. Use one of the extra serial ports, like Serial1 (pins 18 & 19).

Just add a few lines to the ethernet sketch:

#include <NMEAGPS.h>

NMEAGPS gps;            // parses GPS characters
gps_fix fix;            // structure with parsed fields, like lat/lon and dateTime
#define gpsPort Serial1 // Connect GPS TX to Arduino pin 19

#include <EtherCard.h>

static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
static byte myip[] = {192, 168, 1, 177};
byte Ethernet::buffer[500];
int LineHorz = 0;
int Linevirt = 0;
BufferFiller bfill;
static word HeadPage() {
  bfill.emit_p(PSTR(
                "HTTP/1.0 200 OK\r\n"
                "Content-Type: text/html\r\n"
                "Pragma: no-cache\r\n"
                "\r\n"
              ));  // appear on the page
}

static word StreamPage() {
  bfill = ether.tcpOffset();
  HeadPage();
  bfill.emit_p(PSTR(
                //"" //or refresh the page
                "StreamPage" // title of home page
                "

Led

"
                "" // check box to let led on or off
                "

Forward
"
                "Backward
"
                "Left
"
                "Right
"
                //"Off

"
                "

Horizontal = $D
"
                "Virtical \t = $D

"
                ""
                ""
              ), LineHorz, Linevirt); // appear on the page
  return bfill.position();
}

void setup() {
  Serial.begin(9600);
  Serial.println("[Test]");
  gpsPort.begin(9600);
  if ( ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)
    Serial.println( "Faild to access Ethernet controller");
  else
    Serial.println( "Ethernet controller Accessed !!!! ");
  ether.staticSetup(myip);
  /////////////////////////////////////////////////
}

void loop() {
  // put your main code here, to run repeatedly:

if (gps.available( gpsPort )) {
    fix = gps.read();
   
    // What to do with GPS data when it's ready?
    Serial.println( fix.dateTime.seconds ); // print the current second

// Or, you can use 'fix' anywhere in the sketch, like in StreamPage().
  }

word pos = ether.packetLoop( ether.packetReceive() );

if (pos) { // check if valid tcp data is received
    // data received from ethernet
    ///////////////////////////////////////////////////////////////////////////

if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Forward") != 0) {
      Serial.println("Received ON command");

}
    else if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Backward") != 0) {
      //Serial.println("Received OFF command");

}
    else if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Left") != 0) {
      //Serial.println("Received OFF command");

}
    else if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Right") != 0) {
      //Serial.println("Received OFF command");

}
    else if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Stop") != 0) {
      //Serial.println("Received OFF command");

}
    ////////////////////////////////////////////////////////////////////////////
    word n;
    n = StreamPage() ;
    ether.httpServerReply(n);
    //delay(500);
  }
}



And don't use `delay`. That will make it lose GPS characters.

If you want to try it, NeoGPS is available from the Arduino IDE Library Manager, under the menu **Sketch -> Include Library -> Manage Libraries**. Even if you don't try it, be sure to read the Troubleshooting page for other tips for GPS sketches.

thanks -dev
your code worked perfectly and it's much faster than the old one
I just had to add few code lines to get the data i needed

    if (fix.valid.location) {
      Serial.print( F("latitude: ") );
      Serial.println( fix.latitude(), 6 );
      Serial.print( F("longitude: ") );
      Serial.println( fix.longitude(), 6 );
    }
    if (fix.valid.altitude)
      Serial.print( F("Altitude: ") );
      Serial.println( fix.altitude() );

thanks so much man I appreciate your help .

this is the full code for reference

#include <NMEAGPS.h>
NMEAGPS gps;            // parses GPS characters
gps_fix fix;            // structure with parsed fields, like lat/lon and dateTime
#define gpsPort Serial1 // Connect GPS TX to Arduino pin 19 (RX)

#include <EtherCard.h>

int latitudeInt    = 0 ;
int latitudeFloat1 = 0 ;
int latitudeFloat2 = 0 ;

int longitudeInt   = 0 ;
int longitudeFloat1 = 0 ;
int longitudeFloat2 = 0 ;

/*int altitudeInt    = 0 ;
int altitudeFloat1 = 0 ;
int altitudeFloat2 = 0 ;*/

static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
static byte myip[] = {192, 168, 1, 177};
byte Ethernet::buffer[500];

BufferFiller bfill;
static word HeadPage() {
  bfill.emit_p(PSTR(
                 "HTTP/1.0 200 OK\r\n"
                 "Content-Type: text/html\r\n"
                 "Pragma: no-cache\r\n"
                 "\r\n"
               ));  // appear on the page
}

static word StreamPage() {
  bfill = ether.tcpOffset();
  HeadPage();
  bfill.emit_p(PSTR(
                 "<meta http-equiv='refresh' content='4'/>" //or refresh the page
                 "<title>StreamPage</title>" // title of home page
                 "<form method=get>" // check box to let led on or off
                 "<h1>"
                 "latitude  = $D.$D$D  
"
                 "longitude = $D.$D$D  
"
                 //"altitude  = $D.$D$D  
"
                 "</h1>"
                 "<input type=submit value=Submit>"
                 "</form>"
               ), latitudeInt , latitudeFloat1 , latitudeFloat2 , longitudeInt , longitudeFloat1 , longitudeFloat2 ); // appear on the page
  return bfill.position();
}

////////////////////////////////////////////////////////

long getDecimal1(float val)
{
  int intPart = int(val);
  long decPart = 10000 * (val - intPart); //I am multiplying by 1000 assuming that the foat values will have a maximum of 3 decimal places
  //Change to match the number of decimal places you need
  if (decPart > 0)return (decPart);       //return the decimal part of float number if it is available
  else if (decPart < 0)return ((-1) * decPart); //if negative, multiply by -1
  else if (decPart = 0)return (00);       //return 0 if decimal part of float number is not available
}
long getDecimal2(float val)
{
  int intPart = int(val);
  float decPart = 10000 * (val - intPart); //I am multiplying by 1000 assuming that the foat values will have a maximum of 3 decimal places
  //Change to match the number of decimal places you need
  int intPart2 = int(decPart);
  long decPart2 = 100 * (decPart - intPart2); //I am multiplying by 1000 assuming that the foat values will have a maximum of 3 decimal places
  //Change to match the number of decimal places you need
  if (decPart2 > 0)return (decPart2);       //return the decimal part of float number if it is available
  else if (decPart2 < 0)return ((-1) * decPart2); //if negative, multiply by -1
  else if (decPart2 = 0)return (00);       //return 0 if decimal part of float number is not available
}

///////////////////////////////////////////////////////

void setup() {
  Serial.begin(9600);
  Serial.println("[Test]");
  gpsPort.begin(9600);
  if ( ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)
    Serial.println( "Faild to access Ethernet controller");
  else
    Serial.println( "Ethernet controller Accessed !!!! ");
  ether.staticSetup(myip);
  /////////////////////////////////////////////////
}

void loop() {
  // put your main code here, to run repeatedly:

  if (gps.available( gpsPort )) {
    fix = gps.read();

    // What to do with GPS data when it's ready?
    Serial.print( F("time: ") );
    Serial.println( fix.dateTime.seconds ); // print the current second
    //test code for lat long alt


    latitudeInt    = 0 ;
    latitudeFloat1 = 0 ;
    latitudeFloat2 = 0 ;

    longitudeInt   = 0 ;
    longitudeFloat1 = 0 ;
    longitudeFloat2 = 0 ;

   /* altitudeInt    = 0 ;
    altitudeFloat1 = 0 ;
    altitudeFloat2 = 0 ;*/
    if (fix.valid.location) {

      Serial.print( F("latitude: ") );
      //Serial.println( fix.latitude(), 6 );
      latitudeInt   = fix.latitude();
      float x = fix.latitude();
      latitudeFloat1 = getDecimal1(x);
      latitudeFloat2 = getDecimal2(x);
      Serial.println( fix.latitude(), 6 );
      //Serial.println( latitudeInt );
      //Serial.println( latitudeFloat );
      //Serial.print( ',' );

      Serial.print( F("longitude: ") );
      //Serial.println( fix.longitude(), 6 );
      longitudeInt   = fix.longitude();
      x = fix.longitude();
      longitudeFloat1 = getDecimal1(x);
      longitudeFloat2 = getDecimal2(x);
      Serial.println( fix.longitude(), 6 );
      //Serial.println( longitudeInt );
      //Serial.println( longitudeFloat );

      /*Serial.print( F("Altitude: ") );
      //Serial.println( fix.altitude() );
      altitudeInt   =  fix.altitude();
      x = fix.altitude();
      altitudeFloat1 =  getDecimal1(x);
      altitudeFloat2 =  getDecimal2(x);
      Serial.println( fix.altitude(), 6 );
      //Serial.println( altitudeInt );
      //Serial.println( altitudeFloat );*/

    }



    // Or, you can use 'fix' anywhere in the sketch, like in StreamPage().
  }

  word pos = ether.packetLoop( ether.packetReceive() );

  if (pos) { // check if valid tcp data is received
    // data received from ethernet
    ///////////////////////////////////////////////////////////////////////////

    if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Forward") != 0) {
      Serial.println("Received ON command");

    }
    else if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Backward") != 0) {
      //Serial.println("Received OFF command");

    }
    else if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Left") != 0) {
      //Serial.println("Received OFF command");

    }
    else if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Right") != 0) {
      //Serial.println("Received OFF command");

    }
    else if (strstr((char *)Ethernet::buffer + pos, "GET /?MotorMode=Stop") != 0) {
      //Serial.println("Received OFF command");

    }
    ////////////////////////////////////////////////////////////////////////////
    word n;
    n = StreamPage() ;
    ether.httpServerReply(n);
    //delay(500);
  }
}