Having some issues on a Giga R1 Wifi and trying to create a KVStore to store the ssid and password and for some reasons when re-booting the board the password is being returned when I ask for the ssid. I have even so much as created just a small sample file for testing just this part of the functionality and it still has this issue. I am pulling my hair out on this one.
Sample Code
#include <KVStore.h>
#include <kvstore_global_api.h>
/**************************************/
// sample code from - https://forum.arduino.cc/t/persistent-storage/939250/9
const char* const KVS_ssid = "wifi_ssid"; /* wifi network Key*/
char wifi_ssid[] = ""; /* wifi network Value */
const char* const KVS_pass = "wifi_pass"; /* wifi_pass Key */
char wifi_pass[] = ""; /* wifi_pass Value */
/**************************************/
void serial_listen(){
if (Serial.available()){
String PCRCommand = "";
PCRCommand = Serial.readStringUntil('\n');
PCRCommand.trim(); // trim whitspace from end of string
if( PCRCommand.startsWith("set_ssid:") ){
Serial.print("set_ssid:");
PCRCommand.remove(0,9); // remove first 8 chars of string
//set_ssid:Netgear96
Serial.println(PCRCommand);
int str_len = PCRCommand.length();
strcpy(wifi_ssid,PCRCommand.c_str());
kv_set(KVS_ssid, wifi_ssid, str_len + 1, 0);
Serial.print("kv_get key: ");
Serial.println(KVS_ssid);
Serial.print("kv_get Value: ");
Serial.println(wifi_ssid);
Serial.println();
}else if( PCRCommand.startsWith("set_pass:") ){
//set_pass:curlyjungle455
Serial.print("set_pass:");
PCRCommand.remove(0,9); // remove first 8 chars of string 'set_pass:'
Serial.println(PCRCommand);
int str_len = PCRCommand.length();
strcpy(wifi_pass,PCRCommand.c_str());
kv_set(KVS_pass, wifi_pass, str_len + 1, 0);
Serial.print("kv_get key: ");
Serial.println(KVS_pass);
Serial.print("kv_get Value: ");
Serial.println(wifi_pass);
Serial.println();
//Serial.print("Send Formated DPRINT String:");
}else if( PCRCommand.equals("kvs") ){
kv_reset("/kv/");
Serial.println("KVStore Reset!!!");
}else if( PCRCommand.equals("reset") ){
load_settings();
}
}
}
/**************************************/
void load_settings(){
Serial.println("loading settings!!!");
kv_get(KVS_ssid, wifi_ssid, 32, 0);
Serial.print("kv_get key: ");
Serial.println(KVS_ssid);
Serial.print("kv_get Value: ");
Serial.println(wifi_ssid);
kv_get(KVS_pass, wifi_pass, 32, 0);
Serial.print("kv_get key: ");
Serial.println(KVS_pass);
Serial.print("kv_get key Value: ");
Serial.println(wifi_pass);
}
/**************************************/
void setup() {
Serial.begin(9600);
Serial.println("booting!!!");
while (!Serial); /* wait for serial port to be active */
/* Load all settings */
// onstartup load current settings
kv_get(KVS_ssid, wifi_ssid, 32, 0);
Serial.print("kv_get key: ");
Serial.println(KVS_ssid);
Serial.print("kv_get Value: ");
Serial.println(wifi_ssid);
kv_get(KVS_pass, wifi_pass, 32, 0);
Serial.print("kv_get key: ");
Serial.println(KVS_pass);
Serial.print("kv_get key Value: ");
Serial.println(wifi_pass);
if (*wifi_pass == '\0' || *wifi_ssid == '\0'){
Serial.println("Set WIFI Crendentials!!!!");
}else{
Serial.println("Connect to WIFI with these credentials");
Serial.print("SSID:");
Serial.println(wifi_ssid);
Serial.print("PASS:");
Serial.println(wifi_pass);
}
}
/**************************************/
void loop() {
// put your main code here, to run repeatedly:
serial_listen(); // listen on serial port
}
As you can see the variable names are different so its not a small typo at least that I can see. I have this sample setup in a way I can pass commands via the serial monitor
to set the ssid I pass "set_ssid:myssid"
to set the password I pass "set_pass:mypassword"
I can optionally reset the KVStore by sending "kvs" this will run the kv_reset("/kv/");
and I can pass "reset" and this should output the current values of the KVStore for the two variables.
the weird part is if I program the user and pass and run the "reset" which fires off the "load_settings()" function it shows the correct values. if I reboot the load settings returns the password for both variables.
Here is the serial out put from setting the variables the first time and as you can see everything looks correct after running "set_ssid:myssid", "set_pass:mypassword" and then "reset"
20:11:19.633 -> KVStore Reset!!!
20:11:26.997 -> set_ssid:myssid
20:11:26.997 -> kv_get key: wifi_ssid
20:11:26.997 -> kv_get Value: myssid
20:11:26.997 ->
20:11:37.278 -> set_pass:mypassword
20:11:37.278 -> kv_get key: wifi_pass
20:11:37.278 -> kv_get Value: mypassword
20:11:37.278 ->
20:11:53.313 -> loading settings!!!
20:11:53.313 -> kv_get key: wifi_ssid
20:11:53.313 -> kv_get Value: myssid
20:11:53.313 -> kv_get key: wifi_pass
20:11:53.313 -> kv_get key Value: mypassword
/-------------------------------------/
after re-booting I get this
20:01:46.027 -> kv_get key: wifi_ssid
20:01:46.027 -> kv_get Value: :myssid
20:01:46.027 -> kv_get key: wifi_pass
20:01:46.027 -> kv_get key Value: :mypassword
20:01:46.027 -> Connect to WIFI with these credentials
20:01:46.027 -> SSID:mypassword
20:01:46.027 -> PASS::mypassword
any idea what I am missing on this super simple setup?...
please keep in mind I am still a Arduino Newb and this is my first major project.
and another question in regards to the KV_Get() function, why does it require I send in a length, if I don't know the length of the string my user has input? is there a better way of executing the kv_get command??? some sort of reference variable or pointer I need to use?
kv_get(KVS_pass, wifi_pass, 32, 0);