save some variables into sd card
-
Hi,
I would like to save three variables value on the sd.
boolean brgt
boolean unit
int x
and, on setup() read that values...
I'm trying with String, and I can save a string of my value in a file on sd..
but I don't know how to read the separate value to use it in the sketch// http://forum.m5stack.com/topic/184/problems-writing-to-sd-card
#include <M5Stack.h>//Micro SD / TF Card Test
void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
// Print blank line on screen
M5.Lcd.printf(" \n ");
M5.Lcd.printf(" \n ");Serial.printf("Listing directory: %s\n", dirname);
M5.Lcd.printf("Listing directory: %s\n", dirname);File root = fs.open(dirname);
if (!root) {
Serial.println("Failed to open directory");
M5.Lcd.println("Failed to open directory");
return;
}
if (!root.isDirectory()) {
Serial.println("Not a directory");
M5.Lcd.println("Not a directory");
return;
}File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
Serial.print(" DIR : ");
M5.Lcd.print(" DIR : ");
Serial.println(file.name());
M5.Lcd.println(file.name());
if (levels) {
listDir(fs, file.name(), levels - 1);
}
} else {
Serial.print(" FILE: ");
M5.Lcd.print(" FILE: ");
Serial.print(file.name());
M5.Lcd.print(file.name());
Serial.print(" SIZE: ");
M5.Lcd.print(" SIZE: ");
Serial.println(file.size());
M5.Lcd.println(file.size());
}
file = root.openNextFile();
}
}void readFile(fs::FS &fs, const char * path) {
Serial.printf("Reading file: %s\n", path);
M5.Lcd.printf("Reading file: %s\n", path);File file = fs.open(path);
if (!file) {
Serial.println("Failed to open file for reading");
M5.Lcd.println("Failed to open file for reading");
return;
}Serial.print("Read from file: ");
M5.Lcd.print("Read from file: ");
while (file.available()) {
int ch = file.read();
Serial.write(ch);
M5.Lcd.write(ch);
}
}void writeFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Writing file: %s\n", path);
M5.Lcd.printf("Writing file: %s\n", path);File file = fs.open(path, FILE_WRITE);
if (!file) {
Serial.println("Failed to open file for writing");
M5.Lcd.println("Failed to open file for writing");
return;
}
if (file.print(message)) {
Serial.println("File written");
M5.Lcd.println("File written");
} else {
Serial.println("Write failed");
M5.Lcd.println("Write failed");
}
}//////////////////////////////////////////////////////////////////////////////////////
bool suca = 1;
bool assai = 0;
int x = 1680;
//////////////////////////////////////////////////////////////////////////////////////// the setup routine runs once when M5Stack starts up
void setup() {
// initialize the M5Stack object
M5.begin();// M5.startupLogo();
Wire.begin();// Lcd display
M5.Lcd.setBrightness(100);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 10);
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setTextSize(2);// Page Header
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 05);
M5.Lcd.printf("Testing SD Card Functions:\r\n");//////////////////////////////////////////////////////////////////////////////////////
store = String(brgt) + "," + String(unit) + "," + String(x) + "\r\n";
//////////////////////////////////////////////////////////////////////////////////////
// writeFile(SD, "/hello.txt", "Hello world from M5Stack !!");
writeFile(SD, "/hello.txt", store.c_str()); // questa funziona!!!!M5.Lcd.printf("");
// Print blank line on screen
M5.Lcd.printf(" \n ");// Print blank line on screen
M5.Lcd.printf(" \n ");readFile(SD, "/hello.txt");
// store2[3] = (readFile(SD, "/hello.txt"));
}void loop() {
// put your main code here, to run repeatedly:
M5.update();
}is it correct to use String?
is it the best way to save/read my variables?in the past, with esp8266, I saved my three variables on EEPROM but since M5Stack have sd card I would like to have infinite cycle of read/write