Hi!
I need to tell i'm not too professional in c, now learning, i readed books, and mutch of forums, and i have a question. And my english is poor.
I try to create a function to configuration save and reload program parameters. The configuration setted up a web page and the saved to SPIFFS. It's working. But i'm hit the wall in restore config.
the configuration parameters (now) has all in separate files.
I'm read config files with a for cycle, and i like'd to set variables in this for cycle. So, like this (caricature):
for (int i = 0; i < configparms; i++){
file open(configparams[i]);
tempvariable = file read;
variablearray[i] = tempvariable;
file close(configparams[i]);
}
In this config files need to kept wifi ssid password, runtime parameters, saved status numbers, actual controlled devices status etc. Some devices wait for weeks or months to activate, i need to save counters if power outage or someting similar happened. The setup is come from a webpage (asyncwebserver) and stored to SPIFFS And i would like it to be easily expandable
i know maybe has an easier mode to this do work this. But now io need to resolve this. If anybody send an easier mode, thank s but if possible now i would like to try to done it like this. I post to save method (please not hate me, i learning the language, so if this terrible, sorry)
server.on("/savecfg", HTTP_POST, [](AsyncWebServerRequest *request){
if(!request->authenticate(http_username, http_password))
{
return request->requestAuthentication();
}
logger("for ciklus indul...",1,0);
for (int i = 0; i < confparms; i++){
logger("form tartalom ellenőrzés lekérés: " + configparams[i],1,0);
if (request->hasParam(configparams[i],true))
{
logger(configparams[i] + " mező parameter lekérése",1,0);
String tempstr;
tempstr = request->getParam(configparams[i], true)->value();
logger("parameter lekérés vége " + configparams[i],1,0);
logger("Kapott paraméter: " + tempstr,1,0);
succ = true;
String savePath = "/config_"+configparams[i] +".conf";
logger("Megpróbálom menteni a " + savePath + " fájlba a következő adatot: " + tempstr,1,0);
if(SPIFFS.exists(savePath)){
logger("A fájl " + savePath + "létezik, törlöm...",0,0);
if(SPIFFS.remove(savePath)){
logger("A fájl " + savePath + "törlése sikeres",0,0);
} else {
logger("A fájl " + savePath + "törlése sikertelen",0,0);
}
}
File file = SPIFFS.open(savePath, FILE_WRITE);
if (!file) {
logger("Hiba a " +savePath + " irásra való megnyításakor...",0,0);
return;
}
if (file.print(tempstr)) {
logger("File was written: " + savePath,1,0);
} else {
logger("File write failed: " + savePath,0,0);
}
file.close();
} else {
succ = false;
logger("Gond van a getparammal: " + String(i) + " " + configparams[i],0,0);
}
savePath = "/config_bagfill.conf";
String tempstr ="";
if(SPIFFS.exists(savePath)){
File file = SPIFFS.open(savePath, FILE_READ);
if(!file){
logger("Nem tudom megnyitni a " + savePath + "fájlt olvasásra",0,0);
} else {
while (file.available()){
tempstr = tempstr + file.read();
}
}
bagfilltime = tempstr.toInt();
file.close();
}
savePath = bagfillfile;
if(SPIFFS.exists(savePath)){
File file = SPIFFS.open(savePath, FILE_WRITE);
if(!file){
logger("Nem tudom megnyitni a " + savePath + "fájlt írásra",0,0);
} else {
hourstobagfill = bagfilltime * 7 * 24;
file.print(hourstobagfill);
}
bagfilltime = tempstr.toInt();
file.close();
}
}
request->send(200,"text/plain","Got it!");
});
}
the variables:
char ssid[] = "Soimessid"; // CHANGE IT
char password[] = "somepassword"; // CHANGE IT
char http_username[] = "xxxxxx";
char http_password[] = "zzzzzzz";
char param_delete_path[] = "delete_path";
char param_edit_path[] = "edit_path";
char param_edit_textarea[] = "edit_textarea";
char param_save_path[] = "save_path";
const char *NTPserver = "hu.pool.ntp.org";
String allowedExtensionsForEdit = "txt, h, htm, html, css, cpp, js";
String logmsg = "";
String logfile = "/syslog.txt";
const char* jquery = "/jquery.min.js";
String wifi_mode = ""; //wifi mode AP or client
String filesDropdownOptions = "";
String textareaContent = "";
String savePath = "";
String savePathInput = "";
String status_wifi = "";
String status_ip = "";
String status_boardtemp = ""; //alapl hőmérséklet
String smtpserver ="mail.example.com"; //smtp server address
String smtpuser ="user@example.com"; //smtp user address
String smtppass ="password"; //smtp password
String smtpport ="578";
String recipientaddress ="";
String smtpfrom = "user@example.com";
String messagebody = "";
String host = "SZTCont";
bool enableTLS = false; //tls enabled
int rtcupdate = 0;
int onAPmode = 0;
int APreboot = 600;
//startup beállítás
bool rtcisok = false;
bool wifiOK = false;
int startedtime = 0;
int lastntptry = 0;
bool apreboot = true;
//Ledek
const int workledpin = 13;
const int relaypin1 = 33; //27
const int relaypin2 = 14; //14
const int relaypin3 = 12; //12
const int currpin1 = 4;
const int currpin2 = 2;
const int currpin3 = 15;
const int dspin = 26;
int runtime = 5; //Compressor ontime minutes
int sleeptime = 5; //Compressol halt time minutes
int bagfilltime = 2; //Extraction BAG fill time weeks
int pumplasttime = 0;
int hourstobagfill = 0;
String bagfillfile = "/hourstobagfill.txt";
bool rebooting = false;
String configparams[15]
= {
"SSID",
"wifipwd",
"sysname",
"smtpserv",
"smtpusr",
"smtppwd",
"smtpport",
"tlsenabled",
"recipient1",
"recipient2",
"recipient3",
"recipient4",
"subj"
"comprun",
"compstop",
"bagfill"
};
The whole program is 8 files in memory (in arduino ide) and 9 on SPIFFS (+config files) but if need i'm post all here for help.
Thanks for any help.