Saving data in SD card

Hello, I'm trying to get the data from a grove GPS device and save it in the SD card.

However, it seems that something is going wrong when I open the file.
Below is my code.

unsigned char buffer[512];                   // buffer array for data receive over serial port
int count=0;     // counter for buffer array

void loop()
{
    File dataFile = FileSystem.open("/mnt/sd/GPSdatalog.txt", FILE_APPEND);
    if (dataFile) {
        if (SoftSerial.available())                     // if date is coming from software serial port ==> data is coming from SoftSerial shield
        {
            while(SoftSerial.available())               // reading data into char array
            {
                buffer[count++]=SoftSerial.read();      // writing data into array
                if (count == 512) {
                    dataFile.close();
                    break;
                }
            }
            Serial.write(buffer,count);            // if no data transmission ends, write buffer to hardware serial port
            dataFile.write(buffer,count);
            clearBufferArray();                         // call clearBufferArray function to clear the stored data from the array
            count = 0;                                  // set counter of while loop to zero
        }
        dataFile.close();
    }
    else {
        Serial.println("error opening datalog.txt");
    }
    if (Serial.available())                 // if data is available on hardware serial port ==> data is coming from PC or notebook
        SoftSerial.write(Serial.read());        
}

There is two problem I can't figure out.

  1. I set the buffer to be in size of 512, but I only get 64 char one time.
  2. The file is normally opened only at the first time the when loop() is executed, then it keep failed to open the file.

The output looks like this.

$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPGSV,1,1,0$GPGGA,001826.801,,,,,0,0,,,M,,M,,*4C
$GPGSA,A,1,,,,,,,,,,,,,,error opening datalog.txt
$GPGGA,001831.801,,,,,0,0,,,M,,M,,*4A
$GPGSA,A,1,,,,,,,,,,,,,,error opening datalog.txt
$GPGGA,001859.801,,,,,0,0,,,M,,M,,*44
$GPGSA,A,1,,,,,,,,,,,,,,error opening datalog.txt
$GPGGA,001928.801,,,,,0,0,,,M,,M,,*43
$GPGSA,A,1,,,,,,,,,,,,,,error opening datalog.txt

PS. I'm just testing indoor, so the GPS data is all blank.

You post incomplete code? You could attach rest code.

GPSdataToSD is the complete code.

And YunFileLogger is a similar but simpler code I tried.

However, it also went wrong and the File also wasn’t opened correctly.

Following is the output of YunFileLogger in serial monitor.


File System datalogger Start!
32126
38642
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt

Lots of thanks for your reply.

GPSdataToSD.ino (2.33 KB)

YunFileLogger.ino (585 Bytes)

Try to declare File dataFile as a global variable and open the file in the setup function.

File dataFile;

void setup()
{
    dataFile = FileSystem.open("/mnt/sd/GPSdatalog.txt", FILE_APPEND);
}

void loop( )
{
.....

Yes, I have ever thought of doing so.

However, if I declare the File dataFile as a global variable and open the file in the setup function, there will be compilation error.

The error is below:

In file included from GPSdataLogger.ino:8:0:
C:\Program Files (x86)\Arduino\libraries\Bridge\src/FileIO.h: In member function 'File& File::operator=(const File&)':
C:\Program Files (x86)\Arduino\libraries\Bridge\src/FileIO.h:28:7: error: non-static reference member 'BridgeClass& File::bridge', can't use default assignment operator
class File : public Stream {
^
GPSdataLogger.ino: In function 'void setup()':
GPSdataLogger.ino:29:14: note: synthesized method 'File& File::operator=(const File&)' first required here

YunDatalogger or YunFileLogger should work, if not check your Arduino IDE version either use hourly build one or back to 1.5.X. I will start with YunDatalogger.

Your own code is missing:

void loop() {
...
  delay(1000);
}

@davidliu4417,
You are doing a FILE_APPEND. This is not well documented and should NOT be in the example, Yún Datalogger. Per the documentation, FILE_WRITE should work.

NOTE: another documentation ERROR, the URL says YunFileIOOpen, but the link to it and the document title says FileSystem.open()
.

Jesse

jessemonroy650:
You are doing a FILE_APPEND. This is not well documented and should NOT be in the example, Yún Datalogger.

Why would performing a fully implemented and appropriate operation not be recommended?

Per the documentation, FILE_WRITE should work.

FILE_WRITE will NOT work, unless one is only interested in the last sample. Each time the file is opened with FILE_WRITE the previous contents will be lost and overwritten with the new content. (Although, I suppose it would be possible to seek to the end of the file immediately after opening it, but why do that explicitly when that's what FILE_APPEND does automatically?)

In making your suggestion, I guess you're referring to this:

  • FILE_WRITE: open the file for reading and writing, starting at the end of the file.

This is clearly another documentation error. It would appear that the beginning of FILE_WRITE and the end of FILE_APPEND got mixed together while the middle was deleted. I'm guessing this is what it should read:

  • FILE_WRITE: open the file for writing, starting at the beginning of the file and replacing any existing content.
  • FILE_APPEND: open the file for reading and writing, starting at the end of the file.

NOTE: another documentation ERROR, the URL says YunFileIOOpen, but the link to it and the document title says FileSystem.open()
.

Is the name of the URL really that important?

ShapeShifter:
::::SNIP:::
Is the name of the URL really that important?

Okay we have multiple documentation error in that section. No wonder we continue to get questions on that. That definitely need to be fixed.

On the URL, it is definitely important. As the search engines use that for indexing the data when people make a query. As matter of fact the URL scores higher in the SEO than the internal description.

Now that that is out of the way, can we get to the OP's problem?

Jesse

@jessemonroy650, @ShapeShifter
Thanks a lot for your reply! What I wanna do is exactly what ShapeShifter said. If I change the mode to FILE_WRITE, I cannot collect all the data.

What you mean is that mode FILE_APPEND can perform correctly, and there is just documentation error?

Besides, I wonder if the method below really work.

File dataFile;

void setup()
{
    dataFile = FileSystem.open("/mnt/sd/GPSdatalog.txt", FILE_WRITE);
}

I saw people recommend this method, but there is compilation error for me when I'm doing so. Is it a correct way?
Is "File dataFile" declared as global variable?

davidliu4417:
What you mean is that mode FILE_APPEND can perform correctly, and there is just documentation error?

Correct.

The problem with your code is that you are using the "File" class. That is part of the SD library for use when the SD card is connected directly to the Arduino processor.

On the Yun, the SD card is connected to the Linux processor. You must use the "FileIO" class defined as part of the bridge library. So your declaration should be FileIO dataFile;

Also, you should call FileSystem.begin() in setup(), before you try to open the file, but after you call Bridge.begin().

Look at the Yun Datalogger example for a complete example of writing to a file.

ShapeShifter:
Correct.

The problem with your code is that you are using the "File" class. That is part of the SD library for use when the SD card is connected directly to the Arduino processor.

On the Yun, the SD card is connected to the Linux processor. You must use the "FileIO" class defined as part of the bridge library. So your declaration should be FileIO dataFile;

Also, you should call FileSystem.begin() in setup(), before you try to open the file, but after you call Bridge.begin().

Look at the Yun Datalogger example for a complete example of writing to a file.

@ShapeShifter,
please note, I am not complaining at you, but that is an awful set of NON-documented, or poorly documented circumstances for writing files to the SD. It's no wonder this is such a constant issue in the forum.

It appears I have more thing to document.
Jesse

jessemonroy650:
that is an awful set of NON-documented, or poorly documented circumstances for writing files to the SD.

For the Yun, the real crux of what is poorly documented is that SD card/file access, Ethernet connectivity, and WiFi connectivity is completely different on the Yun than on any other Arduino board:

  • The SD library that is used on every other Arduino will not work on the Yun (unless you add an SD card shield)
  • The WiFi library that is used on every other Arduino will not work on the Yun (unless you add a WiFi shield)
  • The Ethernet library that is used on every other Arduino will not work on the Yun (unless you add an Ethernet shield)

Of course, there isn't much point to adding any of those shields, since the Yun already has native functionality through the Linux processor. But the standard libraries won't work there, all of these cases need to go through the Yun Bridge library.

ShapeShifter:
For the Yun, the real crux of what is poorly documented is that SD card/file access, Ethernet connectivity, and WiFi connectivity is completely different on the Yun than on any other Arduino board:

  • The SD library that is used on every other Arduino will not work on the Yun (unless you add an SD card shield)
  • The WiFi library that is used on every other Arduino will not work on the Yun (unless you add a WiFi shield)
  • The Ethernet library that is used on every other Arduino will not work on the Yun (unless you add an Ethernet shield)

Of course, there isn't much point to adding any of those shields, since the Yun already has native functionality through the Linux processor. But the standard libraries won't work there, all of these cases need to go through the Yun Bridge library.

Thanks for pointing that out. I'll see what I can do.
Jesse

jessemonroy650:
Thanks for pointing that out. I’ll see what I can do.

There are several introductory information pages on the Yun: the product page, a hardware/getting started guide, and a playground page. It would probably be a good idea if one (or more) of those pages pointed out how those three functions are so drastically different from the traditional methods, and that new techniques are needed.

When you understand the architecture, and what device is connected to which processor, and what code has access to those devices, then it all makes sense. But these nuances are easily missed by a neophyte, so I agree it’s a good idea for the documentation to explicitly point it out. I think your efforts will be worthwhile, and they are much appreciated.