2
votes

What is the approach to be followed (on the device) if we have to publish events to cloud and subscribe for MQTT messages on the Device at the same time using the auth-token provided by the Watson IoT platform. When we try to publish to a topic when the device has already subscribed to some topics then the connection is lost. What would be the right approach to be followed in order to publish and subscribe at the same time? The details of the MQTT connection are as follows,

Client id- <d:orgId:deviceType:deviceId>
Username - use-token-auth
Password - <platform-generated-token>
1
I don't see what that would be a problem. Unless you have 2 connects? If you could update the question with your code it would be helpful.amadain
You should not be being disconnected because you are subscribing and publishing concurrently if you are using the same connection to do both. It sounds like to me you are connecting and subscribing, and then trying to connect again (and therefore reusing the client ID, which is not allowed) and so being disconnected. If this is not the case I suggest you provide your org ID so someone from WIoT can take a look and tell you the exact reason - MQTT 3.1.1 is not good at telling you reason codes. You could also check your connection logs in the UI or using the API.Paul Slater

1 Answers

2
votes

As per the documentation the devices in Watson IoT Platform can send events and subscribe to commands from the Platform. Note that the devices can only subscribe to commands and not events. Can you make sure that the topics used are correct?

Events

Events are the mechanism by which devices publish data to the Watson IoT Platform (and to the application). Devices control the content of their messages and assign a name for each event that is sent. The following MQTT topic to be used to publish the events,

iot-2/evt/event_id/fmt/format_string

where

  • event_id is the ID of the event, for example status. The event ID can be any string that is valid in MQTT. If wildcards are not used, subscriber applications must use this string in their subscription topic to receive the events that are published on their topic.
  • format_string is the format of the event payload, for example json. The format can be any string that is valid in MQTT. If wildcards are not used, subscriber applications must use this string in their subscription topic to receive events that are published on their topic.

Commands

Commands are the mechanism by which applications communicate with devices. Only applications can send commands, and the commands are sent to specific devices. The device must determine which action to take on receipt of any given command. 2 Devices can subscribe to command topics in the following format:

iot-2/cmd/command_id/fmt/format_string

Where

  • command_id is the ID of the command, for example, update. The command ID can be any string that is valid in the MQTT protocol. If wildcards are not used, a device must use this string in its subscription topic to receive commands that are published on their topic.
  • format_string is the format of the command payload, for example json. The format can be any string that is valid in the MQTT protocol. If wildcards are not used, a device must use this string in its subscription topic to receive commands that are published on their topic.

Refer to this section of the document for more information about the device connectivity to the Watson IoT Platform.

We have written a step by step guide to show how one can conect a device to Watson IoT Platform, publish events, subscribe to commands from application, subscribe to events in application, publish commands from the application to deivce using the Java client library for Watson IoT Platform and is availabe here - Java Client Library for IBM Watson IoT Platform.

Also, you can find samples in multiple languages. Refer this document for the list of available libraries and samples.