QBoard » Supporting Tech Stack » IOT » How do you publish to a MQTT topic in IoT using java?

How do you publish to a MQTT topic in IoT using java?

  • In Bluemix I have a java_buildpack application that currently picks up/consumes a message from a Message Hub topic and I now want to reformat that message and publish it to a Mqtt client. I have the Internet of Things Platform service bound to my java_buildpack application and I created a device type, javaDevice, and device id, javaDevice01.

    Here's the environment variables for my iotf-service:

    "iotf-service": [ { "name": "Internet of Things Platform-or", "label": "iotf-service", "plan": "iotf-service-free", "credentials": { "iotCredentialsIdentifier": "a2g6k39sl6r5", "mqtt_host": "qzfiiy.messaging.internetofthings.ibmcloud.com", "mqtt_u_port": 1883, "mqtt_s_port": 8883, "base_uri": "https://qzfiiy.internetofthings.ibmcloud.com:443/api/v0001", "http_host": "qzfiiy.internetofthings.ibmcloud.com", "org": "qzfiiy", "apiKey": "xxxxxxxxxxxxxxx", "apiToken": "xxxxxxxxxxxxxx" } }

    When I create the instance of the MqttClient, what should I be passing in:

    MqttClient mqttClient = new MqttClient(broker, clientId, persistence);

    I thought I read somewhere for the clientId that it should be like:

    A:qzfiiy:javaDevice:javaDevice01

    What should be passed in for broker? Do I use this environment variable - "mqtt_host": "qzfiiy.messaging.internetofthings.ibmcloud.com",

    Any help would be greatly appreciated.

    Thanks!

      January 6, 2022 1:22 PM IST
    0