Is there a Network IP "print()" function

I’d like to get the IP address as a string. Is there a print() function that would return the output of “Serial.print(myIPAddress)” as a string?

See this:

http://docs.spark.io/firmware/#connection-management-network

1 Like

Seems like @kennethlimcp provided you with an answer.
I would only like to ask you to reconsider your topic title, since it’s now somewhat confusing. Something like “how to retrieve IPadress as string?” might be more accurate.

Thanks!

1 Like

Sorry about the confusion. I’ll try the Network function out. Do these function return strings? I got an IPAddress object, I don’t know how to work with that.

Thanks @Moors7!

I have edited the title :slight_smile:

@ronm, I think it’s a string

Here the code:
String ip = Network.localIP() ;

here’s the compile error:
error: conversion from ‘IPAddress’ to non-scalar type ‘String’ requested

Is there a list of methods for class IPAddress?

I tried assigning it to a char * and an int. I got similar errors.

It’s funny, I just happen to have exactly what you need right here in an example that demonstrates saving 20KB of flash memory:

It would also seem that if you can’t do this Serial.println(Network.localIP()); then the Docs/Firmware has a discrepancy.

Hi @ronm

So IPAddress which is the return type for Network.localIP() has a printTo method and it works great for me. So Serial.println(Network.localIP()); works great. And the itoa() trick is a good one to know. But if you still want an Arduino String, read on.

    IPAddress myIP = Network.localIP();
    String myStr = String(myIP[0]) + "." +
                   String(myIP[1]) + "." +
                   String(myIP[2]) + "." +
                   String(myIP[3]);

2 Likes

Thanks all, this worked.
Ron

FYI did some testing tonight… it appears sprintf() doesn’t consume 20KB of flash anymore, more like 2000 bytes or so. The String object method consumes about 120 bytes more than the strcat()/itoa() method, and strcat()/itoa() is about 1120 less than sprintf().

1 Like