Spark Core External IP

Hey is it possible to get the External IP of the spark core?

Im looking to set one up as a dynamic IP updater.

So there are lots of ā€œget my IP addressā€ web page services. Most of them want you to pay if you are using it programmatically. Here is one siteā€™s FAQ and pricing:

So you would have a TCPClient on the core that fetched a web page and parsed it to find the IP address. You could then publish that to the world via a Spark.variable() or Spark.publish().

3 Likes

I could access my scripts through a TCPClient. I was hoping to push it through the cloud without calling an external service.

From what I read I thought sparks cant connect to https services because of the ssl handling requirements

Found one free service http://www.telize.com/, they says it is free and no usage limit is there.

1 Like

thanks I will give it a try

This might be something simple that could be added to the Spark Cloud API. Maybe as part of GET /v1/devices or GET /v1/devices/{DEVICE_ID}. What do you think @Dave?

1 Like

Totally, Iā€™ve been thinking about this. We have a reserved event namespace (published events starting with ā€œSparkā€), so I think there is an opportunity to add a ton of flexible and powerful functionality between the core and the server, especially for stuff like ā€œWhatā€™s my IPā€, ā€œStore this value,ā€, etc. For me, the biggest thing to solve before rolling that out is just designing a good user interface / and documenting it well. :slight_smile:

Thanks,
David

My first approach (without any sugar added) looked something like this, but I think it deserves a nice wrapper. The general ā€œask server for something / setup a function to run when it comes backā€ pattern would be easy to wrap.

void setup() {
    Spark.subscribe("spark/get/ip_address", external_ip_handler);
}
void external_ip_handler(const char *event, const char *data)
{
    if (data && (strlen(data) != 0)) {
        my_external_ip_address = String(data);
    }
}
1 Like

I was thinking of something much simpler from the Cloud with minimal user effort and firmware overhead. Just return the external IP address (the one the Cloud sees) as part of the core device info API call. Without knowing the cloud code/infrastructure, I could take a blind guess at using something along the lines of socket.remoteAddress?

1 Like

I like the idea of it being incorporated as a spark function

@Dave Did you rolled out something to get the external IP inside the Firmware?

Yup! If you subscribe to the event ā€œspark/device/ipā€, and publish the event ā€œspark/device/ipā€, the cloud will reply with your external ip. :slight_smile: (secret features I need to document!)

Thanks,
David

3 Likes

Hi @Pascal
Dave, thanks, that works great!!!

Didnā€™t checked yet as using for now serial.println, if the data format returned for the external IP is an IPAddress (would be programatically preferable) or a stringā€¦

Other questions:
1/ would it be possible that at the subscribe, there is automatically an event ā€œspark/device/ipā€ published, or even a flow of events when there is external IP changesā€¦ :
at any network change (e.g. disconnect/reconnect), or when the external IP change for any other reason (like connected on a mobile network),it could be automatically an updated event published - i.e. do you on-time keep track on the cloud side for each core of its external IP, and are you event notified of that?

Pascal

1 Like

@Pascal,

Just opened an issue this morning but havenā€™t had time to work on a PR:

See example here: https://github.com/spark/docs/issues/297

1 Like

Nice to hear this feature has been incorporated.

Iā€™ll have to give it a try!

1 Like

Sorry about the slow reply, finally catching up on email! I like the idea of triggering it based on the subscribe instead of the publish, Iā€™ll make a note for myself to look into that when I get a chance. :smile:

Thanks!
David

Hi Dave,

did you had a chance to check on a subscribe approach, in order not to poll all time and rather get a notification when IP change, which may happen from time to time, depending on telcosā€¦ :smile:

Thanks

Pascal

Hi @Pascal,

Hmm. Generally speaking if your IP address changed during the session, I would think the session would end. So your IP address at the start of a session would be the same for the whole life of the session, so grabbing the IP in ā€˜setupā€™ is reasonable, yeah? Or am I missing something?

Thanks!
David

Hi @Dave
you might be right, but it probably depends on the implementation of both server side and client.
I just know that in mobile internet (or small 3G/4G internet router), or also in some fixed internet line, you are not allocated a fixed external IP due to lack of IP v4 numbers. So what is happening if external IP change during a session (loop) between the Core/Photon and the cloud ? how is the reconnection to the cloud re-handled?

Hi @Pascal! This is an interesting scenario, one that I know we will meet more and more. When the IP changes externally, I would guess all existing connections are dropped? If so, the device will temporarily disconnect from the cloud, and then reconnect.

We have an upcoming feature in firmware called system events. These tell your application about interesting things happening in the system. One of the notifications pertains to the connection state of the cloud - an event is sent each time the cloud connects or disconnects. Your application could listen for the cloud connection event and when the cloud is connected, sends the request to the cloud for the updated IP address.

This means you only request the IP address on each connect to the cloud rather than continually polling while the connection is maintained.