David Wood
David Wood

Reputation: 434

I can't DeviceClient.connect() to a device created with a GatewayClient on the IOT platform

I have a system in which devices use REST to talk to my server which acts as an IOT gateway to register and send events on behalf of the devices. This works fine. Now I would like to add remote command/control to the device for which I am creating a DeviceClient (Using the Java APIs). The device uses the same api key and auth token as the gateway and I can see the device in the Devices view on of the dashboard, yet when I connect() I get a connection failure. Are devices created under a gateway allowed to connect directly to the IOT platform? If so, any suggestions.

Looks like one or more connection parameters are wrong !!!
Apr 27, 2017 9:19:40 AM com.ibm.iotf.client.AbstractClient connect
SEVERE: main: Connecting to Watson IoT Platform failed - one or more connection parameters are wrong !!!
Not authorized to connect (5)
    at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:28)
    at org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:990)
    at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:118)
    at java.lang.Thread.run(Thread.java:745)

The connection Properties are as follows:

{API-Key=a-sl0l32-..., iotp.enabled=true, type=AudioSensor, 
 Organization-ID=sl0l32, Authentication-Method=token, Authentication-Token=...., 
 id=cognitiveear-gmail.com-sensor-name}

and my Device is listed in the dashboard as follows:

Device ID cognitiveear-gmail.com-sensor-name
Device Type AudioSensor
Date Added Thursday, April 27, 2017
Added By a-sl0l32-rsmowsjmuj
Connection State Registered Refresh

Upvotes: 1

Views: 627

Answers (3)

David Wood
David Wood

Reputation: 434

I was using the Gateway approach to register my devices since all my sensors talked to my single server. However, now that I want to receive events on the devices, I need to have the devices register directly with the IOT platform. This is primarily so I can get the auth token to create the DeviceClient through which I can then register for events. To register the device, I used the APIClient.

    String deviceID = "test-device";
    String deviceTypeID = "test-type";

    Properties props = new Properties(); 
    props.put("Organization-ID","...");
    props.put("API-Key","...");
    props.put("Authentication-Token","...");
    APIClient apiClient = new APIClient(props); 

    // Make sure the device type exists.
    try {
        apiClient.getDeviceType(deviceTypeID);
    } catch (Exception e) { // Device does not exist!
        JsonObject deviceType = new JsonObject();
        deviceType.addProperty("id", deviceTypeID);
        apiClient.addDeviceType(deviceType);
    }
    // Now create the device and get its auth token
    try {
        apiClient.getDevice(deviceTypeID, deviceID);
        apiClient.deleteDevice(deviceTypeID, deviceID);
    } catch (Exception e) { // Device does not exist!
    }
    JsonObject device = new JsonObject();
    device.addProperty("deviceId", deviceID);
    JsonObject resp = apiClient.registerDevice(deviceTypeID, device); 
    String authToken = resp.get("authToken").getAsString();

    // Now create the DeviceClient 
    props.put("Authentication-Method", "token");
    props.put("Authentication-Token", authToken);
    props.put("id", deviceID);
    props.put("type", deviceTypeID);
    DeviceClient devClient = new DeviceClient(props);
    devClient.connect();
    ...

Upvotes: 0

ValerieLampkin
ValerieLampkin

Reputation: 2626

Checking the logs I see error for that time period: Token auth failed (Device/gateway token is invalid) so you are providing the wrong credentials for authentication.

When you set up your device with the platform there is a point where you can create an Authentication Token, if you didn't create it, then it was auto generated for you. This token is not recoverable once the device is created, the last page in the device set up tells you that you will not be able to access the token after that point. So if you don't remember what this token is then you may need to create a new instance of the device and write down the Authentication Token. This Authentication Token is what would be used for the password, while the username as "use-token-auth" stays as the actual username.

If you automatically registering the devices are you setting the token or letting it be auto-generated?

Upvotes: 1

Mike T
Mike T

Reputation: 36

When you see this output: "Looks like one or more connection parameters are wrong !!!" Most likely that the values that your use to connect are wrong.

For gateway, please check out this sample link : https://github.com/ibm-messaging/iot-gateway-samples/tree/master/java/advanced-gateway-sample

You will need to set :

  • Organization-ID = [Your Organization ID]
  • Device-Type = [Your Gateway Device Type]
  • Device-ID = [Your Gateway Device ID]
  • Authentication-Method = token
  • Authentication-Token = [Your Gateway Token]

For devices, see https://github.com/ibm-messaging/iot-device-samples/tree/master/java

You will need to set :

  • Organization-ID = [Your Organization ID]
  • Device-Type = [Your Device Type]
  • Device-ID = [Your Device ID]
  • Authentication-Method = token
  • Authentication-Token = [Your Device Token]

Upvotes: 1

Related Questions