Tutorial: Build an IBM IOT Foundation Device

We send data from applications using the visual Reactive Blocks tool with Java to the IoT Foundation. In this tutorial, we are going to simulate a device that sends events and receives commands. On the server side, we use a Node-RED application to receive events and send commands.

This tutorial requires that you sign up for Bluemix, and requires that you register a new device in the IoT Foundation service.

Note that the IoT Foundation uses MQTT over ports 1883 or 8883, which must not be blocked by your firewall.

Why IBM Internet of Things Foundation? With the IBM Internet of Things Foundation you can collect event data from devices, forward it to other applications, and retrieve historical data.

Why Reactive Blocks? With Reactive Blocks you can build robust applications from building blocks. The simple example here can for instance be extended, block for block, towards the final application.

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 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.

Step 1: Install Reactive Blocks

Step 2: Import the Example

  • In the Blocks view, click the import button.
  • Import Build an IoT Foundation Device from the Tutorial Team.

This is how the device application works:

  1. createParams is a Java operation that creates the configuration for the IoT Foundation.
  2. IoT Foundation is the block to connect to IoT Foundation. 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 IoT Foundation via the block “Event”
  5. Commands are received from the IoT Foundation via the blocks “Command”. In the example, we just print out the command.

Step 3: Sign up For Bluemix

  1. Sign up for Bluemix
  2. From the Catalog, select Internet of Things Platform Starter as Boilerplate.
  3. Click on Create
  4. Check if the created application has the IoT Foundation Service attached. If not, you need to add it.
    • Visit the overview page of your application
    • Select “+ Add a Service or API”
    • Add the Service “Internet of Things”

Step 4: Register an IoT Device in the IoT Foundation

On the pages for the IoT Foundation, register a new Device. (Click on “Internet of Things Platform” service, then open the “BluemixDashboard”.) 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=token
auth-token=...

Step 5: Build the Node-RED Application

From your Dashboard in Bluemix, open the site for the application, and open the Node-RED editor. This is where you build the application part that runs in the cloud.

You will see an existing system in the Node-RED editor. You can play with that later, but for now just add our stuff below the existing system.

  • Drag the input nodes inject (twice) and ibmiot into the canvas.
  • Drag the output nodes debug and ibmiot (twice) into the canvas.
  • Connect the nodes as shown in the screenshot.
  • Double-click on the inject node (it may be labelled timestamp)
    • Set the Payload to “blank string”
    • Set the name to “on” (or “off”, respectively)
  • 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.)
  • Double-click on the other IBM IoT App Out node, set the same properties as above, just the command type to “off”.
  • 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”.
  • Deploy the application by clicking “Deploy” in the upper right corner.

Step 6: 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 IoT Foundation 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, select Build / Re-Build for Java Standard Edition
  • The package explorer will open and reveal the generated Java project.

Step 7: Run the Example

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

Now, the following should happen:

  • The window for the event simulator should open.
  • Whenever you click on the Send Event button, the device application sends in a new event.
  • The Node-RED receives the event and prints it via the debug node into the debug sidebar, to the right
  • 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.