Code doesnt work as GPS gets stuck.

I'm basically trying to make a device that pulls the GPS information from a SIM808 and pushes it to a server online via GPRS.

I am sorry as I am not good at programming so ive spent days trying to debug this but i have not been able to figure out whats wrong.

Ill get the basics out of the way.

Board: Mega2560 (I know its overkill but its what i had in hand)
SIM+GPS sensor: SIM808 Module TinyGPS v13 (pins 46,48 - RX/TX AltSoftSerial)
RTC: DS1307 (RX1/TX1)
Arduino IDE v1.8.8

Down to the specifics,
I started with SoftwareSerial Library but i heard it was inefficient so i switched to AltSoftSerial.
The serial library is working as I do get output using AT commands.
The problem is that when i load up the stock TinyGPS example sketch sometimes it works sometimes it gives me error saying the GPS module wasn't detected.

I tried using AT commands directly or a different GPS library but i cant figure out how to pass on data to a database over GPRS.

but basically I am sure the wiring isn't faulty nor is the GPS module (i hope) as it gives an output at ATCommands, even the RTC module is confirmed working. I know the URL in the ATCommand is fake but that is for demonstration purposes.

Coming to the code below. If i change the bool newData = false; to bool newData = true; the the code works just once with wrong GPS information. If i let the code be the way it is now there is no response on serial monitor.

any help would be greatly appreciated. Thank You

Edit: The problem has been solved, it wasn't working when i used both GPS and GPRS on SIM808 i switched to NeoGPS 6m module and GPRS from SIM800/808 it worked. Thank You

#include <AltSoftSerial.h>
#include <TinyGPS.h>
#include <Wire.h>
#include <DS1307new.h>


TinyGPS gps;

SoftwareSerial sGPS (46,48);

uint16_t startAddr = 0x0000;  //Start address to store in NVRam
uint16_t lastAddr;        //New address to store in NVRam
uint16_t TimeIsSet = 0xaa55;  //Helper that time must not be set again

int currentDay = 0;
int currentMonth = 0;
int currentYear = 0;
int currentHour = 0;
int currentMinute = 0;

float flat, flon;
float previousFLat = 0.0;
float previousFLon = 0.0;

void setup()
{

  
  Serial.begin(9600);
  sGPS.begin(9600);
  

  /*
  Control the clock.
  Clock will only be set if NV-RAM Address does not contain 0xaa.
  DS1307 should have a battery backup.
  */
  
  RTC.getRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
  if (TimeIsSet != 0xaa55)
  {
    RTC.stopClock();
        
    RTC.fillByYMD(2011,4,8);
    RTC.fillByHMS(22,7,0);
    
    RTC.setTime();
    TimeIsSet = 0xaa55;
    RTC.setRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
    RTC.startClock();
  }
  else
  {
    RTC.getTime();
  }
  
  /*
   Control Register for SQW pin which can be used as an interrupt.
*/
  RTC.ctrl = 0x00;                      // 0x00=disable SQW pin, 0x10=1Hz,
                                        // 0x11=4096Hz, 0x12=8192Hz, 0x13=32768Hz
  RTC.setCTRL();
  delay(5000);
  }
  
  void loop()
{
    RTC.getTime();
  
    currentDay = RTC.day;
    currentMonth = RTC.month;
    currentYear = RTC.year;
    currentHour = RTC.hour;
    currentMinute = RTC.minute;
  

  bool newData = false;
  
  //For one second we parse GPS data and report some key values
  
  for (unsigned long start = millis(); millis() - start < 1000;)
  
  {
  while (sGPS.available())
    {
      char c = sGPS.read();
      Serial.write(c); // uncomment this line if you want to see the GPS data flowing
      if (gps.encode(c)) // Did a new valid sentence come in?
      newData = true;
    }
  
  }
  
  if (newData)
  {
    unsigned long age;
    gps.f_get_position(&flat, &flon, &age);
  
    if ((flat !=previousFLat)||(flon !=previousFLon))
    {
      previousFLat = flat;
      previousFLon = flon;
      Serial.print (previousFLat,6);
      Serial.println(previousFLon,6);
      SendSQL();
    }
  }
  
}  

  
  void SendSQL()
  {
  
    //Serial.println("Start Send");
    Serial.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\"");
    delay(500);
    Serial.println("AT+SAPBR=3,1,\"APN\",\"airtelgprs.com\"");
    delay(500);
    Serial.println("AT+SAPBR=1,1");
    delay(500);
    Serial.println("AT+SAPBR=2,1");
    delay(3000);
    Serial.println("AT+HTTPINIT");
    delay(500);
    Serial.println("AT+HTTPARA=\"CID\",1");
    delay(500);
    Serial.print("AT+HTTPARA=\"URL\",\"www.google.com/add_data.php?");
    Serial.print("addDay=");
    
      if (currentDay <10)
        {
        Serial.print("0");
        Serial.print(currentDay);
        }
        
      else
        {
        Serial.print(currentDay);
        }
        
      Serial.print("/");
      
      if (currentMonth < 10)
        {
          Serial.print("0");
          Serial.print(currentMonth);
        }
        
      else 
        {
          Serial.print(currentMonth);
        }
          
      Serial.print("/");
      Serial.print(currentYear);
      Serial.print("&addHour=");
      
      if (currentHour <10) 
        {
          Serial.print("0");
          Serial.print(currentHour);
        }
        
        else
        
        {
          Serial.print(currentHour);
        }
        
        Serial.print(":");
        
        if (currentMinute <10) 
        
        {
          Serial.print("0");
          Serial.print(currentMinute);
        }
        
        else
        
        {
          Serial.print (currentMinute);
        }
        
          Serial.print ("&addLati=");
          Serial.print(previousFLat, 6);
          Serial.print("&addLongti=");
          Serial.print(previousFLon, 6);
          Serial.println("\"");
          delay(3000);
          Serial.println("AT+HTTPACTION=0");
          delay(15000);
          Serial.println("AT+HTTPTERM");
          delay(500);
          Serial.println("AT+SAPBR=0,1");
          delay(500);
          Serial.println("Finish");
        }

This line is your problem:

  for (unsigned long start = millis(); millis() - start < 1000;)

What do you think it does? You assign start = millis() and then immediately subtract start from millis() so it will be zero which is less than 1000 so your for() loop never executes and that is where you are seeing if anything is available on the gps.

blh64:
This line is your problem:

  for (unsigned long start = millis(); millis() - start < 1000;)

What do you think it does? You assign start = millis() and then immediately subtract start from millis() so it will be zero which is less than 1000 so your for() loop never executes and that is where you are seeing if anything is available on the gps.

Thank You for your prompt reply, can you please tell me how else can i structure this ?

ihertz:
Thank You for your prompt reply, can you please tell me how else can i structure this ?

what are you trying to accomplish?

blh64:
what are you trying to accomplish?

all i need to do is basically send gps latitude and longitude data to a server.

i have the server and the server side script ready only the arduino is giving me problems with the gps module being finiky.

I can get output using AT commands but when i use any library (i.e. tiny gps) i can barely get it to work.

Even when I try the basic sketch to make the GPS module work using SIM808 and TinyGPS it doesn't work.

all the wiring is right and pins have been defined properly as it has worked once or twice when i burned the same code.

If all you need to do is basically send gps data to a server, why is that line even in your code? If you don't want to send the data any faster than every second, then just use delay() since you aren't doing anything else.

I would suggest you get the GPS debugged and working through the serial monitor and then work you way up to sending it to a server and using the RTC.

The problem has been solved, it wasn't working when i used both GPS and GPRS on SIM808 i switched to NeoGPS 6m module and GPRS from SIM800/808 it worked. Thank You