-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Example request for WiFiNINA #2065
Comments
Hi @wittrup, I'm surprised you had to use buffers. Best regards, |
Yes. Reproduced what I did first: // ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
//
// This example shows how to store your project configuration in a file.
// It uses the SD library but can be easily modified for any other file-system.
//
// The file contains a JSON document with the following content:
// {
// "hostname": "examples.com",
// "port": 2731
// }
//
//
// https://arduinojson.org/v7/example/config/
#include <WiFiNINA.h>
#include <ArduinoJson.h>
// Our configuration structure.
struct Config {
char hostname[64];
int port;
};
const char* filename = "/fs/config.txt"; // File path
Config config; // <- global configuration object
// Loads the configuration from a file
void loadConfiguration(const char* filename, Config& config) {
// Open file for reading
WiFiStorageFile file = WiFiStorage.open(filename); // Open file for reading
// Allocate a temporary JsonDocument
JsonDocument doc;
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, file);
if (error)
Serial.println(F("Failed to read file, using default configuration"));
// Copy values from the JsonDocument to the Config
config.port = doc["port"] | 2731;
strlcpy(config.hostname, // <- destination
doc["hostname"] | "example.com", // <- source
sizeof(config.hostname)); // <- destination's capacity
// Close the file (Curiously, File's destructor doesn't close the file)
file.close();
}
// Saves the configuration to a file
void saveConfiguration(const char* filename, const Config& config) {
// Open file for writing
WiFiStorageFile file = WiFiStorage.open(filename); // Open file
if (file) {
file.erase(); // Erase existing file to overwrite
}
if (!file) {
Serial.println(F("Failed to create file"));
return;
}
// Allocate a temporary JsonDocument
JsonDocument doc;
// Set the values in the document
doc["hostname"] = config.hostname;
doc["port"] = config.port;
// Serialize JSON to file
if (serializeJson(doc, file) == 0) {
Serial.println(F("Failed to write to file"));
}
// Close the file
file.close();
}
// Prints the content of a file to the Serial
void printFile(const char* filename) {
// Open file for reading
WiFiStorageFile file = WiFiStorage.open(filename); // Open file for reading
if (!file) {
Serial.println(F("Failed to read file"));
return;
}
// Extract each characters by one by one
while (file.available()) {
Serial.print((char)file.read());
}
Serial.println();
// Close the file
file.close();
}
void setup() {
// Initialize serial port
Serial.begin(115200);
while ((!Serial) && (millis() < 3000)); // Wait until 3 seconds for Serial
while (!SD.begin(chipSelect)) {
Serial.println(F("Failed to initialize SD library"));
delay(1000);
}
// Should load default config if run for the first time
Serial.println(F("Loading configuration..."));
loadConfiguration(filename, config);
// Create configuration file
Serial.println(F("Saving configuration..."));
saveConfiguration(filename, config);
// Dump config file
Serial.println(F("Print config file..."));
printFile(filename);
} Verify generates the output:
|
I'm sorry, I didn't realize that |
Once |
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/
The text was updated successfully, but these errors were encountered: