- Hands-On IoT Solutions with Blockchain
- Maximiliano Santos Enio Moura
- 362字
- 2021-07-02 14:27:35
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:
- 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:

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

- 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>"
}
- 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");
});
- 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!
- 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.
推薦閱讀
- 西去東來(lái):沿絲綢之路數(shù)學(xué)知識(shí)的傳播與交流
- 奇妙數(shù)學(xué)史:數(shù)字與生活
- 數(shù)學(xué)建模:算法與編程實(shí)現(xiàn)
- 小數(shù)學(xué)家應(yīng)該知道的數(shù)學(xué)故事
- Foundations of Blockchain
- 這才是好看的數(shù)學(xué)
- 好玩的數(shù)學(xué)符號(hào)
- 一定要懂博弈論
- 現(xiàn)代數(shù)值計(jì)算(第2版)
- 魔方的思維世界
- Abaqus有限元分析從入門到精通(2022版)
- 10堂極簡(jiǎn)概率課
- 微積分Ⅱ
- 不可思議的自然對(duì)數(shù)
- 給孩子的數(shù)學(xué)思維課