Question

I have a sketch to take information (Lat, Long) from an EM-406a GPS receiver and write the information to an SD card on an Arduino shield.

The program is as follows:

#include <TinyGPS++.h>

#include <SoftwareSerial.h>

#include <SD.h>

TinyGPSPlus gps;
SoftwareSerial ss(4, 3); //pins for the GPS
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

void setup()
{
  Serial.begin(115200);  //for the serial output
  ss.begin(4800);  //start ss at 4800 baud
  Serial.println("gpsLogger by Aaron McRuer");
  Serial.println("based on code by Mikal Hart");
  Serial.println();
  //initialize the SD card 
  if(!card.init(SPI_FULL_SPEED, 9))
  {
    Serial.println("card.init failed"); 
  }
  //initialize a FAT volume
  if(!volume.init(&card)){
    Serial.println("volume.init failed"); 
  }
  //open the root directory
  if(!root.openRoot(&volume)){
    Serial.println("openRoot failed"); 
  }

  //create new file
  char name[] = "WRITE00.TXT";
  for (uint8_t i = 0; i < 100; i++){
    name[5] = i/10 + '0';
    name[6] = i%10 + '0';
    if(file.open(&root, name, O_CREAT | O_EXCL | O_WRITE)){
      break; 
    } 
  }
  if(!file.isOpen())
  {
    Serial.println("file.create"); 
  }
  file.print("Ready...\n");
}

void loop()
{
  bool newData = false;

  //For one second we parse GPS data and report some key values
  for (unsigned long start = millis(); millis() - start < 1000;)
  {
    while (ss.available())
    {
      char c = ss.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)
  {
    file.write(gps.location.lat());
    file.write("\n");
    file.write(gps.location.lng());
    file.write("\n");
  }

  file.close();
}

When I open up the file on the SD card when the program is finished executing, I get a message that it has an encoding error.

Arduino error

I'm currently inside (and unable to get a GPS signal, thus the 0), but the encoding problem needs to be tackled, and there should be as many lines as there are seconds that the device has been on. There's only that one. What do I need to do to make things work correctly here?

Was it helpful?

Solution

Closing the file in the loop, and never reopening it, is the reason there's only one set of data in your file.

Are you sure gps.location.lat() and gps.location.lng() return strings, not an integer or float? That would explain the binary data and the "encoding error" you see.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top