Closed as not planned
Description
I've merged together a working example of JsonConfigFile.ino and WiFiStorage.ino so that ArduinoJson can be used with Arduino Nano 33 IoT and others that use u-blox NINA-W102 (datasheet).
I'd like to have it improved, quality checked etc. and implemented as an example in ArduinoJson/examples/
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit Blanchon
// MIT License
//
// This example demonstrates storing project configuration in a file and interacting with NINA internal memory partition.
// APIs are modeled on SerialFlash library (not on SD) for faster operations and buffer avoidance.
//
// The file contains a JSON document structured as:
// {
// "hostname": "examples.com",
// "port": 2731
// }
//
// For more details, refer: https://arduinojson.org/v7/example/config/
#include <WiFiNINA.h>
#include <ArduinoJson.h>
// Configuration structure
struct Config {
char hostname[64];
int port;
};
const char* filename = "/fs/config.txt"; // File path
Config config; // Global configuration object
// Load configuration from file
void loadConfiguration(const char* filename, Config& config) {
WiFiStorageFile file = WiFiStorage.open(filename); // Open file
char file_buffer[1024]; // Buffer to read file content
int n = 0; // Counter for file buffer
if (file) {
file.seek(0);
while (file.available()) {
uint8_t buf[128];
int ret = file.read(buf, 128);
for (size_t i = 0; i < ret; i++) {
file_buffer[n] = buf[i];
n++;
}
}
}
// Deserialize JSON document
JsonDocument doc;
DeserializationError error = deserializeJson(doc, file_buffer);
if (error)
Serial.println(F("Failed to read file, using default configuration"));
// Copy values from JsonDocument to Config
config.port = doc["port"] | 2731; // Default port if not specified
strlcpy(config.hostname, doc["hostname"] | "example.com", sizeof(config.hostname)); // Default hostname if not specified
file.close(); // Close file
}
// Save configuration to file
void saveConfiguration(const char* filename, const Config& config) {
WiFiStorageFile file = WiFiStorage.open(filename); // Open file for writing
if (file) {
file.erase(); // Erase existing file to overwrite
}
// Create a JSON document
JsonDocument doc;
doc["hostname"] = config.hostname; // Set hostname
doc["port"] = config.port; // Set port
// Serialize JSON to string
String file_buffer;
if (serializeJson(doc, file_buffer) == 0) {
Serial.println(F("Failed to write to file_buffer"));
} else {
Serial.print(F("Write to file_buffer success: "));
Serial.println(file_buffer);
file.write(file_buffer.c_str(), file_buffer.length());
}
file.close(); // Close file
}
// Print file content to Serial
void printFile(const char* filename) {
WiFiStorageFile file = WiFiStorage.open(filename); // Open file for reading
if (!file) {
Serial.print("Failed to read file: ");
Serial.println(filename);
} else {
if (file) {
file.seek(0);
while (file.available()) {
uint8_t buf[128];
int ret = file.read(buf, 128);
Serial.write(buf, ret); // Print content
}
Serial.println("");
}
file.close(); // Close file
}
}
void setup() {
Serial.begin(115200);
while ((!Serial) && (millis() < 3000)); // Wait until 3 seconds for Serial
// Load default config if running for the first time
Serial.println(F("Loading configuration..."));
loadConfiguration(filename, config);
// Save configuration
Serial.println(F("Saving configuration..."));
saveConfiguration(filename, config);
// Print config file content
Serial.println(F("Print config file..."));
printFile(filename);
}
void loop() {
// Not used in this example
}