GSM module doesn't work when interfaced with GPS Sensor

Hello everybody,

I am trying to read latitude and longitude from a GPS sensor and send the same to the server through a HTTP request by using a GSM module. I have used SIM900A GSM module. The GSM module and the GPS sensor are interfaced to Arduino MEGA. The HTTP requests work fine when the GSM module is isolated from GPS sensor and interfaced with Arduino. However when i interface it with GPS sensor, some of the AT commands throw errors.

Here is the code snippet:

void loop() {
  url="http://*******************************";
   while(Serial1.available()>0)
  {
     if(gps.encode(Serial1.read()))
    {
      lati=gps.location.lat();
      lon=gps.location.lng();
      }

}
if(lati!=0.000000 && lon!=0.000000)
{
 dtostrf(lati,8,6,latis);
     dtostrf(lon,8,6,lons);
     url.concat(latis);
     url.concat(",");
     url.concat(lons);
 delay(2000);
  url.trim();
   track(url);
   //Serial.println(url);
}
    delay(5000);
 }

void track(String url)
{
  url.trim();
  //Serial.println(url);
   Serial.println("AT+CREG?");
  delay(5000);
   while(Serial.available()>0)
     //Serial.write(Serial.read());
     data+=(char)Serial.read();
  delay(1000);
  Serial.println("AT+SAPBR=1,1");
  Serial.flush();
  delay(5000);
   while(Serial.available()>0)
    data+=(char)Serial.read();
  Serial.println("AT+HTTPINIT");
  delay(5000);
   while(Serial.available()>0)
   //Serial.write(Serial.read());
    data+=(char)Serial.read();
   Serial.println("AT+HTTPPARA=\"URL\",url");
  delay(5000);
  while(Serial.available()>0)
    data+=(char)Serial.read();
  Serial.println("AT+HTTPPARA=\"CID\",1");
  delay(5000);
   while(Serial.available()>0)
    data+=(char)Serial.read();
  Serial.println("AT+HTTPACTION=0");
  delay(10000);
   while(Serial.available()>0)
    data+=(char)Serial.read();
  Serial.println("AT+HTTPREAD");
  delay(2000);
  while(Serial.available()>0)
    buf+=(char)Serial.read();
    Serial.println("AT+HTTPTERM");
  delay(5000);
  while(Serial.available()>0)
    data+=(char)Serial.read();
    Serial.println(data);

The following AT commands throw error:

AT+HTTPACTION=0 throws one of the following errors:

  • +HTTPACTION:0,603,0
  • +HTTPACTION:0,601,0
  • ERROR

Also, sometimes AT+HTTPPARA="URL",url throws an "ERROR".

Any suggestions to fix them?

Thank you!

Don't use the String class. Use char arrays instead (aka C strings).

Don't try to hold on to the entire response strings. Using String to do this can exhaust memory, causing weird errors. Parse/scan them with a State Machine as they are received.

Avoid delay. Use State Machine instead. See Blink Without Delay and "How to do several things at a time" in Useful Links.

Use NeoGPS + NeoHWSerial to handle GPS in the background. The latest GPS fix will always be available, even if the GSM module takes a long time to do something (or you continue to use delay).

Cheers, /dev