Hi guys, I'm having issue reading some data stored into an sd and parse into an array of values.
The array are the following
ImpostazioneInt arraySDInt []
{
{ "timeoutTimerMax", timeoutTimerMax },
{ "timeoutDistance", timeoutDistance },
{ "GreenZoneCirc", GreenZoneCirc},
{ "exitTime_delay", exitTime_delay },
{ "TimeZone", TimeZone },
};
ImpostazioneBool arraySDBool []
{
{ "timeout", timeout },
{ "Daylight Savings Time", daylightSavingsTime }
};
I couldn't find a way to store both bools and long value so i created 2 struct:
struct ImpostazioneInt
{
String NomeImpostazione;
unsigned long Valore;
};
struct ImpostazioneBool
{
String NomeImpostazione;
bool Valore;
};
Now what i want from the following function is to scan tha settings file, store each row into a string, split it using strtok and parse it using as key value the first token (impostazione wich in italian means setting) scan trough the array, find the right setting and replace the second token which is the value (valore). As soon it find the right key it reads another row and so on.
This is the function:
void syncSDSettings()
{
Serial.println("Starting settings sync.");
File impostazioni = SD.open("/Impostazioni.txt", FILE_READ);
String riga;
size_t n;
char buff[128];
if(!impostazioni) //se il file impostazioni viene letto procedi
{
Serial.println("ERROR, unable to read settings.txt");
}
else
{
while (impostazioni.available()) //esegui il ciclo finche le righe del file non esauriscono
{
riga = impostazioni.readStringUntil('\n'); //legge da impostazioni una stringa fino al capo
char* rigaChar;
riga.toCharArray(rigaChar,128);
Serial.println("Analyzing row: ");
Serial.println(riga);
Serial.println();
Serial.println("Tokenizing");
char *impostazione = strtok (rigaChar," = "); //associa la prima parte della riga all'impostazione
char *valore = strtok(NULL,";"); //associa la seconda parte al valore
Serial.print("Value tokenized: ");
Serial.print(impostazione);
Serial.print(" : ");
Serial.print(valore);
Serial.println();
if (valore=="TRUE" || valore =="FALSE") //se il valore è un buleano associalo a ImpostazioneBool
{
Serial.println("Boolean value found");
for (int i = 0; i < sizeof(arraySDBool)/sizeof(ImpostazioneBool); i++) //scansiona ogni settaggio di ImpostazioneBool
{
Serial.println("Checking occurance");
if (strcmp(impostazione, stringToChar128(arraySDInt[i].NomeImpostazione))) //se l'etichetta corrisponde
{
Serial.println("Occurance found");
if (strcmp(valore, "TRUE"))
{
arraySDBool[i].Valore = true; //associa a ImpostazioneBool il valore letto
}
else
{
arraySDBool[i].Valore = false;
}
Serial.println("Value updated");
}
}
}
else //avvia associazione di valori int
{
Serial.println("Int value found");
for (int i = 0; i < sizeof(arraySDInt)/sizeof(ImpostazioneInt); i++) //scansiona ogni settaggio di ImpostazioneInt
{
Serial.println("Controllo corrispondenza etichetta");
if (strcmp(impostazione, stringToChar128(arraySDInt[i].NomeImpostazione))) //se l'etichetta corrisponde
{
Serial.println("Corrispondenza trovata");
arraySDInt[i].Valore = atoi(valore);
Serial.println("Valore aggiornato");
}
}
}
}
closeFile(impostazioni);
}
}
This is what happens:
- I have added some monitor outs to check if strtok is working but it's not, the values are both empty
- the inner while works 1 time and then i have a runtime error
I know, i'm using String instead of c string, it seemed easier to me but it's still not working and i have tried tons of stuff to fix it but i could not make it work properly.
Do you have any idea?
P.S. I'm working with an esp32