Using REST and IBM Watson™ IoT Platform Service with IoT Blockchain Service
In the following topic we go over the steps to deploy IBM Watson™ IoT Platform Service, import and deploy a supply chain sample IoT contract, use Postman to manually configure IoT Blockchain Service with the IBM-provided postman collection, set up Node-RED to send device data to Watson™ IoT Platform Service and finally configure Watson™ IoT Platform Service to store your IoT device data in a blockchain.
Before you begin
Before you start you must complete the steps in the IoT Blockchain Service quick start guide, and verify that your blockchain transactions show up in IBM Blockchain Platform.
Step 1: Deploy or install the required supporting software
- Installed as part of this getting started example:
-
- Postman
- Postman
- Previously deployed as part of IoT Blockchain Service quick start
-
- IBM Blockchain Platform
- Hyperledger Composer CLI
- Node-RED
- Required for this getting started example:
-
- Watson IoT Platform Service
Use an existing Watson IoT Platform Service organization, for example the instance that might be included with IBM® Watson™ IoT Platform, or deploy a new instance.
Tip: You can create an Watson IoT Platform Service instance directly from the Watson IoT Platform Service page in the IBM Cloud Services Catalog. For more information, see the Getting started with Watson IoT Platform Service topic in the Watson IoT Platform Service documentation.
- Watson IoT Platform Service
Note: In the following diagram the label IoT Blockchain Service identifies the IoT Blockchain Service service component.
Step 2: Get started with IoT Blockchain Service and Watson™ IoT Platform Service
Configure IoT Blockchain Service and Watson™ IoT Platform Service. With that done, use the Node-RED device simulator to writing device data to blockchain.
-
Utilize the sample contract previously deployed and locate the admin@iot-supplychain-network card you created as part of IoT Blockchain Service quick start.
-
Create a fabric definition in IoT Blockchain Service.
Import a Hyperledger Composer Business Network Card to create an IoT Blockchain Service fabric definition for IoT Supply Chain Business Network.- Go to the IoT Blockchain Service login page at: https://iot-blockchain.ibm.com/#.
- Click Launch and log in with your IBMid. This action takes you to the IoT Blockchain Service user interface.
- In the Organization and Blockchain Routes tile, click Manage Fabric Definitions to work with your fabric and route definitions.
- Click Select a Composer ID Card and import the Hyperledger Composer Business Network Card that you downloaded earlier.
- Provide any route name, any short description, and provide the fabric name
k8sFabric. - Select the Composer version you are using.
- If you selected Composer v0.16.x enter the public IP address. If you selected Composer v0.19.x or Composer v0.20.x skip this step.
- Select
No Notificationas you will not need IoT Blockchain Service notifications for this exercise. - Click Create New Route.
-
Configure IoT Blockchain Service via Postman.
Use Postman to manually configure IoT Blockchain Service with the IBM®-provided postman collection.
Important: To access the IoT Blockchain Service API with Postman, use Basic Auth with the fixed user nameuse-token-authand the API key that you created as part of the IoT Blockchain Service quick start as password.-
Set up Postman.
Download and install Postman at: https://www.getpostman.com. -
Import the Postman collection.
- Download the IoTBC_Customer.postman_collection.json collection file from Github.
- In Postman, click Import and then import the file that you just downloaded.
-
Create a Postman environment.
- In Postman, select Environment options > Manage Environments, then click Add.
- Add a descriptive environment name, such as
Blockchain. - Click Bulk Edit.
- Paste the following sample and replace
<organization_id>and<your_api_key>with the information for your environment:iotbc_url:https://iot-blockchain.ibm.com org:<organization_id> user:use-token-auth api:api/v1/{{org}} pass:<your_organization_api_key> - Click Add to create the environment.
-
Display your IoT Blockchain Service fabric definition.
Use Postman to list the IoT Blockchain Service fabric definition that you created when you loaded your Hyperledger Composer ID card. This fabric definition lets IoT Blockchain Service connect to your deployed Hyperledger Fabric instance.- To check your submission, select the
GET all fabricsREST call under the Fabrics section of the Postman Collections. - Click Send to get a list of all fabrics.
- To check your submission, select the
-
Configure IoT Blockchain Service route definitions.
Use Postman to create a route definition in IoT Blockchain Service to map your data from your IoT device or application to your deployed contract. Because you are using the IoT Blockchain Service supply chain IoT sample contract (iot-supplychain-network), you can use thePOST new route iot-device-createandPOST new route iot-device-readingREST calls under the Routes section without changes.- Select
POST new route iot-device-createand click Send to submit the route to IoT Blockchain Service. - Select
POST new route iot-device-readingand click Send to submit the route to IoT Blockchain Service. - To check your submission, select the
GET all routesREST call under the Routes section of the Postman Collections. - Click Send to get a list of all routes.
- Select
-
-
Create a device type, device and API key in Watson™ IoT Platform Service.
- Open your Watson™ IoT Platform Service dashboard.
- Create a device type and register your IoT device with Watson™ IoT Platform Service.
For more details on this step, see Connecting Devices in the Watson™ IoT Platform Service documentation.
Important: Record the device type, device ID and device token for your registered device. You will need this information later when you configure your Node-RED device simulator.
Use the following device information for this example:- Device type:
iotsensor_device - Device ID:
iotsensor
- Device type:
- Create an API key with the
Device Applicationrole.
For more details on this step, see Connecting Applications in the Watson™ IoT Platform Service documentation.
Important: Record the API key and API token. You will need this information later when you configure your Node-RED MQTT Listener.
-
Configure Node-RED to send simulated device data to Watson™ IoT Platform Service.
- Navigate to the Node-RED flow in Github.
- Select and copy all the text.
- Open the Node-RED flow editor.
- From the Node-RED menu, select Import > Clipboard and paste the selected text then click Import.
- In the flow, select the Post to IoT Blockchain node and replace
<your_org>in the URL with your IoT Blockchain Service org ID. - In the flow, double-click the Publish to IoT node.
- Click the edit icon next to the Server field.
- Update the Server name to correspond to your IoT Platform organization.
{org_ID}.messaging.internetofthings.ibmcloud.com - Make sure Enable secure (SSL/TLS) connection is checked. Where {org_ID} is the six character ID of your Watson™ IoT Platform Service organization.
- Update the Client ID with your organization ID, device type, and device ID:
d:<organization_id>:iotsensor_device:iotsensor - Select the Security tab.
- Verify that the Username field has the value
use-token-auth. - Update the Password field with the Watson™ IoT Platform Service device token that you saved earlier.
- Click Update and then OK.
- Deploy the updated Node-RED flow.
Important: If you edit the Security tab settings in the future, you must provide the Authentication Token again. Don't assume that the*******value that you see is the token that you previously provided. If you click Update without correcting this field, your Node-RED flow will fail. If you open this node to view the information, be sure to click Cancel.
-
Configure Node-RED to listen for IoT device messages from Watson™ IoT Platform Service and send those messages to IoT Blockchain Service.
- Navigate to the Node-RED flow in Github.
- Select and copy all the text.
- Open the Node-RED flow editor.
- From the Node-RED menu, select Import > Clipboard and paste the selected text then click Import.
- In the flow, edit the Post to IoT Blockchain node and replace
<your_org>entry in the URL field with your IoT Blockchain Service org ID. - Select to use basic authentication.
- Set the Username field to the value
use-token-auth. - Update the Password field with the IoT Blockchain Service API key that you recorded earlier.
- Make sure Enable secure (SSL/TLS) connection is checked.
- Click Done.
- In the flow, edit the IBM IoT MQTT Listener node.
- Click the edit icon next to the API Key field.
- Provide your Watson™ IoT Platform Service API key in the API Key field.
- Provide your Watson™ IoT Platform Service authentication token in the API Token field.
- Replace the
<your_org>entry in the Server-Name field with your Watson™ IoT Platform Service org ID.
Important: If you edit the Post to IoT Blockchain or IBM IoT MQTT Listener nodes in the future, you must provide the API Key or API Token again. Don't assume that the*******value that you see is the API key or token that you previously provided. If you click Update without correcting these fields, your Node-RED flow will fail. If you open these nodes to view the information, be sure to click Cancel.
-
Send a transaction to IoT Blockchain Service from Node-RED.
- Open the Node-RED flow editor.
- Click the injector button in your Create Device iotsensor node to send a CreateIoTDevice message to the blockchain.
Tip: A device only needs to be created once in Hyperledger Composer. - Click the injector button in your Send Device Reading iotsensor node to send an
IoTDeviceReadingmessage to the blockchain.
-
View your data in the blockchain.
View the blockchain from the IBM Blockchain Platform Monitor application in IBM Cloud to see that a new transaction was added to the blockchain.- Open your IBM Blockchain Platform Starter or Enterprise service instance in IBM Cloud and start the Monitor app.
- Navigate to Channels then double-click on the channel you created when you deployed the composer contract.
- View the details of the latest block in the blockchain.
Congratulations, your IoT Blockchain Service and Watson™ IoT Platform Service getting started environment is now up and running! Happy blockchaining!