#include <Preferences.h>
#include <ArduinoJson.h>
#define LOAD_COUNT 8
static const byte loads[LOAD_COUNT] = {23, 22, 21, 19, 18, 5, 25, 26};
Preferences preferences;
void setup() {
Serial.begin(115200);
// Open Preferences with a namespace, in this case "myApp"
preferences.begin("Load_status", false);
// Create a JSON object
StaticJsonDocument<200> jsonDoc;
for (int i = 0; i < LOAD_COUNT; i++) {
jsonDoc["Load" + String(i + 1)] = digitalRead(loads[i]);
}
// Serialize JSON to a string
String jsonString;
serializeJson(jsonDoc, jsonString);
// Store the JSON string in preferences
preferences.putString("Load_status", jsonString);
// Retrieve the JSON string from preferences
String storedJsonString = preferences.getString("Load_status", "{}");
// Deserialize the JSON string back to a JSON object
StaticJsonDocument<200> retrievedDoc;
DeserializationError error = deserializeJson(retrievedDoc, storedJsonString);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
// Access the values from the JSON object
int load1 = retrievedDoc["Load1"];
int load2 = retrievedDoc["Load2"];
// Print the values
Serial.println("Retrieved JSON:");
Serial.print("Load1: ");
Serial.println(load1);
Serial.print("Load2: ");
Serial.println(load2);
// End the preferences
preferences.end();
}
void loop() {
// Your main code here
}