In earlier sections of this chapter, there were many devices and applications that were not explained in depth. To understand their roles in an IoT solution, it's important to create one example of each.
The scenario created here will be a Device connected to the IBM Watson IoT Platform that sends a timestamp as data, as well as an Application that prints that to stdout using Node.js:

We will then improve this by adding a gateway to the solution, which looks similar to the following diagram:

At the end of the day, the difference of having a gateway connection and a device connection is that you can create an abstraction or specialization of the device connected to the IoT platform, depending on whatever is easier, cheaper, or any other reasons that might drive the decision.
The first task of the job is to create an IoT organization. If you do not have an IBM ID and IBM Cloud account, the sign-up process is very intuitive and only takes a couple of minutes. If you already have an IBM Cloud account and an IBM ID, access the IBM Cloud platform at http://bluemix.net. First, log in and create a space for the exercises in this book.
After logging in to the IBM Cloud platform and accessing the designated space, select the Create resource
option to access the service catalog:

SelectInternet of Things
in the menu and create a service called Internet of Things Platform
. Now, select the option to Create:

When the service is created, you can select the Launch
option and access the IoT Platform:

When you access the IoT Platform, notice that the address is https://xxxxxx.internetofthings.ibmcloud.com/
.
Here, xxxxxx
is your organization ID; make a note of it as it will be used during the entire process.
Creating an application means that you're allowing an actual application or service to connect to a specific Watson IoT Platform organization:
- In order to do that, access the IoT organization through the IBM Cloud dashboard, select Apps from the side menu, then select
Generate API key
and fill in theDescription
field withHands-On IoT Solutions with Blockchain - Chapter 1 App
.
Finally, click onNe
xt:

API key | Authentication token |
- Next, open the IDE of your preference, create a new Node.js project, and install the
ibmiotf
dependency package:
npm install ibmiotf --save
- Ensure that your
package.json
file looks something like the following:
{ "name": "sample-application", "version": "1.0.0", "description": "Hands-On IoT Solutions with Blockchain - Chapter 1 App", "main": "index.js", "scripts": { "start": "node .", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Maximiliano Santos", "license": "ISC", "dependencies": { "ibmiotf": "^0.2.41" } }
- Now, create a file named
application.json
with the following content:
{ "org": "<your iot org id>", "id": "<any application name>", "auth-key": "<application authentication key>", "auth-token": "<application authentication token>" }
var Client = require("ibmiotf"); var appClientConfig = require("./application.json"); var appClient = new Client.IotfApplication(appClientConfig); appClient.connect(); appClient.on("connect", function () { console.log("connected"); });
- The application can be tested by running the
npm start
command:
$ npm start > [email protected] start /sample-application > node . connected
Congratulations, you just created your first application connected to IBM Watson IoT Platform!
- Now, update
index.js
to have the following content:
var Client = require("ibmiotf"); var appClientConfig = require("./application.json"); var appClient = new Client.IotfApplication(appClientConfig); appClient.connect(); appClient.on("connect", function () { appClient.subscribeToDeviceEvents(); }); appClient.on("deviceEvent", function (deviceType, deviceId, payload, topic) { console.log("Device events from : " + deviceType + " : " + deviceId + " with payload : " + payload); });
Now, whenever a device publishes an event, you will get the event printed to stdout
. In the next section, we will create a device to publish the events.
In this section, you'll run through similar steps to create a fake device that connects to IBM Watson IoT Platform and publishes an event.
- From the IoT Platform service created in the setup step, select
Devices
in the menu and then selectAdd Device
. Create a device type namedDeviceSimulator
and fill in theDevice ID
field withDeviceSimulator01
:


Device type | Device ID | Authentication method | Authentication token |
|
- Go back to your preferred IDE and create the project with the same characteristics as the previous application:
npm install ibmiotf --save
- Ensure that your
package.json
file looks like the following:
{ "name": "sample-device", "version": "1.0.0", "description": "Hands-On IoT Solutions with Blockchain - Chapter 1 Device", "main": "index.js", "scripts": { "start": "node .", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Maximiliano Santos", "license": "ISC", "dependencies": { "ibmiotf": "^0.2.41" } }
- Then, create a file named
device.json
with the following content:
{ "org": "<your iot org id>", "type": "DeviceSimulator", "id": "DeviceSimulator01", "auth-method" : "token", "auth-token" : "<device authentication token>" }
var iotf = require("ibmiotf"); var config = require("./device.json"); var deviceClient = new iotf.IotfDevice(config); deviceClient.log.setLevel('debug'); deviceClient.connect(); deviceClient.on('connect', function(){ console.log("connected"); });
- The device simulator can be tested by running the
npm start
command:
$ npm start > [email protected] start /sample-device > node . [BaseClient:connect] Connecting to IoTF with host : ssl://3nr17i.messaging.internetofthings.ibmcloud.co m:8883 and with client id : d:3nr17i:DeviceSimulator:DeviceSimulator01 [DeviceClient:connect] DeviceClient Connected connected
- Now, update the code to send an event with the current timestamp to the IoT Platform service:
var iotf = require("ibmiotf"); var config = require("./device.json"); var deviceClient = new iotf.IotfDevice(config); deviceClient.log.setLevel('debug'); deviceClient.connect(); deviceClient.on('connect', function() { console.log("connected"); setInterval(function function_name () { deviceClient.publish('myevt', 'json', '{"value":' + new Date() +'}', 2); },2000); });
Device Event from :: DeviceSimulator : DeviceSimulator01 of event myevt with payload : {"value":Sun May 20 2018 21:55:19 GMT-0300 (-03)} Device Event from :: DeviceSimulator : DeviceSimulator01 of event myevt with payload : {"value":Sun May 20 2018 21:55:21 GMT-0300 (-03)} Device Event from :: DeviceSimulator : DeviceSimulator01 of event myevt with payload : {"value":Sun May 20 2018 21:55:23 GMT-0300 (-03)} Device Event from :: DeviceSimulator : DeviceSimulator01 of event myevt with payload : {"value":Sun May 20 2018 21:55:25 GMT-0300 (-03)}
Congratulations again, your device simulator is now publishing events and your application is receiving them!