Gps logger

Hi, my name is Leonardo and Im trying to make a gps logger but I have a little problem. The gps data comes ok from the serial port, I got position and time, but at the time to log data to the sd card it only logs the first line, here is the sketch, I think the problem is in the part of the logfile commands but I cant figer out the problem.
Sd shield and gps works just fine, the problem is in the sketch.

#include <SD.h>

#include <SoftwareSerial.h>

SoftwareSerial gpsSerial(6, 5); // RX, TX (TX not used)
const int sentenceSize = 80;

char sentence[sentenceSize];

#define LOG_INTERVAL 100

const int chipSelect = 10;
File logfile;
long idd = 0;
long ids = 0;
long idm = 0;
long idh = 0;

void error(char *str)
{
Serial.print("error: ");
Serial.println(str);

while(1);
}

void setup()
{
Serial.begin(9600);
gpsSerial.begin(9600);

Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card Failed or not present");
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!
}
}

if (! logfile) {
error("couldnt create file");
}

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

void loop()
{
static int i = 0;
if (gpsSerial.available())
{
char ch = gpsSerial.read();
if (ch != '\n' && i < sentenceSize)
{
sentence = ch;

  • i++;*
  • }*
  • else*
  • {*
    _ sentence = '\0';_
    * i = 0;*
    * displayGPS();*
    * }*
    * }*

}
void displayGPS()
{
* char field[20];*
* getField(field, 0);*
* if (strcmp(field, "$GPRMC") == 0)*
* {*
* Serial.print("Tiempo: ");*
* getField(field, 1);*
* logfile.print(field);*
* logfile.print("\t");*
* Serial.println(field);*
* Serial.print("Lat: ");*
* getField(field, 3); // number*
* logfile.print(field);*
* logfile.print("\t");*
* Serial.print(field);*
* getField(field, 4); // N/S*
* logfile.print(field);*
* logfile.print("\t");*
* Serial.println(field);*
* Serial.print("Long: ");*
* getField(field, 5); // number*
* logfile.print(field);*
* logfile.print("\t");*
* Serial.print(field);*
* getField(field, 6); // E/W*
* logfile.print(field);*
* logfile.print("\t");*
* Serial.println(field);*
* Serial.print("Velocidad: ");*
* getField(field, 7);*
* logfile.println(field);*
* logfile.close();*
_ Serial.println(field) * 1.854;_

* }*
}
void getField(char* buffer, int index)
{
* int sentencePos = 0;*
* int fieldPos = 0;*
* int commaCount = 0;*
* while (sentencePos < sentenceSize)*
* {*
* if (sentence[sentencePos] == ',')*
* {*
* commaCount ++;*
* sentencePos ++;*
* }*
* if (commaCount == index)*
* {*
* buffer[fieldPos] = sentence[sentencePos];*
* fieldPos ++;*
* }*
* sentencePos ++;*
* }*
* buffer[fieldPos] = '\0';*
}

Ok, for future posts, please put you code like this

#include <SD.h>

#include <SoftwareSerial.h>

SoftwareSerial gpsSerial(6, 5); // RX, TX (TX not used)
const int sentenceSize = 80;

char sentence[sentenceSize];

#define LOG_INTERVAL 100

const int chipSelect = 10;
File logfile;
long idd = 0;
long ids = 0;
long idm = 0;
long idh = 0;

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);

  while(1);
}

void setup()
{
  Serial.begin(9600);
  gpsSerial.begin(9600);
  
 
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card Failed or not present");
    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!
    }
  }
  
  if (! logfile) {
    error("couldnt create file");
  }
  
  Serial.print("Logging to: ");
  Serial.println(filename);
}

void loop()
{
  static int i = 0;
  if (gpsSerial.available())
  {
    char ch = gpsSerial.read();
    if (ch != '\n' && i < sentenceSize)
    {
      sentence = ch;
      i++;
    }
    else
    {
     sentence = '\0';
     i = 0;
     displayGPS();
    }
  }
  
}

void displayGPS()
{
  char field[20];
  getField(field, 0);
  if (strcmp(field, "$GPRMC") == 0)
  {
    Serial.print("Tiempo: ");
    getField(field, 1);
    logfile.print(field);
    logfile.print("\t");
    Serial.println(field);
    Serial.print("Lat: ");
    getField(field, 3);  // number
    logfile.print(field);
    logfile.print("\t");
    Serial.print(field);
    getField(field, 4); // N/S
    logfile.print(field);
    logfile.print("\t");
    Serial.println(field);
    Serial.print("Long: ");
    getField(field, 5);  // number
    logfile.print(field);
    logfile.print("\t");
    Serial.print(field);
    getField(field, 6);  // E/W
    logfile.print(field);
    logfile.print("\t");
    Serial.println(field);
    Serial.print("Velocidad: ");
    getField(field, 7);
    logfile.println(field);
    logfile.close();
    Serial.println(field) * 1.854;
    
  }
}

void getField(char* buffer, int index)
{
  int sentencePos = 0;
  int fieldPos = 0;
  int commaCount = 0;
  while (sentencePos < sentenceSize)
  {
    if (sentence[sentencePos] == ',')
    {
      commaCount ++;
      sentencePos ++;
    }
    if (commaCount == index)
    {
      buffer[fieldPos] = sentence[sentencePos];
      fieldPos ++;
    }
    sentencePos ++;
  }
  buffer[fieldPos] = '\0';
}

It makes things SO MUCH EASIER FOR US TO WORK WITH, ok.
Now because it only prints one time is because you have that part of the code in Void SETUP() and not where it should be in Void LOOP().

so put this:

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!
    }

inside your void LOOP() and you should get prints everytime.

Sorry, next time Ill post it that way. And thanks for replying, let me tell that the way you told me is not coming along, it says that it cant create the file

What Im saying is that part of the sketch is fine, I think that the problem is that the logfile command is the void gpsdisplay and I think it should be in the loop as you said, but I cant get the way to put that part there

ok so put that part back and maybe post what you are getting.

Does it ONLY write the data one time or does it write the data then put in junk?

It only writes the first line of data, the it close the file, if you hit restart in the arduino it opens anotrher file and do the same thing, just the first line of information.

void displayGPS()
{
  char field[20];
  getField(field, 0);
  if (strcmp(field, "$GPRMC") == 0)
  {
    Serial.print("Tiempo: ");
    getField(field, 1);
    logfile.print(field);
    logfile.print("\t");
    Serial.println(field);
    Serial.print("Lat: ");
    getField(field, 3);  // number
    logfile.print(field);
    logfile.print("\t");
    Serial.print(field);
    getField(field, 4); // N/S
    logfile.print(field);
    logfile.print("\t");
    Serial.println(field);
    Serial.print("Long: ");
    getField(field, 5);  // number
    logfile.print(field);
    logfile.print("\t");
    Serial.print(field);
    getField(field, 6);  // E/W
    logfile.print(field);
    logfile.print("\t");
    Serial.println(field);
    Serial.print("Velocidad: ");
    getField(field, 7);
    logfile.println(field); 
    logfile.close(); // this is the command that  think is doing that problem, the thing is that if I take it off it doesn`t log anything, just the file name
    Serial.println(field) * 1.854;
    
  }
}

it logs in the Serial.print("Tiempo: "); or the very first line, getField(field, 0); ?

And quit crossposting. It wastes everyone's time.

203042.987 3424.8937 S 05835.1069 W 000.0
This is what it logs, the information is correct, it is what I need to log but I need to log the rest of the datalines and not just one.
It seems to me that the part that logs shoul be on the loop and not in the void gpsdisplay but I don`t know how to do that.

What you can try is to print the data on the serial monitor before it goes into the SD card that way you will see exactly what you are getting, and where the error lies.

because in order for it to print the data,

 if (ch != '\n' && i < sentenceSize) //this must be true for sentence to update
    {
      sentence = ch;
      i++;
    }
    else //other wise it will reset sentence and i and then write the data to the SD card.
    {
     sentence = '\0';
     i = 0;
     displayGPS();

Its within this part, that it does not write the other data.

OR...

It is possible that getField is not returning anything TO write.

EDIT: ok disgard this post

In the serial monitor it comes the data just fine, I get position, time and speed, the thing is I dont get that info in the sd. Someone said that I open the file in the setup, and then imediately close after the first line when I put logfile.close();... the thing is that if I remove that comand it doesnt log anything at all, just the filename in the sd card but without data on it.

logfile.close(); // this is the command that think is doing that problem, the thing is that if I take it off it doesn`t log anything, just the file name

ok so then did you try to clear Ch after it goes to displayGPS() ?

print the CH and sentence data to the serial monitor, and see if any more data goes goes into them. If not then it should tell you where your problem is.

EDIT: ughh, disregard this post.

if (gpsSerial.available())
  {
    char ch = gpsSerial.read();
    if (ch != '\n' && i < sentenceSize)
    {
      sentence = ch;
      i++;
    }
    else
    {
     sentence = '\0';
     i = 0;
     displayGPS();
    }
logfile.close(); // try to put it here
  }
if (gpsSerial.available())
  {
    char ch = gpsSerial.read();
    if (ch != '\n' && i < sentenceSize)
    {
      sentence = ch;
      i++;
    }
    else
    {
     sentence = '\0';
     i = 0;
     displayGPS();
    }
  }
else {
logfile.close(); //OR try to put it here
}

Data continues going out, the thing is that it no longer logs to the sd card, if I hit the restart button in the arduino it generates another file but it does the same thing again, it logs the first line.

I have allready tryed and nothing happens, puting it that way it only log the name of the file with nothing on it.

If all else fails, look at this:

no, nothing at all, just logs the filename with nothing on it

I wonder if display GPS is not returning back to the loop after it writes the data, try this.

void displayGPS()
{
  char field[20];
  getField(field, 0);
  if (strcmp(field, "$GPRMC") == 0)
  {
    Serial.print("Tiempo: ");
    getField(field, 1);
    logfile.print(field);
    logfile.print("\t");
    Serial.println(field);
    Serial.print("Lat: ");
    getField(field, 3);  // number
    logfile.print(field);
    logfile.print("\t");
    Serial.print(field);
    getField(field, 4); // N/S
    logfile.print(field);
    logfile.print("\t");
    Serial.println(field);
    Serial.print("Long: ");
    getField(field, 5);  // number
    logfile.print(field);
    logfile.print("\t");
    Serial.print(field);
    getField(field, 6);  // E/W
    logfile.print(field);
    logfile.print("\t");
    Serial.println(field);
    Serial.print("Velocidad: ");
    getField(field, 7);
    logfile.println(field); 
    logfile.close(); // this is the command that  think is doing that problem, the thing is that if I take it off it doesn`t log anything, just the file name
    Serial.println(field) * 1.854;
    
  }
return; //[b] MAKE THIS CHANGE[/b]
}

same result, it logs only the first line

Ok well then Im stumped, try the link I posted and copy and paste what you need to get data from the GPS onto the SD card.

Maybe someone else can figure out what is wrong with your code. I can't figure it out.