How to manipulate IPAddress variables / convert to string.

Hi,

I'm having some major issues with the IPAddress variable.
What I want to be able to do is get the IP address and change the last octet to 255 for broadcasting.

I have tried String(WiFi.localIP()) but that gives me a number 117483712.
The use of sprintf was suggested, but I get an errors with that.
Either I'm stupid, or there's next to nothing on the web about this.

Does anyone know how to do this?
I'm at wits end and close to destroying my Arduino.

Please help.

Thanks,

Seb

2 Likes

What type of variable is IPAddress in your program ?

Why convert to string?

IPAddress broadCast = WiFi.localIP();
broadCast[3] = 255;
Serial.println(broadCast);
3 Likes

SurferTim:
Why convert to string?

IPAddress broadCast = WiFi.localIP();

broadCast[3] = 255;
Serial.println(broadCast);

SuferTim, I love you.
I didn't realise it was actually an array I could manipulate in that way.
My idea of a string was that I could use sprintf or similar on it, but that isn't needed now.

Thank you so much. <3

2 Likes

It's not really an array - it's a lot cleverer than that; it's a class which provides array dereference operators that let you treat the address like an array, and it also provides assignment and comparison operators which enable you to treat it like a primitive type, and methods to help you print it, and so on.

String IpAddress2String(const IPAddress& ipAddress)
{
  return String(ipAddress[0]) + String(".") +\
  String(ipAddress[1]) + String(".") +\
  String(ipAddress[2]) + String(".") +\
  String(ipAddress[3])  ; 
}
7 Likes

So late, but...

WiFi.localIP().toString()

12 Likes

For the future, if you don't know.

If you want to asign an IP Address to a IPAddress variable you need to call the function to do this.

Example:

IPAddress IP_I_WANT = "192.168.0.33"; -> Don't work !!! IPAddress type is not a String.

You need to call a function:

IPAddress IP_I_WANT = IPAddress(192, 168, 0, 33); --> THAT'S CORRECT !!

OR

IPAddress IP_I WANT;
IP_I_WANT = IPAddress(192, 168, 0, 33);

IT'S FINE TOO

2 Likes

Thanks jmonso, just what I needed.

1 Like

I believe what the OP was trying to do was convert the IPAddress variable to a c-string so it can be entered into a sprintf function?

c-strings typically consume less memory than C++ Strings (note the capical S) and do not cause memory fragmentation like Strings do.

If this was the case then where you had String(WiFi.localIP())

... rather use: WiFi.localIP().toString().c_str()

This has the advantage of being compatible with sprintf functions, these are old school I know but they have some advantages. For example, they allow you to format the entire debug message to one string, even with variables in the middle. eg:

char[100] buffer;
sprintf(buffer, "Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
Serial.println(buffer);

this will create a single string called buffer that contains something like:
... "Now listeneng at IP 192.168.1.201, UDP port 88"

Which is much easier when you want to log it to SD card, or send it via wifi... or both.

If you are using the latest Arduino IDE, they have now added a Serial.printf() command so can do it in one line without having to fragment your Serial.println() statements every time you insert a variable.

Here is a really good tutorial on the sprintf function.

Sorry if i went a little off topic at the end...

sadly toString() doesn't seem to be implemented in the IPAddress() for all wifi chipsets.

WiFi.localIP().toString() - this failed for WiFiNina

1 Like

Use this:

String toString(const IPAddress& address){
  return String() + address[0] + "." + address[1] + "." + address[2] + "." + address[3];
}
2 Likes

Use this:

Why would you invoke a useless call to the String constructor?

return String(address[0]) + "." + address[1] + "." + address[2] + "." + address[3];

If you (foolishly) just absolutely MUST have a String.

1 Like

While trying to print the IP Address to a string in a ESP8266 sketch I encountered the same error. The solution suggested by @michaelwardsystems was the proper one: WiFi.localIP().toString().c_str(). I also enjoyed the explanation given. Many thanks

Thank you, Paul Stoffregen! As usual you are spot on. I needed to convert to string because the display driver for the SSD1306 only handles strings.

Serial.println(WiFi.localIP());

String LocalIP = String() + WiFi.localIP()[0] + "." + WiFi.localIP()[1] + "." + WiFi.localIP()[2] + "." +
WiFi.localIP()[3];

Serial.println(LocalIP);

display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
display.drawString(0, 16, "Connected to:");
display.drawString(0, 35, LocalIP);
display.display();

20200102_102458R.jpg

1 Like

jmonso:
So late, but...

WiFi.localIP().toString()

it works with me,
Thanks

1 Like