Motobiker
Member
Вот код целиком
Код:
struct Config
{
String str;
int val;
int val1;
int val2;
int val3;
int val4;
int val5;
int val6;
int val7;
int val8;
int val9;
int val10;
};
const char *filename = "/config.txt";
Config config;
config.str = "stroka";
config.val = 999;
config.val1 = 999;
config.val2 = 999;
config.val3 = 999;
config.val4 = 999;
config.val5 = 999;
config.val6 = 999;
config.val7 = 999;
config.val8 = 999;
config.val9 = 999;
File test_file = LittleFS.open(filename, "w+");
DynamicJsonDocument json_doc(2048);
json_doc["str"] = "11new stroka";
json_doc["val"] = 17771;
json_doc["val1"] = 17771;
json_doc["val2"] = 17771;
json_doc["val3"] = 1777;
json_doc["val4"] = 1777;
json_doc["val5"] = 1777;
json_doc["val6"] = 1777;
json_doc["val7"] = 1777;
json_doc["val8"] = 1777;
json_doc["val9"] = 1777;
json_doc["val10"] = 1777;
if (serializeJson(json_doc, test_file) == 0)
Serial.println(F("Failed to write to file"));
json_doc.clear();
test_file.close();
test_file = LittleFS.open(filename, "r");
DeserializationError error = deserializeJson(json_doc, test_file);
if (error)
Serial.println(F("Failed to read file, using default configuration"));
Serial.println(error.c_str());
config.str = json_doc["str"].as<String>();
config.val = json_doc["val"];
config.val1 = json_doc["val1"];
config.val2 = json_doc["val2"];
config.val3 = json_doc["val3"];
config.val4 = json_doc["val4"];
config.val5 = json_doc["val5"];
config.val6 = json_doc["val6"];
config.val7 = json_doc["val7"];
config.val8 = json_doc["val8"];
config.val9 = json_doc["val9"];
config.val10 = json_doc["val10"];
test_file.close();
json_doc.clear();
Serial.println(config.str);
Serial.println(config.val);
Serial.println(config.val1);
Serial.println(config.val2);
Serial.println(config.val3);
Serial.println(config.val4);
Serial.println(config.val5);
Serial.println(config.val6);
Serial.println(config.val7);
Serial.println(config.val8);
Serial.println(config.val9);
Serial.println(config.val10);