1

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

10
  • Where do you want to store the string? Commented Apr 15, 2021 at 9:36
  • I want to store the information in the global variable that I create : char* list_strings[10]; Commented Apr 15, 2021 at 9:39
  • that's just an array of pointers though. It only stores pointers to other variables. Which other variable should hold the string? Commented Apr 15, 2021 at 9:49
  • Sorry I am not properly understanding what you mean :( Are you asking about the temporary variable that I store the string before I copy it into my array? Commented Apr 15, 2021 at 9:54
  • char* list_strings[10]; can't hold any strings. It can only hold pointers to strings. Commented Apr 15, 2021 at 10:47

1 Answer 1

0

The problem is char temp_buffer[str_len];. This allocates memory on the stack (almost certainly the same bit of memory on each iteration of the for loop. I'm surprised this compiled as I would expect to get an error complaining that str_len is not a constant. In any case you are saving the address of an area of stack that will be available for re-use on each iteration and also after exiting the loop. It might be informative to print the address of temp_buffer on each iteration.

You could instead allocate the memory on the heap with new() and remember to free it with delete() when finished.

Even better could be to use strings and vectors from the STL.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the answer. Unfortunately, printing the address of tempo_buffer is not as straightforward on Arduino IDE as I would want it to be. I cannot get it to work. Do you have any ideas how can I print the address of my temp_buffer? I will look into malloc as I do not have experience with it. And also, I have never heard about STL
I have updated my initial question UPDATE1. I posted some code there. I do not know how to post code in comment.
The point is that you are copying the strings to temp_buffer which is a local variable addressing an area on the stack that will go out of scope at the end of each iteration of the loop. You are overwriting much the same area of stack on each iteration and then saving that address to list_strings[]. By the time you get to look at list_strings [] that bit of stack is probably being used for something else. Even if it hadn't yet been re-used it would only contain the last thing you wrote to it. I think you need to do some reading on memory management in C/C++
Ignore reference to malloc(), I should have said new()
Okay I see, but how is this problem then solved using strcpy instead of = ? How is this different?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.