#include "Preferences.h"
#include "Arduino_JSON.h"
//https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/api/preferences.html
//iN THE BEGINNING OF THE PROGRAM, GRAB VALUES CURRENTLY STORED IN FLASH, AND UPDATE THE DECLARED VALUES
///CueTime[18]
//MODE
//ADDRESS
//REMADDRESS
Preferences preferences;
double CueTime[18];
int mode = 0; //read flash
int address = 0; //read flash
String remaddress =0; //read flash might be int
JSONVar = LastRead;
JSONVar = CurrentVal;
void updateJSON() {
//if curent JSON =! last pulled value- update what is in flash
JSONVar jsonReadings;
jsonReadings["addr"] = address;
jsonReadings["maddr"] = remaddress;//will be address of master remote
jsonReadings["mode"] = mode; //Int 0=X 1=Y 2=Z
jsonReadings["CT1"] = CueTime[0];
jsonReadings["CT2"] = CueTime[1];
jsonReadings["CT3"] = CueTime[2];
jsonReadings["CT4"] = CueTime[3];
jsonReadings["CT5"] = CueTime[4];
jsonReadings["CT6"] = CueTime[5];
jsonReadings["CT7"] = CueTime[6];
jsonReadings["CT8"] = CueTime[7];
jsonReadings["CT9"] = CueTime[8];
jsonReadings["CT10"] = CueTime[9];
jsonReadings["CT11"] = CueTime[10];
jsonReadings["CT12"] = CueTime[11];
jsonReadings["CT13"] = CueTime[12];
jsonReadings["CT14"] = CueTime[13];
jsonReadings["CT15"] = CueTime[14];
jsonReadings["CT16"] = CueTime[15];
jsonReadings["CT17"] = CueTime[16];
jsonReadings["CT18"] = CueTime[17];
CurrentVal = JSOresN.stringify(jsonReadings);
}
void getsavedJSON(){
String Curval = preferences.getUInt("JSONsave", 0);
if(Curval = "0"){
break;
}
else{
Lastread = JSON.parse(Curval);
address = Lastread["addr"];
remaddress = Lastread["maddr"];
mode = Lastread["mode"];
CueTime[0] = Lastread["CT1"];
CueTime[1] = Lastread["CT2"];
CueTime[2] = Lastread["CT3"];
CueTime[3] = Lastread["CT4"];
CueTime[4] = Lastread["CT5"];
CueTime[5] = Lastread["CT6"];
CueTime[6] = Lastread["CT7"];
CueTime[7] = Lastread["CT8"];
CueTime[8] = Lastread["CT9"];
CueTime[9] = Lastread["CT10"];
CueTime[10] = Lastread["CT11"];
CueTime[11] = Lastread["CT12"];
CueTime[12] = Lastread["CT13"];
CueTime[13] = Lastread["CT14"];
CueTime[14] = Lastread["CT15"];
CueTime[15] = Lastread["CT16"];
CueTime[16] = Lastread["CT17"];
CueTime[17] = Lastread["CT18"];
}
}
void setup() {
Serial.begin(115200);
Serial.println();
// Open Preferences with my-app namespace. Each application module, library, etc
// has to use a namespace name to prevent key name collisions. We will open storage in
// RW-mode (second parameter has to be false).
// Note: Namespace name is limited to 15 chars.
preferences.begin("JSONsave", false);
//getsavedJSON();
// Remove all preferences under the opened namespace
//preferences.clear();
// Or remove the counter key only
//preferences.remove("counter");
// Get the counter value, if the key does not exist, return a default value of 0
// Note: Key name is limited to 15 chars.
String JSONdata = preferences.getUInt("JSONsave", 0);
// Increase counter by 1
// Print the counter to Serial Monitor
Serial.printf("current data Saved:", JSONdata);
// Store the counter to the Preferences
preferences.putUInt("JSONsave", JSONdata);
// Close the Preferences
preferences.end();
// Wait 10 seconds
Serial.println("Restarting in 10 seconds...");
delay(1000);
// Restart ESP
ESP.restart();
}
void loop() {
}