NTPClient with multiple time servers

Hi,

in the past I was only read some entrys here and they helped me much to realize my little projects. Thank you all for that.

Now I have a special problem and found no solving for that.
I use a ESP32 to get the actual time from a time server pool.

WiFiUDP ntpUDP;               
NTPClient timeClient(ntpUDP, "de.pool.ntp.org");
.
.
.

So far that's working fine.

Now I had the idea to change the time server name in the running system.
To try if it's working I stored the Server name in a character array:

char *server = "de.pool.ntp.org";
NTPClient timeClient(ntpUDP, server);
.
.
.

This is working fine also.

Unfortunetly the server names are stored in strings. I tried the following code:

string timeServer = "de.pool.ntp.org";
char Time_Server[timeServer.length()+1];
timeServer.toCharArray(Time_Server, timeServer.length()+1);
char *server = Time_Server;
NTPClient timeClient(ntpUDP, server);
.
.
.

This is not working.
There is a different between

char *server = "de.pool.ntp.org";

and

char *server = Time_Server;

whereby Time_Server is a character array.

I know I have a comprehension gap in using pointers and I worked through some lessons and tried some other things in the last days but I couldn't find the solving.

Could somewhere explain me what's wrong?

Thank you very much.

Ralf

Try

string timeServer = "de.pool.ntp.org";
...
char const * const server = timeServer.c_str();
NTPClient timeClient(ntpUDP, server);
//or simply
NTPClient timeClient(ntpUDP, timeServer.c_str());

But why use string instead of a char array ?

Hi guix,

thank you very much.

Your following tipp was the solving:

NTPClient timeClient(ntpUDP, timeServer.c_str());

I get the server name via bluetooth and store it into the flash of the ESP32 (preference).
The handling of strings seems easier for me.

Thank you again, Ralf