Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / IoT

Connecting to IBM Bluemix Internet of Things using MQTT

4.00/5 (1 vote)
17 May 2016CPOL2 min read 9.3K  
This article provides a step-by-step guide to show you how to set up a connection to IBM Bluemix using MQTT, as well as command and code samples for additional setup and ease of use.

This article is for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers

Get access to the new Intel® IoT Developer Kit, a complete hardware and software solution that allows developers to create exciting new solutions with the Intel® Galileo and Intel® Edison boards. Visit the Intel® Developer Zone for IoT.

This article provides a step-by-step guide to show you how to set up a connection to IBM* Bluemix* using MQTT*, as well as command and code samples for additional setup and ease of use.

IBM* Bluemix* IoT initial setup

  1. Create an account, if you do not yet have one. Image 1

  2. Log into your account. Image 2

  3. Select New Dashboard if that option is presented.

Add an Internet of Things Platform

  1. Go to your dashboard and click Use Services or APIs to add a new service. Image 3

  2. Click Internet of Things Platform. Image 4

  3. In the Service Name field, type a name of your choosing. Image 5

  4. From the Selected Plan drop-down list, choose a pricing plan. Image 6

  5. Click the Create button. Image 7

Add a device type

  1. Under Connect your devices, click the Launch dashboard button.

  2. This opens a new IBM Watson IoT Platform window. Image 8

  3. On the Device Types tile, click the Add Device button. Image 9

  4. Click the Create device type button.

  5. This opens the Create Device Type page. Image 10

  6. Click the Create device type button. Image 11

  7. Fill the Name and Description fields and click Next. Image 12

  8. Specify the attributes for your template and click Next. Image 13

  9. In the Submit Information step, click Next. Image 14

  10. If necessary, add metadata and click Create. Image 15

Add a device

  1. From the Choose Device Type drop-down list, select the new device type you created in the previous section, and click Next. Image 16

  2. In the Device ID field, type the ID of your device and click Next. Image 17

  3. If necessary, add metadata and click Next. Image 18

  4. In the Security step, auto-generate an authentication token by clicking Next. Image 19

  5. Click Add. Image 20

  6. Make a note of the Authentication Token displayed under Your Device Credentials. Image 21

Summary

If you have followed all the steps above, you should have all the information that your program needs to connect to the MQTT* server:

MQTT_SERVER - use \<Your organization ID\>.messaging.internetofthings.ibmcloud.com, along with the ssl:// (for C++) or mqtts:// (for JavaScript*) protocol

MQTT_CLIENTID - use d:\<Your organization ID\>:\<Your device type\>:\<Your device ID\>

MQTT_TOPIC - use iot-2/evt/status/fmt/json

MQTT_USERNAME - use use-token-auth

MQTT_PASSWORD - use the string with the authorization token of your device

Additional setup for C++

When running your C++ code on the Intel® Edison board, you need to set the MQTT* client parameters in Eclipse*. To do that:

  1. Go to Run configurations and, in the Commands to execute before application field, type the following:

    C++
    chmod 755 /tmp/<Your app name>; export MQTT_SERVER="ssl://<Your organization ID>.messaging.internetofthings.ibmcloud.com:8883"; export MQTT_CLIENTID="d:<Your organization ID>:<Your device type>:<Your device ID>"; export MQTT_USERNAME="use-token-auth"; export MQTT_PASSWORD="<Your authorization token>"; export MQTT_TOPIC="iot-2/evt/status/fmt/json"
  2. Click the Apply button to save these settings.

  3. Click the Run button to run the code on your board.

Additional setup for JavaScript*

When running your JavaScript* code on the Intel® Edison board, you need to set the MQTT* client parameters in the Intel® XDK IDE. Add the following entries to the config.json file:

JavaScript
{
"MQTT_SERVER": "mqtts://<Your organization ID>.messaging.internetofthings.ibmcloud.com:8883",
"MQTT_CLIENTID": "d:<Your organization ID>:<Your device type>:<Your device ID>",
"MQTT_USERNAME": "use-token-auth",
"MQTT_PASSWORD": "<Your authorization token>",
"MQTT_TOPIC": "iot-2/evt/status/fmt/json"
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)