char array gets overwritten

Hello all. I am trying it now for nearly 2 days, but cant find my mistake.

I try to get my ssid and pw via bluetooth. For this i wrote a function. But everytime i'll try to pass the data to the char array it gets the data of the next action:
Output:
ssid-buff:FRITZ!Box
ssid:22536373737373833628
password:122536373737373833628
ssid_aussen:22536373737373833628
password_aussen:122536373737373833628

I am using an ESP32. I tried different methodes but none of them worked out :confused:

//definition in main.cpp
char ssid[] = "";
char password[] = "";

//call in the void setup()
bl_get_WiFi(ssid,password,SerialBT);
Serial.print("ssid_aussen:"); Serial.println(ssid);
Serial.print("password_aussen:"); Serial.println(password);

//function itself
void bl_get_WiFi(char *ssid_bl, char *pw_bl,BluetoothSerial bt_handler){
  int c_ssid = 0; //1 = got ssid
  int c_pw = 0; //1 = got pw
  String bl_message = "";
  String ssid_buff = "";
  String pw_buff = "";
  while((c_ssid==0) || (c_pw==0)){
    if (SerialBT.available()){ //coming in in chars not as String!
      char incomingChar = SerialBT.read();
      if (incomingChar != '\n'){
        bl_message += String(incomingChar);
      }
      else{
        bl_message = "";
      }
    }
    delay(20);

    /*get ssid*/
    if(bl_message.endsWith(":ssid") && (c_ssid==0)){ // from end cause bl_serial receive evry char. So marked needs to be at the end
      bl_message.replace(":ssid",""); //delete maker
      ssid_buff = bl_message; // change to uniq var
      c_ssid = 1; // received ssid
      bl_message = "";
    }//end get ssid

  /*get pw*/
    if(bl_message.endsWith(":pw")&& (c_pw==0)){ // from end cause bl_serial receive evry char. So marked needs to be at the end
      bl_message.replace(":pw","");//delete maker
      pw_buff = bl_message; // change to uniq var
      c_pw = 1; //received pw
      bl_message = "";
    }//end pw
  }//end While
  // pw_buff.toCharArray(pw_bl,pw_buff.length()+1); //pointer to ssid_buff var
  // ssid_buff.toCharArray(ssid_bl,ssid_buff.length()+1); //pointer to ssid_buff var
  for(int i = 0;i<(ssid_buff.length()+1); i++)ssid_bl[i] = ssid_buff.charAt(i);
  for(int i = 0; i<(pw_buff.length()+1); i++)pw_bl[i] = pw_buff.charAt(i);
  
  Serial.print("ssid-buff:"); Serial.println(ssid_buff);
  Serial.print("ssid:"); Serial.println(ssid_bl);
  Serial.print("password:"); Serial.println(pw_bl);
}//end bl_get_WiFi
char ssid[] = "";
char password[] = "";

i'm not sure i fully understand your code, but the above statements suggest you are trying to allocate space to store strings. by specifying an array without a size (i.e. "[]"), the compiler sizes the array based on how it is initilaized. since you specify an empty string the compiler just allocates 1 byte for the null.

try specifying an array of appropriate size which is initialized to an empty string

char ssid[40] = "";
char password[40] = "";

@gcjr, you are completly right. It worked.
Thank you

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.