Convert WiFi.localIP() to String

Hi folks,
My project sends push notifications using Slack and Pushover. Part of the message needs to include the IP Address that was assigned to the Arduino. The problem is that you need to include the length() of the message in the message header. Obviously the length is going to vary depending on the assigned IP. Is there a way to convert the WiFi.localIP() to a String, or otherwise count the number of characters? Thanks people.

Almost as you said it :wink:

This would be a String

WiFi.localIP().toString()

So you can then call length etc

1 Like
String localip = WiFi.localIP().toString();

Yields "error: 'class arduino::IPAddress' has no member named 'toString'; did you mean 'fromString'?"

1 Like

It’s in the .h

Which type of board are you using ?

1 Like

MKR WiFi 1010.
I have to confess that I have no idea what to do with the code you posted :grimacing: I'm still pretty new at this. Thanks again.

The code I posted is the source code of the IPAddress header class for an ESP8286 and shows the toString() function exists.

It seems Arduino doesn’t have it for the SAMD core. So you have to build it yourself.
Do you want it in Hexadecimal or Decimal ?
Do you want a String or a char buffer?

OP wanted to know the size of that representation

so I assumed he wanted some sort of textual representation so that he can get the length.

he could print to his stream and capture the value returned by print() to know the size, If that's an option. otherwise it's a couple lines to build a String or c-string...

The length should be in decimal and it would need to be a String().

message = "the IP Address is: " + localip;
length = message.length();

with my StreamLib (available in Library Manager)

  char buff[50];
  CStringBuilder message(buff, sizeof(buff));
  message.print(F("the IP Address is: "));
  message.println(ipAddress);
  int length = message.length();

or without any library you can just build it yourself

IPAddress address(100, 20, 3, 255);

String toString(IPAddress& ip) { // IP v4 only
  String ips;
  ips.reserve(16);
  ips = ip[0];  ips += ':';
  ips += ip[1]; ips += ':';
  ips += ip[2]; ips += ':';
  ips += ip[3];
  return ips;
}

void setup() {
  Serial.begin(115200);
  String message = "Your IP address is ";
  message += toString(address);
  Serial.println(message);
}

void loop() {}

This produces no results for me, either in your original form or by substituting WiFi.localIP() for the address. As a matter of fact, it seems to disable Serial.print().

IPAddress address(100, 20, 3, 255);

String toString(IPAddress& ip) { // IP v4 only
  String ips;
  ips.reserve(16);
  ips = ip[0];  ips += ':';
  ips += ip[1]; ips += ':';
  ips += ip[2]; ips += ':';
  ips += ip[3];
  return ips;
}

void setup() {
  Serial.begin(115200);
  String message = "Your IP address is ";
  message += toString(address);
  Serial.println(message);
}

void loop() {}

can you post your test code? did you open the Serial monitor at 115200 bauds?

actually you are just missing the

  while (!Serial);

after

  Serial.begin(115200);

if you don't do that you send stuff to the serial port but the USB connection has not been initialized yet

IPAddress address(100, 20, 3, 255);

String toString(IPAddress& ip) { // IP v4 only
  String ips;
  ips.reserve(16);
  ips = ip[0];  ips += ':';
  ips += ip[1]; ips += ':';
  ips += ip[2]; ips += ':';
  ips += ip[3];
  return ips;
}

void setup() {
  Serial.begin(115200);  
  while (!Serial);
  String message = "Your IP address is ";
  message += toString(address);
  Serial.println(message);
}

void loop() {}

or you can use snprintf function

char buff[50];
int length = snprintf(buff, sizeof(buff), "Your IP address is: %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);

yes, I did it with a String because that was the ask

I prefer the c-string :slight_smile:

I installed the StreamLib but I don't understand where WiFi.localIP() fits into this. I tried:

IPAddress ipAddress(WiFi.localIP());
and
IPAddress ipAddress = WiFi.localIP();

But the only thing that works is:

IPAddress ipAddress(192, 168, 0, 1);

Did you check post 14?

Did you check post 14?

Yea, I didn't notice that was missing from your code when I copied it. It's moot now since I went with Juraj's suggestion to use the CStringBuider function. Thanks.

OK - what matters is that you got a solution

have fun

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