What I want: a function to write WiFi new credentials in JSON list format. If the credentials does not exist, it will just add a new item to the JSON object and save, else if exist it will replace it (in case that the password changes). Also it should re-sort or re-arrange the credentials, giving more priority to the credentials comming as parameters to the function, where 0 is the lowest priority and 10 (or more depending of the limit that I set in a future) is the highest priority to connect to. I'm using NVS to store these parameters as JSON strings.
The problem: The function works well until it needs to re-sort an existing credential, for example:
Before adding a new password to the list, I have these values stored in the flash
Code: Select all
{
"0": {
"ssid": "random",
"password": "access1234"
},
"1": {
"ssid": "HELLO",
"password": "takemypassword"
}
}
Code: Select all
{
"0": {
"ssid": "random",
"password": "access1234"
},
"1": {
"ssid": "HELLO",
"password": "takemypassword"
},
"1": {
"ssid": "HELLO",
"password": "donttakeitplease"
}
}
The problem is in the sorting algorithm and between line 148-172, and the replacing credentials implementation ends at 191. After some deep debugging in the code, I think I found the source problem at line 156 where I try to get an specific object from creds_list_json within a for loop() to sort the old credentials in a new JSON object, and after that I'm adding the comming credentials to the highest position.
For some reason I think that I'm using wrong the cJSON_AddItemToObject() and cJSON_GetObjectItem() since it returns not only one credentials in the object, but two. and that's why when I try to add the comming credentials to the highest index number, it repeats. The expected JSON value should be:
Code: Select all
{
"0": {
"ssid": "random",
"password": "access1234"
},
"1": {
"ssid": "HELLO",
"password": "donttakeitplease"
}
}
Kind regards,
Antonio M.