What needed?
I need to obtain device/gateway current IP address to determine its location.
How to achieve this?
I found 2 options:
Can anyone help me with any of this options or propose another solution?
P.S. Current workaround is to send an IP address from the gateway directly, but it looks not the best possible option.
There is no direct "give me the IP address of this device" API, but there are a few different sources of this information:
For the following, assume a device exists with type = "testDeviceType"
and id = "testDeviceId"
and org = "123456"
.
The connection logs API
GET
/api/v0002/logs/connection?typeId=testDeviceType&deviceId=testDeviceId
Response:
[
{
"timestamp": "2016-10-22T05:07:36.064Z",
"message": "Closed connection from 127.0.0.1. The connection was closed by the client. Error=Connection timed out(110)"
},
{
"timestamp": "2016-10-22T03:59:36.182Z",
"message": "Token auth succeeded: ClientID='d:123456:testDeviceType:testDeviceId', ClientIP=127.0.0.1"
}
]
Subscribing to the monitoring topic with an api key:
topic = iot-2/type/+/id/+/mon
Message Payload:
RCV [iot-2/type/testDeviceType/id/testDeviceId/mon][retained] {"Action":"Connect","Time":"2016-11-01T14:18:36.550Z","ClientAddr":"127.0.0.1","ClientID":"d:123456:testDeviceType:testDeviceId","Port":8883,"Secure":true,"Protocol":"mqtt4","Durable":false }
As mentioned in the question, it could always be specified in message payload but maybe there are some reasons not to trust this information being published by the device?
The DeviceInfo field on devices includes a descriptive location
field which is just a string and could technically be set to the IP address of a device... this might be a bit far-fetched though.
Device geolocation can also be set over HTTP or by using the device management MQTT topics.
https://console.ng.bluemix.net/docs/services/IoT/devices/device_mgmt/index.html#update-location
{
"d": {
"longitude": number,
"latitude": number,
"elevation": number,
"measuredDateTime": "string in ISO8601 format",
"updatedDateTime": "string in ISO8601 format",
"accuracy": number
},
"reqId": "string"
}
Another thing to keep in mind, since gateways are mentioned, is that the connection logs and monitoring topic mentioned in (1) and (2) are only relevant for devices which connect directly to the platform.