Go Down

Topic: Gps logger (Read 2054 times) previous topic - next topic

HazardsMind

If all else fails, look at this:

http://arduino.cc/en/Tutorial/Datalogger

My GitHub:
https://github.com/AndrewMascolo?tab=repositories

leotriador

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

HazardsMind

I wonder if display GPS is not returning back to the loop after it writes the data, try this.
Code: [Select]
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]
}
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

leotriador

same result, it logs only the first line

HazardsMind

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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

leotriador

ok, thaks a lot for your time

PaulS

You need to open the file, write to it, and close the file ALL IN ONE FUNCTION. Show the code where you are doing that!

leotriador

Code: [Select]
Here is the code
[code#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[i] = ch;
      i++;
    }
    else
    {
     sentence[i] = '\0';
     i = 0;
     displayGPS();
    }
  }
 
}

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

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';
} ]

HazardsMind

Ohhh, I see what PaulS is saying, ok

look at this sample code again:  http://arduino.cc/en/Tutorial/Datalogger

more to the point this part:
Code: [Select]
File dataFile = SD.open("datalog.txt", FILE_WRITE); //[b]Right here it opens the file[/b]

  // if the file is available, write to it:
   if (dataFile) {                                                                //[b]Does some work[/b]
     dataFile.println(dataString);                                       //[b]It then writes the data to that file[/b]
     dataFile.close();                                                         //[b]Then right after, it closes the file, all in one shot![/b]
     // print to the serial port too:
     Serial.println(dataString);
   } 
   // if the file isn't open, pop up an error:
   else {
     Serial.println("error opening datalog.txt");
   }
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

leotriador

I have seen it, the thing is that this part of the sketch is in the loop, that is my problem, I can`t get to put the log command in the loop.
Do you have some idea for parsing the gps data on a string and put it on the loop?
Because if I open the file in the void gpsdisplay it generates lots of diferent files with only one line of data.

HazardsMind

#25
Nov 29, 2012, 05:30 pm Last Edit: Nov 29, 2012, 05:39 pm by HazardsMind Reason: 1
Also this is more like what you need to do, STUDY THE CODE CAREFULLY!!!
http://arduino.cc/en/Tutorial/Files

RE-EDIT:
From what I see, you dont need to have the logfile.open in your SETUP, what you can try is to make the change I said above and just comment //  out the one you have in your setup and see if the data goes in properly.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

HazardsMind

OK you tried to put logfile.open() in displayGPS, and that created many files, all with one line in them.
You want just ONE file with multiple lines.

well it seems that because the logfile.close() is being done when the code HAS AVAILABLE data, it only writes one line then closes the file and does NOT reopen it. You need to take that .close line out and put it somewhere when there is NO available data. That way it will still write the data to the SD card and only close when there is no data. You may need to tell it to close with a button when you go to shut down the ARD.

I'm not sure if Arduino has a way to update the file when it is created instead of constantly creating new files each time. Look into that.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

HazardsMind

#27
Nov 29, 2012, 06:46 pm Last Edit: Nov 29, 2012, 06:51 pm by HazardsMind Reason: 1
I found this for you, look at what it puts into the file.

www.ladyada.net/products/microsd/

Also one last look at your code,
Code: [Select]



void displayGPS()
{
  char field[20];
  logfile.println();      //CHANGE: I think you had it correct the first time but it was not being told to make a new line
  getField(field, 0);    //when there was new data, so it put everything back to back.
  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]
}














My GitHub:
https://github.com/AndrewMascolo?tab=repositories

PaulS

This code:
Code: [Select]
  // 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!
    }
  }

determines the name of a file that does  not already exist, and leaves that file open. Move char filename[] out of the setup() function, to make it a global variable.

Then, after the SD.open(), before the break, close the file.

Then,  copy the SD.open() statement to displayGPS(). This will open the same file, not create a new one.

leotriador

Thanks to all, I have solved the problem, I just replace the function logfile.close() with logfile.flush()

I post the code if anyone likes to use it

Code: [Select]

#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[i] = ch;
      i++;
    }
    else
    {
     sentence[i] = '\0';
     i = 0;
     char field[20];
      getField(field, 0);
  if (strcmp(field, "$GPRMC") == 0)
  {
   
    Serial.print("Tiempo: ");
    getField(field, 1);
    Serial.print(field);
    logfile.print(field);
    logfile.print("\t");   
   
    Serial.print("Lat: ");
    getField(field, 3);  // number
    Serial.print(field);
    logfile.print(field);
    logfile.print("\t");
    getField(field, 4); // N/S
    Serial.print(field);
    logfile.print(field);
    logfile.print("\t");
   
    Serial.print("Long: ");
    getField(field, 5);  // number
    Serial.print(field);
    logfile.print(field);
    logfile.print("\t");
    getField(field, 6);  // E/W
    Serial.print(field);
    logfile.print(field);
    logfile.print("\t");
   
    Serial.print("Velocidad: ");
    getField(field, 7);
    Serial.println(field) * 1.854;
    logfile.println(field);
    logfile.flush();   
  }
    }
  }
 
}

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';
}


Now does anyone know how to configure the venus gps in 10hz with the gps viewer?

Go Up