Text data from SD card

Hi there,

I am storing GPS data on a micro SD card. I am able to write the data and then read it again without any issues (both as .csv and .txt file types). However, I want to be able to send this data periodically (say, once a day). How can I text this data using AT commands? I believe the easiest way to do this may be to read the data and store it as a variable and then insert the variable into the AT command. What is the best way to do this, I am not able to store it as a string and and int only reads the first character. Bear in mind that the data contains a variety of numbers and letters.

Many thanks,
Macgregor

Why can't you read the data from the SD card as a string? What have you tried? Without posting any code, only you can solve your problem.

#include <SPI.h>
#include <SD.h>
File myFile;

String gpsString, gpsString2;

void setup() {

  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.print("Initializing SD card...");
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  gpsString = "";
  gpsString = "lat, -42.173926, lon, 173.364926";

  myFile = SD.open("test.csv", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.csv...");
    myFile.println("This is a test file :)");
    myFile.println(gpsString);
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.csv");
  }
  myFile = SD.open("test.csv");
  if (myFile) {
  Serial.println("test.csv: ");
    while (myFile.available()) {
//      Serial.write(myFile.read());
      gpsString2 = (myFile.read());
      Serial.println(gpsString2);
      
    }
    myFile.close();
  } else {
    Serial.println("error opening test.csv");

  }

}

void loop() {
  // nothing happens after setup
}

Hi,
Sorry for not posting any code I didn't have it on me at the time.

I'm not sure why it didn't work before but I can now read it and store is as a string (it was telling me the conversion was ambiguous) . However, it now displays the data as numbers representing the string rather than the actual data (as pasted below). How can I store it as a human interpretable string or at least display/send it that way?

12:39:35.776 -> Initializing SD card...initialization done.
12:39:35.822 -> Writing to test.csv...done.
12:39:35.822 -> test.csv: 
12:39:35.822 -> 84
12:39:35.822 -> 104
12:39:35.822 -> 105
12:39:35.822 -> 115
12:39:35.822 -> 32
12:39:35.822 -> 105
12:39:35.822 -> 115
12:39:35.822 -> 32
12:39:35.822 -> 97
12:39:35.822 -> 32

etc...

Many thanks,
Macgregor

That code did not produce that output. It looks like the output was produced when this line

      //      Serial.write(myFile.read());

was not commented out since your output matches "This is a "

There is a difference between write() and print(). Check out the documentation Arduino Reference - Arduino Reference

The code did produce that output.

      gpsString2 = (myFile.read());[color=#222222][/color]
      Serial.println(gpsString2);

I store the data as a string and then print the string into the serial monitor. I have tried Serial.write() and Serial.print() and in this case they both give me the same result. Is there some way I can store the data in the string as human readable characters?

myFile.read() reads one byte

My bad. At first glance, I thought you were writing the contents of gpsString to the file for testing ("lat, -42.173926, lon, 173.364926") but you are not, you are actually writing "This is a test"

Go get the string back, you have to read, one character at a time until you find a newline '\n' and concatenate them together

int c = myFile.read();
while ( c != '\n' ) {
  gpsString2 += c;
  c = myFile.read();
}

If there are multiple lines, etc, you will have to check for when you hit the end of the file as well. You should also preallocate some space for gpsString2 so it doesn't fragment memory (or better yet, don't use the String class)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.