I can't convert String into const char* at ESP32

This is just the code that I'm using to check out how i could make this happen..

#include <string.h>

String x = "Test";
const char* y = "Test";

void setup() {
  Serial.begin(115200);
  delay(10);
}

void loop() {
  if (x.c_str() == y) {
    Serial.println("1 c_str() working");
    Serial.println(x.c_str());
    Serial.println(y);
    delay(3000);
  } 
  else 
  {
    Serial.println("1 c_str() not working");
    Serial.println(x.c_str());
    Serial.println(y);
    delay(3000);
  }

  if ((const char *)x.c_str() == y) {
    Serial.println("2 c_str() working");
    Serial.println((const char *)x.c_str());
    Serial.println(y);
    delay(3000);
  } 
  else 
  {
    Serial.println("2 c_str() not working");
    Serial.println((const char *)x.c_str());
    Serial.println(y);
    delay(3000);
  }

  if ((const char *)x.c_str() == "Test") {
    Serial.println("3 c_str() working");
    delay(3000);
  } 
  else 
  {
    Serial.println("3 c_str() not working");
    delay(3000);
  }

  if (y == "Test") {
    Serial.println("4 working");
    delay(3000);
  } 
  else 
  {
    Serial.println("4 not working");
    delay(3000);
  }
}

The output that i'm getting is this..

1 c_str() not working
Test
Test

2 c_str() not working
Test
Test

3 c_str() not working

4 working

Any idea on how i could make this happen??
thank you in advnace.

You can't compare c-strings this way. Use the strcmp function

Welcome to the forum

What is it that you want to make happen ?

I'm trying to store at the EEPROM an SSID and PASSWORD value as strings and when i read them when i restart the esp put them at the WiFi.begin() function. When i hardcode them or pass them directly from const char* variables they work but when i retrieve a string from the eeprom and convert it into a const char* with the c_str() function it doesn't work.....

So show the code how do you storing it rather than how to compare...

Are you confusing Strings (uppercase S) and strings (lowercase s). They are not the same thing at all

Can you actually store Strings (objects created using the String library) in EEPROM ?

This is the code. The first time i get into the loop I type my password and hit enter.
Then i restart my ESP and even though this Serial.print(password.c_str()); correctly prints my password the WiFi.begin() ain't connecting.. When i hardcode my pass like this
Serial.begin("MY SSID","MY PASS") it works or when i assign the values directly to const char* variables and pass them into the function but it's not what i need for my project..
Thank you for your time guys.

#include <WiFi.h>
#include <EEPROM.h>
#include <string.h>

#define EEPROM_SIZE 512

const char* ssid;
String password = "wrong pass";
void setup()

{

  Serial.begin(115200);
  delay(10);
  EEPROM.begin(EEPROM_SIZE);
  Serial.println();
  WiFi.disconnect();


  password = EEPROM.readString(0);

  ssid = "pew - pew 2.4GHz";

  Serial.print(password.c_str());
  WiFi.begin(ssid, password.c_str());

  unsigned long currentMillis = millis();

  while (WiFi.status() != WL_CONNECTED && millis() - currentMillis < 15000) {
    delay(500);
    Serial.print(".");
  }

  Serial.println(WiFi.status());
  // Serial.println("WiFi connected");
  // Serial.println("IP address: ");
  // Serial.println(WiFi.localIP());
}

int value = 0;

void loop() {
  if (Serial.available() > 0) {
    password = Serial.readString();
    EEPROM.writeString(0, password);
    EEPROM.commit();
    Serial.print("saved to EEPROM");
  }
}

What does that function call do with a String object, as you have declared for password?

You really should avoid using Strings. They are never necessary, and cause serious problems with certain types of Arduinos.

At EEPROM.h
size_t writeString(int address, String value);

It stores a String at the eeprom and uses as much space as it needs.

Then you read it like that..

String readString(int address);

in my code

password = EEPROM.readString(0);

If Serial.print(password.c_str()) correctly prints your password - it means that you store and read pass in the EEPROM correctly.
Therefore the error is elsewhere.
Are you sure that spaces are allowed in the SSID?

Yes as i said when i hardcode the SSID and PASS i get no issue... The exact same values that i pass through the String.

Thanks, I see there are two prototypes for EEPROM.writeString, one for C-strings (zero terminated character arrays) and the other for Strings.

    size_t writeString(int address, const char* value);
    size_t writeString(int address, String value);

It is pointless to store a String.

So you tried to compare it after the reading from EEPROM?
Show your code

I'm sorry i don't understand the issue can you further explain please?

If you want to enter, save, recall and use a C-string (a zero-terminated character array), there is no point in using a String.

If you do, you bring along an entire class of unneeded baggage, and have to worry about which of many functions require one or the other.

This, from your code, declares a pointer to a C-string, not to a String.

const char* ssid;

This, from your code, declares a String object, which is not a C-string.

String password = "wrong pass";

If you want password to be a C-string, this is preferred:

const char password[] = "wrong pass";
1 Like

Daaaamn mate you are right i directly stored my pass into a string and then called it with the c_str() function and worked..
But do you have an alternative on how to store and read it in order to make it work?

Again thank you so much for your time guys

const char password[] = "wrong pass";
EEPROM.writeString(0,password);  //will store password as a C-string

To read a stored password, try something like this:

char stored_password[20] = {0}; //make sure this is long enough, # of characters + 1
EEPROM.readString(0, stored_password);
1 Like

You doesn't need a c_str() function, just read the pass from the EEPROM as string, not a String

1 Like

EEPROM on a ESP32 has been depreciated use ESP32 Save Data Permanently using Preferences Library | Random Nerd Tutorials instead.

1 Like

Guys i thank you so much for your replies they were most appreciated.
The problem was th readString() function causi it saved the input as a String class and not as a string (as @jremington said) so i had to read from the Serial input char by char. SO finally for anyone that might be interested this is the working code. Again thank you!!

#include <WiFi.h>
#include <Preferences.h>
#include <nvs_flash.h>

Preferences preferences;

char* ssid = "pew - pew 2.4GHz";
String password;
char readByte;

void setup()

{
  // Only when i need to erase NVS partition then comment again and reupload code
  // nvs_flash_erase(); 
  // nvs_flash_init();

  Serial.begin(115200);
  Serial.println();

  preferences.begin("credentials", false);

  // ssid = preferences.getString("ssid", "");
  password = preferences.getString("password", "");
  Serial.println("current pass");
  Serial.println(password);


  if (password.c_str() == "") {
    Serial.println("No values saved for ssid or password");
  } else {
    // Connect to Wi-Fi
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password.c_str());
    Serial.print("Connecting to WiFi ..");
    unsigned long currentMillis = millis();
    while (WiFi.status() != WL_CONNECTED && millis() - currentMillis < 15000) {
      Serial.print('.');
      delay(1000);
    }
    Serial.println(WiFi.localIP());
  }
  preferences.end();
  password = "";
}

void loop() {

  while (Serial.available() > 0) {
    readByte = Serial.read();
    
    if (readByte != '\n') {
      password += readByte;
    } else {
      Serial.println("le pass");
      preferences.begin("credentials", false);
      preferences.putString("password", password);
      password = preferences.getString("password", "");
      Serial.println(password);
      Serial.println("Network Credentials Saved using Preferences");
      preferences.end();
      ESP.restart();
    }
  }
}