Search code examples
ibm-cloudwatson-iot

How to get a device/gateway IP address from Bluemix IoT service?


What needed?

I need to obtain device/gateway current IP address to determine its location.

How to achieve this?

I found 2 options:

  1. Get a gateway IP address through Bluemix API because I see it in admin panel so decide it's available through API (but is can be not). But can’t find appropriate API call. Please, suggest one if it's implemented.
  2. In API response for device details, I saw additional location parameter, but can't find a full description how to use it. This parameter sends back with a links for logs and error codes. But on any API request, location parameter returns an empty string.

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.


Solution

  • 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".

    1. 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" } ]

    2. 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 }

    3. 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?

    4. 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.

    5. 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.