IBM Watson IoT from Mbed OS device

Japanese translation version by toyowata available here.

Overview

In this page, we'll try to connect to IBM Watson IoT Platform from a Mbed OS device and send/receive messages.

/media/uploads/coisme/ibm-001.png

We use Mosquitto as Application.

Prerequisites

  • An IBM Cloud account (Create an account here if you don't have it)
  • Mbed OS board
  • Network connectivity
  • Mosquitto
  • Mbed account
  • Mbed CLI (optional)

Test Environment

  • macOS HighSierra 10.13.6 (17G65)
  • Mbed Enabled board - FRDM-K64F
  • Ethernet (on the board)
  • mosquitto version 1.4.14 (build date 2017-10-22 16:34:39+0100)

Set up the Watson IoT Platform service

Set up the Watson IoT Platform service by following the step 1 in this page. You should memo the following parameters in the step.

Device

  • Organization ID
  • Device Type
  • Device ID
  • Authentication Token

Application

  • API Key
  • Authentication Token

Build firmware

Let's build the firmware. You can use either online compiler or CLI. Here, we use the online compiler.

Import project

Log in to online compiler and click Import. You'll see the import wizard. Follow the link click here in the image below.

Input the repository URL https://github.com/coisme/Mbed-to-IBM-Watson-IoT into the Source URL textbox.

The project will be imported into your workspace.

Modify parameters

Edit MQTT_server_setting.h in the project root. Four parameters need to be changed.

  • ORG_ID - Organization ID obtained in the previous step.
  • DEVICE_TYPE - Device Type obtained in the previous step.
  • DEVICE_ID - Devie ID obtained in the previous step.
  • TOKEN - Authentication Token of device obtained in the previous step.

Compile

Click the Compile button in the top menu.

/media/uploads/coisme/ms-005.png

If compilation done successfully, <project name>.bin file will be downloaded. Please flash the <project name>.bin file to your target board.

Run

Let's run the application and see how it works.

Connect to your board

To see messages from your board via USB serial, run this command from the terminal:

$ mbed sterm -b 115200

note

To run the mbed command above, Mbed CLI is needed. If you don't have Mbed CLI, use your terminal software, for example Teraterm, Putty, or CoolTerm.

Push the reset button on your board, application will be relaunch. You'll see messages from your board like the picture below:

/media/uploads/coisme/ibm-003.png

Send a message from Device to Application

Let's send a message from your device to an application. This device to application message is called an event. We use Mosquitto to receive events from your device via IBM Watson IoT Cloud.

/media/uploads/coisme/ibm-004.png

When you push the button 1 on your board, an event is sent to Watson IoT Platform. Watson IoT Platform relays it to the application.

Launch Application

You need to download a server's root CA certificate IoTFoundation.pem from here.

To launch an application with Mosquitto, run this script file on your terminal. Edit orgId, cafile, password, username, deviceType, and deviceId to your environment.

application_recv_evt.sh

#!/bin/tcsh

set orgId="<< YOUR ORG ID >>"
set cafile="<< path to >>/IoTFoundation.pem"
set password="<< YOUR AUTHENTICATION TOKEN for APPLICATION >>"
set username="<< YOUR API KEY >>"
set deviceType="<< YOUR DEVICE TYPE >>"
set deviceId="<< YOUR DEVICE ID >>"

set cid="a:${orgId}:apitest"
set host="${orgId}.messaging.internetofthings.ibmcloud.com"
set port=8883
set eventId="myevt"
set topic="iot-2/type/${deviceType}/id/${deviceId}/evt/${eventId}/fmt/text"

mosquitto_sub -h ${host} -p ${port} -q 1\
    -t "${topic}" \
    -i ${cid} \
    -d \
    -u ${username}\
    -P ${password}\
    --cafile ${cafile}\
    -V mqttv311

After you launch the Application's script, it waits for receiving messages.

Send messages from Device

Push the button 1 on your board, your device publish a message to the topic iot-2/evt/myevt/fmt/text.

And Application will receive the message from Watson IoT Platform through the topic specified in the above script.

Send a message from Application to Device

/media/uploads/coisme/ibm-005.png

Send a message from Application

We use Mosquitto to send a message as an Application. Edit orgId, username, password, deviceType, deviceId, and cafile to your environment.

application_send_cmd.sh

#!/bin/tcsh

set orgId="<< YOUR ORG ID >>"
set username="<< YOUR API KEY >>"
set password="<< YOUR AUTHENTICATION TOKEN for APPLICATION >>"
set deviceType="<< YOUR DEVICE TYPE >>"
set deviceId="<< YOUR DEVICE ID >>"
set cafile="<<path to >>/IoTFoundation.pem"

set host="${orgId}.messaging.internetofthings.ibmcloud.com"
set cid="a:${orgId}:apitest"
set cmdId="mycmd"
set topic="iot-2/type/${deviceType}/id/${deviceId}/cmd/${cmdId}/fmt/text"

mosquitto_pub -h "${host}" -p 8883\
        -u "${username}"\
        -P "${password}"\
        -i ${cid}\
        -t "${topic}"\
        -m "`date`"\
        --cafile "${cafile}"

When you run this script, it sends a command which contains the return of date command. Then the device will receive the command and show it on your terminal.


3 comments on IBM Watson IoT from Mbed OS device:

21 Jan 2019

Compiling produces an error: Error: Library name 'atmel-rf' is not unique (defined in '/tmp/chroots/ch-256bc631-fd1d-4cf6-8f53-5fc46601ddf0/src/easy-connect/atmel-rf-driver/mbed_lib.json' and '/tmp/chroots/ch-256bc631-fd1d-4cf6-8f53-5fc46601ddf0/extras/mbed-os.lib/components/802.15.4_RF/atmel-rf-driver/mbed_lib.json') How to solve that?

21 Jul 2019

Same issue

26 Sep 2019

Bob van der Putten wrote:

Compiling produces an error: Error: Library name 'atmel-rf' is not unique (defined in '/tmp/chroots/ch-256bc631-fd1d-4cf6-8f53-5fc46601ddf0/src/easy-connect/atmel-rf-driver/mbed_lib.json' and '/tmp/chroots/ch-256bc631-fd1d-4cf6-8f53-5fc46601ddf0/extras/mbed-os.lib/components/802.15.4_RF/atmel-rf-driver/mbed_lib.json') How to solve that?

Updated the program and now it should be solved.

Please log in to post comments.