简体   繁体   中英

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!

If you are connecting as an application, use the application criteria. If connecting as device, use the device criteria (see below).

For DEVICE: authenticate using a client ID in the following format

 d:$org:$type:$id

MQTT client ID must be d:$org:$type:$id

MQTT username must be use-token-auth

MQTT password must be the authentication token: auth token (ex: MP$08VKz!8rXwnR-Q*)

An APPLICATION authenticates using a client ID in this format:

 a:$org:$appId

When making an MQTT connection using an API key the following applies:

MQTT client ID: a:$org:$appId

MQTT username must be the API key: a:$org:apikeyvalue

MQTT password must be the authentication token: APItoken

in case you needed the topics, then those are as follows.

For applications, events topic is iot-2/type/{device type}/id/{device id}/evt/{event type}/fmt/{format type}

For applications, commands topic is iot-2/type/{device type}/id/{device id}/cmd/{command type}/fmt/{format type}

For devices, events topic is iot-2/evt/{event type}/fmt/{format type}

For commands, commands topic is iot-2/cmd/{command type}/fmt/{format type}

You can refer to the documentation for further information. https://iotf.readthedocs.org/en/latest/?cm_mc_uid=41893914391914121143444&cm_mc_sid_50200000=1460668601

You can also use the java client library which encapsulates all the functionalities and provides wrapper APIs https://github.com/ibm-messaging/iot-java/releases?cm_mc_uid=41893914391914121143444&cm_mc_sid_50200000=1460668601

For MQTT broker, you need to use {orgid}.messaging.internetofthings.ibmcloud.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM