I can not write the received data on a SD card-Arduino GPS tracker

Hello!

I want to build a GPS tracker with Arduino UNO and SIM808. I can see the received data, I create the file on the SD card but I can not write latitude and longitude in the file...

Here is the code I am running.

 #include <TinyGPS++.h>
    #include <SoftwareSerial.h>
    #include "SD.h"
 #include<stdio.h>
 #include<string.h>
 #define DEBUG true

//    //input for the light detecting sensor (LDR)
//    int LDR = A3;   

    //inputs and triggers for GPS
    TinyGPSPlus gps;
    SoftwareSerial ss (7, 8); // TX=, RX= 

    // digital pin 4 for the SD cs line
    const int chipSelect = 4;
    File logfile;// the logging file

char filename[] = "LOGGER00.CSV";

    //data logger timing and other 
    #define LOG_INTERVAL  1000 // mills between entries
    #define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
    uint32_t syncTime = 0; // time of last sync()
    #define ECHO_TO_SERIAL   1 // echo data to serial port
    #define WAIT_TO_START    0 // Wait for serial input in setup()    

    void setup() {
      Serial.begin(9600);
      ss.begin(9600);
      Serial.println();
      


      #if WAIT_TO_START
        Serial.println("Type any character to start");
        while (!Serial.available());
      #endif //WAIT_TO_START

      // initialize the SD card
      Serial.print("Initializing SD card...");
//Serial.println("AT+CGPSPWR=1\r");
//delay(100);
//Serial.print("AT+CREG?\r");
//delay(100);
//Serial.print("AT+CMGF=1\r");
//Serial.print("AT+CGPSINF?");

    
      
      pinMode(10, OUTPUT);

      // see if the card is present and can be initialized:
      if (!SD.begin(chipSelect)) {
        Serial.println("Card failed, or not present");
        // don't do anything more:
        return;
      }
      Serial.println("card initialized.");

      // create a new file
//      char filename[] = "LOGGER00.CSV";
//      for (uint8_t i = 0; i < 100; i++) {
//        filename[6] = i/10 + '0';
//        filename[7] = i%10 + '0';
//        if (! SD.exists(filename)) {
//          // only open a new file if it doesn't exist
//          logfile = SD.open(filename, FILE_WRITE); 
//          break;  // leave the loop!
//        }
//      }

      Serial.print("Logging to: ");
      Serial.println(filename);

      logfile.println("date,time,lat,long,light");

      #if ECHO_TO_SERIAL
        Serial.println("lat,log,date,time,altitude");
      #endif// attempt to write out the header to the file
         Serial.print("AT+CGPSPWR=1\r");

      
    }

void loop(void) {  

   // delay for the amount of time we want between readings
   delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
   getgps();
   //here is GPS data (if I got it right)
   while (ss.available() > 0)
  { gps.encode(ss.read());

File logfile = SD.open(filename, FILE_WRITE);

   logfile.print(gps.date.value());
   logfile.print(", ");
   logfile.print(gps.time.value());
   logfile.print(", ");
   logfile.print(gps.location.lat(), 6);
   logfile.print(", ");
   logfile.print(gps.location.lng(), 6);
   logfile.print(", ");
  }
  #if ECHO_TO_SERIAL
      Serial.print(gps.date.value());
      Serial.print(", ");  
      Serial.print(gps.time.value());
      Serial.print(", ");  
      Serial.print(gps.location.lat(), 6);
      Serial.print(", ");  
      Serial.print(gps.location.lng(), 6);
      Serial.print(", ");  
   #endif
  logfile.close();
  delay(1000);
  
//   //Read light intensity
//   float lux = 500/(10.72/(5-(photocellReading * 0.00488759))*(photocellReading * 0.00488759));
//   logfile.println(lux);
//   #if ECHO_TO_SERIAL
//      Serial.println(lux);
//   #endif //ECHO_TO_SERIAL

   //write data to disk
//   if ((millis() - syncTime) < SYNC_INTERVAL) return;
//   syncTime = millis();
//   logfile.flush();
}

void getgps(void)
{
   sendData( "AT+CGPSPWR=1",1000,DEBUG); 
   sendData( "AT+CGPSSEQ=RMC",1000,DEBUG); 
   sendData( "AT+CREG?",1000,DEBUG); 
   sendData( "AT+CMGF=1",1000,DEBUG);
   sendData( "AT+CSQ",1000,DEBUG); 
   sendData( "AT+CSMP=17,167,0,16",1000,DEBUG); 
//   sendData( "AT+CMGS=\"+40721071002\"\r ",1000,DEBUG);
   


}

String sendData(String command, const int timeout, boolean debug)
{
    String response = "";    
    ss.println(command); 
    long int time = millis();   
    while( (time+timeout) > millis())
    {
      while(ss.available())
      {       
        char c = ss.read(); 
        response+=c;
      }  
    }    
    if(debug)
    {
      Serial.print(response);
    }    
    return response;
}

Can anyone help me please? I really need help, and I am running out of time...

I am looking forward to answer me! Thank you!

How many post do you plan to open in this topic? I think I've already seen 4... bad practice

Madalina:
Can anyone help me please? I really need help, and I am running out of time...

Several suggestions have been made as to how to tie the various bits together but you seem to be expecting for someone to correct or re-write your program and then open another thread when you dont get the response you seem to expect.

If you can read the GPS, you now need to use one of the readily available GPS libraries that will allow you to convert the GPS NMEA data into variables that you can then print on screen or write to SD card. Most libraries have examples to help you along.

Why are you running out of time ?

{ gps.encode(ss.read());

Aren't you supposed to check the value returned by gps.encode() to see if that one character completed a message?

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }

That 'return' will just skip the rest of setup() and go straight to loop(). If you want the code to stop there, use

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while(true);
  }