Skip to main content
added 461 characters in body
Source Link
Juraj
  • 18.3k
  • 4
  • 32
  • 50

You can write a struct to file as binary data. This example is from my project.

global data:

struct Stats {
  int heatingTime; // minutes
  int consumedPower; // watts
};

Stats statsData;

snippet from setup():

  File file = SPIFFS.open(STATS_FILENAME, "r");
  if (file) {
    file.readBytes((byte*) &statsData, sizeof(statsData));
    file.close();
  }

snippet from statsSave() function:

  File file = SPIFFS.open(STATS_FILENAME, "w");
  if (file) {
    file.write((byte*) &statsData, sizeof(statsData));
    file.close();
  }

your updated readSPIFFS should be:

RGBLA readSPIFFS() {

  RGBLA returnedVars;
  File configFile = SPIFFS.open("/config.json", "r");
  if (configFile ) {
    Serial.println("YAY!");
    configFile.readBytes((byte*) &returnedValues, sizeof(RGBLA));
    configFile.close();
  } else {
    Serial.println("Boo!");
  }
  return returnedValues;
}

You can write a struct to file as binary data. This example is from my project.

global data:

struct Stats {
  int heatingTime; // minutes
  int consumedPower; // watts
};

Stats statsData;

snippet from setup():

  File file = SPIFFS.open(STATS_FILENAME, "r");
  if (file) {
    file.readBytes((byte*) &statsData, sizeof(statsData));
    file.close();
  }

snippet from statsSave() function:

  File file = SPIFFS.open(STATS_FILENAME, "w");
  if (file) {
    file.write((byte*) &statsData, sizeof(statsData));
    file.close();
  }

You can write a struct to file as binary data. This example is from my project.

global data:

struct Stats {
  int heatingTime; // minutes
  int consumedPower; // watts
};

Stats statsData;

snippet from setup():

  File file = SPIFFS.open(STATS_FILENAME, "r");
  if (file) {
    file.readBytes((byte*) &statsData, sizeof(statsData));
    file.close();
  }

snippet from statsSave() function:

  File file = SPIFFS.open(STATS_FILENAME, "w");
  if (file) {
    file.write((byte*) &statsData, sizeof(statsData));
    file.close();
  }

your updated readSPIFFS should be:

RGBLA readSPIFFS() {

  RGBLA returnedVars;
  File configFile = SPIFFS.open("/config.json", "r");
  if (configFile ) {
    Serial.println("YAY!");
    configFile.readBytes((byte*) &returnedValues, sizeof(RGBLA));
    configFile.close();
  } else {
    Serial.println("Boo!");
  }
  return returnedValues;
}
Source Link
Juraj
  • 18.3k
  • 4
  • 32
  • 50

You can write a struct to file as binary data. This example is from my project.

global data:

struct Stats {
  int heatingTime; // minutes
  int consumedPower; // watts
};

Stats statsData;

snippet from setup():

  File file = SPIFFS.open(STATS_FILENAME, "r");
  if (file) {
    file.readBytes((byte*) &statsData, sizeof(statsData));
    file.close();
  }

snippet from statsSave() function:

  File file = SPIFFS.open(STATS_FILENAME, "w");
  if (file) {
    file.write((byte*) &statsData, sizeof(statsData));
    file.close();
  }