Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32 - Overwriting config.json caused garbage input #1222

Closed
paytah232 opened this issue Mar 26, 2020 · 1 comment
Closed

ESP32 - Overwriting config.json caused garbage input #1222

paytah232 opened this issue Mar 26, 2020 · 1 comment
Labels
question v6 ArduinoJson 6

Comments

@paytah232
Copy link

Hi. To my knowledge my config.json works perfectly in my program. It is able to store and update/overwrite existing key/value pairs. However, I have noticed that upon overwriting or resaving the config.json file, it preappends a whole heap of garbled text and I'm not sure why. Here is initially what the config.json file is saved as in the program:
image

After resaving or overwriting:
image

Below is the snippet of my code that saves the config file:

// Read and Save Updated Config Parameters
strcpy(Server_Name, custom_Server_Name.getValue());
strcpy(LED_Strip_Num, custom_LED_Strip_Num.getValue());
if (shouldSaveConfig) {
Serial.println("Saving Config");
doc["Server_Name"] = Server_Name;
doc["LED_Strip_Num"] = LED_Strip_Num;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("Failed to open config.json to write to...");
}
serializeJson(doc, Serial);
serializeJson(doc, configFile);
configFile.close();
}

Hopefully someone here knows why this might be happening; incase it help i'm running an esp32 sparkfun thingy, 4MB flash, 921600 upload, 115200 baud, 80MHz. Here is how I initialise the document, where size = 1024:

if (!SPIFFS.exists("/config.json")) {
Serial.println("No Saved Configuration to Read");
} else {
Serial.println("Reading Saved Config!");
File configFile = SPIFFS.open("/config.json", "r");
// If opened and has contents, allocate an appropriately sized buffer and parse
if (configFile) {
Serial.println("Config Opened Successfully");
size_t size = configFile.size();
std::unique_ptr<char[]> buff(new char[size]);
configFile.readBytes(buff.get(), size);
DeserializationError JsonError = deserializeJson(doc, buff.get());
serializeJson(doc, Serial);
// If parse is successful, load parsed variables
if (JsonError) {
Serial.println("Failed to load config.json");
} else {
Serial.println("\nSuccessfully Loaded config.json!");
strcpy(Server_Name, doc["Server_Name"]);
strcpy(LED_Strip_Num, doc["LED_Strip_Num"]);
Serial.println("Loaded Info:");
Serial.print("Server_Name: ");
Serial.println(Server_Name);
Serial.print("LED_Strip_Num: ");
Serial.println(LED_Strip_Num);
}
}
}

@bblanchon
Copy link
Owner

Hi @paytah232,

Please see:

Best regards,
Benoit

@lock lock bot locked as resolved and limited conversation to collaborators May 5, 2020
@bblanchon bblanchon added the v6 ArduinoJson 6 label Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question v6 ArduinoJson 6
Projects
None yet
Development

No branches or pull requests

2 participants