Creating a device

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.

  1. From the IoT Platform service created in the setup step, select Devices in the menu and then select Add Device. Create a device type named DeviceSimulator and fill in the Device ID field with DeviceSimulator01:
  1. Since it's only a simulator, just click on Next until you reach the end of the wizard: 
  1. Note the device credentials generated, in the following format:

 

  1. Go back to your preferred IDE and create the project with the same characteristics as the previous application:
npm install ibmiotf --save
  1. 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"
}
}
  1. 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>"
}
  1. Create a file named index.js and add the following content:
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");
});
  1. The device simulator can be tested by running the npm start command:
$ npm start
> sample-device@1.0.0 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
  1. 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);
});
  1. Run npm start again and every two seconds the device will send an event to the Watson IoT Platform. You can check the logs of the application to see whether it has received the events, like so:
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!