Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Hands-On IoT Solutions with Blockchain

You're reading from   Hands-On IoT Solutions with Blockchain Discover how converging IoT and blockchain can help you build effective solutions

Arrow left icon
Product type Paperback
Published in Jan 2019
Publisher Packt
ISBN-13 9781789132243
Length 206 pages
Edition 1st Edition
Concepts
Arrow right icon
Authors (2):
Arrow left icon
Maximiliano Santos Maximiliano Santos
Author Profile Icon Maximiliano Santos
Maximiliano Santos
Enio Moura Enio Moura
Author Profile Icon Enio Moura
Enio Moura
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Understanding IoT and Developing Devices on the IBM Watson IoT Platform FREE CHAPTER 2. Creating Your First IoT Solution 3. Explaining Blockchain Technology and Working with Hyperledger 4. Creating Your Own Blockchain Network 5. Addressing Food Safety - Building around the Blockchain 6. Designing the Solution Architecture 7. Creating Your Blockchain and IoT Solution 8. The IoT, Blockchain, and Industry 4.0 9. Best Practices for Developing Blockchain and IoT Solutions 10. Other Books You May Enjoy

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:

Select Internet 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 Key and Authentication 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
> sample-application@1.0.0 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 Next until 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
> 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!

You have been reading a chapter from
Hands-On IoT Solutions with Blockchain
Published in: Jan 2019
Publisher: Packt
ISBN-13: 9781789132243
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image