Network.localIP() to String (to get it via Spark.variable())

Hi, I need to learn core’s LocalIp address from the cloud. But I couldn’t get the structure/type of the Network.localIP(). I cant use serial.write because core isnt appeared in device manager list neither as com port or as another thing.


I think that IPAddress overloads the indexing operator (square brackets or [ ]) so you ought to be able to do:

IPAddress myAddr = Network.localIP();

byte first_octet = myAddr[0];
byte second_octet = myAddr[1];
byte third_octet = myAddr[2];
byte fourth_octet = myAddr[3];

From there you just have four bytes (unsigned char) to convert to a string with ‘.’ between them.

1 Like

I get this error:
error: invalid operands of types ‘const char*’ and ‘const char [2]’ to binary ‘operator+’

IPAddress myAddr = Network.localIP();

byte first_octet = myAddr[0];
byte second_octet = myAddr[1];
byte third_octet = myAddr[2];
byte fourth_octet = myAddr[3];

String  localIP =  first_octet  + "." + second_octet  + "." + third_octet  + "." + fourth_octet;

Is this wrong?

I also tried;

String localIP = "" + a;
   localIP += "." + b;
   localIP += "." + c;
   localIP += "." + d;

It compiled but bring me just nothing. Just empty string “”

I am afraid that I don’t know much about the Arduino String class but redefining the name localIP from method to variable seems like a bad idea. Here is some code that worked fine for me.

  localAddr = WiFi.localIP();
  byte oct1 = localAddr[0];
  byte oct2 = localAddr[1];
  byte oct3 = localAddr[2];
  byte oct4 = localAddr[3];
  char s[16];  
  sprintf(s, "%d.%d.%d.%d", oct1, oct2, oct3, oct4);

Hope this helps!


No it didn’t :smiley: But thanks, its indeed a good idea.

I couldnt find a solution yet.

Threw this in an app from above:

char myIpString[24];
void setup() {
    IPAddress myIp = WiFi.localIP();
    sprintf(myIpString, "%d.%d.%d.%d", myIp[0], myIp[1], myIp[2], myIp[3]);
    Spark.variable("ipAddress", myIpString, STRING);

Edit: This is just a mash up of what @bko had posted, I just put them next to each-other. :slight_smile:

Works great!


Yeah its great! thanks @Dave :slight_smile:

So, what is the difference between yours and @bko’s?

Hi @triuman

@Dave’s code just expanded on what I wrote to hook up a :spark: variable so you can read the IP address of your core from another computer on the internet. This is a good thing.

I had experimented with this earlier and discovered that the :spark: cloud API requires HTTPS and so one :spark: core cannot (easily) use the API to discover another :spark: core. So if you are building say a multisensor network and want the cores to talk to each other, you would need to either (1) use another protocol such as the way :spark: cores announce themselves over UDP multicast on powerup or (2) use a full host on your network to act as the “master” to communicate addresses between the cores so they can start talking to each other.

@Dave, maybe that is a longer term feature request: provide a way for a :spark: core to query another :spark: core variable through the cloud, using the firmware crypto to avoid HTTPS. What do you think?

[EDIT] I see this is very similar to the posting here, so perhaps that is a better way.

Hi @bko,

We’ve been talking about the best way to accomplish this, and I think we have some really exciting ideas about a feature similar to this. We’re definitely going to build something that will let you do this, and hopefully it will be ready in the next few sprints. First thing I’m building with it is a Core -> Core Telegraph. :smile:


when I try the above code I get this from the spark api:

“id”: my id,
“name”: “sparkHead”,
“connected”: true,
“variables”: {
“ipAddress”: “string”
“functions”: []

any idea what might be happening?

You are reading the device, but not the variable. Try adding “/ipAddress” to the end of your URL (before any “?” parameters of course).

The response you got is the one for discovering what functions and variables a core has. This core has one variable named “ipAddress” which is type string and no functions.

1 Like

thanks bko, that did the trick.

Is there documentation for how to use the api like this?

There is a whole section of doc devoted to this:

1 Like

Aha, thanks. My username came in good use there i guess :slight_smile:


This thread was very helpful, but I can’t find where the returned struct (or type) returned by localIP() is defined. Can anyone point me in the right direction?

Thanks as ever.
Alan T

Hi @AlanSparkMan

It is an object called IPAddress which has a lot of methods for printing and setting etc. On Arduino you pass around a 4-byte char array but on Spark that idea is rolled up in the IPAddress object, but you can address it like an array too.

See the doc here:

Thanks, I needed that!

i didn’t understood that how to use.
Which command did i have to type to get out the created spark variable?
I did come from the question to get out the ip address of the Photon?
I have compiled the above code no problem. But i didn’t get together how to get now the ip address.
thx in advance for your help

Hi @hape

Have you seen the doc for the Particle Cloud API here:

You can get your IP address cloud variable via many methods, curl, the Particle CLI, a web page, a NodeJS program or any other technology that can do web requests.

1 Like