Sample for MQTT communication with IBM Watson IoT

This sample shows the communication with IBM Watson IoT.

Topic structure

The topic structure for sending and receiving messages is predefined by the IBM Watson IoT message broker.

Messages are sent to and received from IBM Watson IoT. Since this sample is essentially based on the sample IotMqttSampleUsingQueue, only the parts that are relevant for establishing a connection are explained in this section.

Parameters for establishing a connection

The following code snippet shows the parameters required for establishing a connection to IBM Watson IoT. The parameters are essentially static parameters. These can also be specified in the declaration part during instantiation of the MQTT client.

IF bSetParameter THEN
  bSetParameter := FALSE;
  fbMqttClient.sHostName := '';
  fbMqttClient.nHostPort := 1883;
  fbMqttClient.sClientId := 'd:orgid:IPC:deviceId';
  fbMqttClient.sUserName := 'use-token-auth';
  fbMqttClient.sUserPassword := '12342y?c12Gfq_8r12';

The topics for publish and subscribe are specified by IBM Watson IoT and cannot be changed. The “orgID” placeholder must be replaced with the organization ID of the IBM Watson account. The “deviceID” placeholder is replaced with the ID of the device, as created in IBM Watson.


When data are published to IBM Watson IoT, the topic must be specified in the following form:

iot-2 / evt / eventId / fmt / json

The event ID corresponds to the event ID as configured and expected by IBM Watson IoT. If an IBM Watson dashboard is used, the event ID is generated dynamically and can be linked to a chart on IBM Watson IoT.


When subscribing to IBM Watson IoT commands, the topic must be specified in the following form:

iot-2 / cmd / cmdId / fmt / json

The Cmd ID corresponds to the ID of the command sent by IBM Watson IoT to the device.


Development environment

Target platform

PLC libraries to include

TwinCAT v3.1.4022.0

IPC or CX (x86, x64, ARM)

Tc2_Utilities (>= v3.3.19.0)