I am developing program with ESP32 WIFI microcontroller where I need to scan the available nearby WiFi networks and store them in a list so I can use them later outside the function. I have initially managed to do everything easily using String type variable, but I hear a lot of bad words about using String in Arduino so I am now trying to do everything with char* which I find a little bit more challenging than String.
I have found an article which describes how to convert String to char array: https://circuits4you.com/2018/03/08/arduino-convert-string-to-character-array/
// Define
String str = "This is my string";
// Length (with one extra character for the null terminator)
int str_len = str.length() + 1;
// Prepare the character array (the buffer)
char char_array[str_len];
// Copy it over
str.toCharArray(char_array, str_len);
In my code: I initialise a global variable char array where I want to store my information.
char* list_strings[10];
Then I call function to scan networks:
int scan_wifi_networks(){
Serial.println("scan start");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
int str_len = WiFi.SSID(i).length() + 1; // this is to know the the length of the string +1
char temp_buffer[str_len]; // create a temporary storage to copy the string to char array
WiFi.SSID(i).toCharArray(temp_buffer, str_len); // converts string to char array
Serial.print("temp buffer is set to=");
Serial.println(temp_buffer);
list_strings[i] = temp_buffer;
}
}
Serial.print("list of strings inside scanning function");
for(int i=0 ; i<=n ; i++){
Serial.println(list_strings[i]); // prints a lot of garbage??
}
return n;
}
The function above will scan networks and is suppose to convert the String variable (WiFi.SSID) to char array which I then store into my list:
list_strings[i] = temp_buffer;
My serial monitor : serial monitor image
It is able to print out the wifi names properly in for loop, but as soon as it gets out of for loop, it is printing some garbage.
Appreciate any help
UPDATE1
I am trying to print the address of temp_buffer:
char temp_buffer[str_len]; // create a temporary storage to copy the string to char array
int ptr = &temp_buffer;
Serial.print("Pointer :" );
Serial.println(ptr);
But with no luck. I am getitng a invalid conversion from char* to int error
UPDATE2
I have cast my temp_buffer to int and that compiles, however, I do not know if that is correct or not.
char temp_buffer[str_len]; // create a temporary storage to copy the string to char array
int ptr = (int)&temp_buffer;
Serial.print("Pointer :" );
Serial.println(ptr);
The serial monitor:
Pointer :1073422080
temp buffer is set to=Telia-33F8A3-Greitas
Pointer :1073422080
temp buffer is set to=HUAWEI-STOLYARCHUK
Pointer :1073422080
temp buffer is set to=Telia-E619A5-Greitas
Pointer :1073422080
temp buffer is set to=Telia-ED7E29-Greitas
Pointer :1073422096
temp buffer is set to=MEZON_1156F3
Pointer :1073422080
temp buffer is set to=Carolyne’s iPhone
Pointer :1073422080
temp buffer is set to=Teo-E6A379-Greitasis
Pointer :1073422096
temp buffer is set to=HH40V_BE2F
Pointer :1073422096
temp buffer is set to=Fishman
Pointer :1073422096
temp buffer is set to=MEZON_9EE8
Pointer :1073422096
temp buffer is set to=4G-Gateway-4981
Pointer :1073422080
temp buffer is set to=HUAWEI-B525-C466
Pointer :1073422096
temp buffer is set to=DGD
Pointer :1073422096
temp buffer is set to=4G-Gateway-D29C
Pointer :1073422096
temp buffer is set to=WIFI 2021
Pointer :1073422080
temp buffer is set to=Telia-E79C95-Greitas
Pointer :1073422080
temp buffer is set to=Telia-E5E1B9-Greitas
Pointer :1073422096
temp buffer is set to=#Telia-1E6E7A
list of strings inside scanning function
For some weird reason, the pointer changing from 1073422080 to 1073422096 every now and then
char* list_strings[10];char* list_strings[10];can't hold any strings. It can only hold pointers to strings.