Book Image

Hands-On IoT Solutions with Blockchain

By : Maximiliano Santos, Enio Moura
Book Image

Hands-On IoT Solutions with Blockchain

By: Maximiliano Santos, Enio Moura

Overview of this book

Blockchain has been the hot topic of late thanks to cryptocurrencies. To make matters more interesting, the financial market is looking for ways to reduce operational costs and generate new business models, and this is where blockchain solutions come into the picture. In addition to this, with Internet of Things (IoT) trending and Arduino, Raspberry Pi, and other devices flooding the market, you can now create cheap devices even at home. Hands-On IoT Solutions with Blockchain starts with an overview of IoT concepts in the current business scenario. It then helps you develop your own device on the IBM Watson IoT platform and create your fi rst IoT solution using Watson and Intel Edison.Once you are familiar with IoT, you will learn about Blockchain technology and its use cases. You will also work with the Hyperledger framework and develop your own Blockchain network. As you progress through the chapters, you'll work with problem statements and learn how to design your solution architecture so that you can create your own integrated Blockchain and IoT solution. The next set of chapters will explain how to implement end-to-end Blockchain solutions with IoT using the IBM Cloud platform. By the end of this book, you will have mastered the convergence of IoT and Blockchain technology and exploited the best practices and drivers to develop a bulletproof integrated solution.
Table of Contents (15 chapters)
Title Page
About Packt
Contributors
Preface
Index

Creating your first IoT solution


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.

Creating a gateway

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 PlatformNow, 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

Creating an application means that you're allowing an actual application or service to connect to a specific Watson IoT Platform organization:

  1. 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 the Description field with Hands-On IoT Solutions with Blockchain - Chapter 1 App. Finally, click on Next:
  1. Select the Standard Application role and click on Generate Key.You will get an API KeyandAuthentication Token. Make a note of these in a table format, like the one that follows, as you'll need them to connect to your application:

API key

Authentication token

  1. Next, open the IDE of your preference, create a new Node.js project, and install the ibmiotf dependency package:
npm install ibmiotf --save
  1. 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"
  }
}
  1. 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>"
}
  1. Create a file named index.js and add the following content:
var Client = require("ibmiotf");
var appClientConfig = require("./application.json");

var appClient = new Client.IotfApplication(appClientConfig);

appClient.connect();

appClient.on("connect", function () {
  console.log("connected");
});
  1. 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!

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

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 Nextuntil you reach the end of the wizard: 
  1. Note the device credentials generated, in the following format:

Device type

Device ID

Authentication method

Authentication token

 

 

  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
> [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
  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!