Overview

Skill Level: Beginner

Developers who want to build gateway applications that communicate with the cloud. 

Ingredients

Developers who want to build gateway applications that communicate with the cloud. 

Step-by-step

  1. Overview

    The application will consist of two parts:

    • The device application generates events and listens for commands. The device application runs on an IoT Gateway or a similar device, like a Raspberry Pi, or your computer. This part is built in Reactive Blocks, where each functions corresponds to a building block.
    • The cloud application runs a Node-RED instance in Bluemix. It receives the events, and sends some commands (depending on data in the events) back to the device.

    The two applications communicate via the IBM Watson IoT service. The device application is registered in the IBM Watson IoT service as a device. The result is a minimal application that runs and can easily be extended towards a more complex application.

  2. Install Reactive Blocks

    Follow these instructions to install Reactive Blocks.

  3. Sign up For Bluemix

    1. Sign up for Bluemix.
    2. From the Catalog, select Internet of Things Foundation Starter as Boilerplate.
      • We have observed that this boilerplate was only available in the Region US South, but this may change.
    3. Click on Create.
    4. Check if the created application has the IBM Watson IoT Service attached. If not, you need to add it.
      1. Visit the overview page of your application
      2. Select “+ Add a Service or API”
      3. Add the Service “Internet of Things”
  4. Import the Example into Reactive Blocks

    • After Eclipse restarted, open the Reactive Blocks perspective: Select Window Open Perspective Other… Reactive Blocks
    • Click the import button in the Blocks View, as shown here:
    • Select Simple Examples and Tutorials
    • The wizard asks you to sign in, using your Google or Facebook Id
    • Import Build an IoT Foundation Device
    • When you finish the wizard, the blocks editor opens with the following application:
    This is how the device application works:
    1. createParams is a Java operation that creates the configuration for the IBM Watson IoT Platform.
    2. IoT Foundation is the block to connect to IBM Watson IoT Platform. Once it’s ready, we continue via pin ‘ready’.
    3. The Event Simulator Window opens. Each time we click on the “Send Event” button, a new event is created.
    4. Events are sent into the IBM Watson IoT Platform via the block “Event”.
    5. Commands are received from the IBM Watson IoT Platform via the blocks “Command”. In the example, we just print out the command.
  5. Register an IoT Device in the IBM Watson IoT Platform

    On the pages for theIBM Watson IoT Platform, register a new Device. As a result, you will receive a set of properties for your device that you should write down. For instance:
    org=... type=... id=... auth-method=tokenauth-token=...

  6. Build the Node-RED Application

    1. Drag the input nodes inject (twice) and ibmiot into the canvas
    2. Drag the output nodes debug and ibmiot (twice) into the canvas
    3. Connect the nodes as shown in the screenshot
    4. Double-click on the inject node (it may be labelled timestamp)
      • Set the Payload to “blank”
      • Set the name to “on” (or “off”, respectively)
    5. Double-click on the other inject node, and configure it in the same way
    6. Double-click on the IBM IoT App Out, to reveal its details
      • Set the authentication to Bluemix Service, Output type to Device Command
      • Set Device Type and Device Id to the values registered previously
      • Set the Command type to “on”
      • Set the Format to “json”
      • Set the Data to “{}” (Two curly braces, denoting an empty JSON data object.)
    7. Double-click on the other IBM IoT App Out node, set the same properties as above, just the command type to “off”
    8. Double-click on the IBM IoT App In node
      • Set the Authentication type to Bluemix Service
      • Set the Input Type to “Device Event”
      • For Device Type, Device Id, Event, Format, check “All”
    9. Deploy the application by clicking “Deploy” in the upper right corner

    This is a screenshot of the configuration of the nodes:

  7. Adjust and Build the Client Application

    Double-click on the createParams method, to configure your device parameters. Set the device Id, device type, organization and the authentication token that you received when creating the device with the IBM Watson IoT service.

    public IOTParams createParams() {    // TODO set the following variables after     // registering a new device at the IoT Foundation:   String deviceId = "";    String deviceType = "";   String organization = "";   String authenticationToken = "";    IOTParams p = new IOTParams(deviceId, deviceType, organization, authenticationToken);   return p;}

    • To build the application, right-click in the blocks editor and select Build / Re-Build for Java Standard Edition
    • The package explorer will open and reveal the generated Java project
  8. Run the Example

    Find the launch configuration, right-click, and select Run As / IOTFoundationQuickstart

    Now, the following happens:

    1. The window for the event simulator opens.
    2. Whenever you click on the Send Event button, the device application sends in a new event.
    3. The Node-RED receives the event and prints it via the debug node into the debug sidebar, to the right
    4. Whenever you click any of the inject nodes in Node-RED, the device application receives the corresponding command, on or off.

    If the device application cannot connect, this may be due to a firewall that blocks the MQTT traffic via ports 1883 or 8883.

Expected Outcome

 
The application will consist of two parts:
 
  • The device application generates events and listens for commands. The device application runs on an IoT Gateway or a similar device, like a Raspberry Pi, or your computer. This part is built in Reactive Blocks, where each functions corresponds to a building block.
  • The cloud application runs a Node-RED instance in Bluemix. It receives the events, and sends some commands (depending on data in the events) back to the device. 
 
The two applications communicate via the IoT Foundation service. The device application is registered in the IoT Foundation service as a device. The result is a minimal application that runs and can easily be extended towards a more complex application.

2 comments on"Reactive Blocks to IBM Watson IoT Platform (Part 2: IoT Device)"

  1. Hi, Thank you for the nice recipe!
    Just to let you know, I’ve tried this tutorial and found that
    >Run the Example
    >Find the launch configuration, right-click, and select Run As / “IOTFoundationQuickstart”
    should be
    Find the launch configuration in launch folder(IoTFoundationDevice.launch) , right-click, and select Run As / “IoTFoundationDevice”.
    to make it work succesfully.
    Thanks.

  2. […] IoT Foundation: IoT Foundation is a service from IBM to send event data into Bluemix, and control devices via commands. IoT Foundation uses MQTT in its core. It adds a specific topic structure to manage devices, events and commands. It also stores all events, so that they are available as historic data. IoT Foundation is ideal for sending sensor readings and similar data. Since you send it to IBM’s broker, you do not have to setup your own MQTT broker. We have two tutorials on how to use the IoT Foundation: Without setup of Bluemix in Reactive Blocks to IoT Foundation (Part 1: Quickstart), and a more elaborate tutorial in Reactive Blocks to IoT Foundation (Part 2: IoT Device). […]

Join The Discussion