Using event filtering to limit the data in the blockchain
To reduce the number of Watson IoT Platform Service events that are written to your blockchain, you can configure your logical interface to only send event data on state changes.
When using blockchain with the Internet of Things, writing all device data points to the blockchain is of limited value because the majority of events typically are identical or very similar. By filtering events to send only state changes when the device data changes, blockchain traffic can be reduced so that only the important data is isolated in the ledger.
About this task
In this task, a device type logical interface is configured to send state change notifications.
In addition, we cover the steps to deploy and configure a Node-RED flow to listen for these state changes and to send them to IoT Blockchain Service.
Before you begin
- Identify the logical interface for the device type for which you want to filter events.
- Verify that you have a Node-RED instance.
Deploy a new instance or use an existing instance. Node-RED can run locally, or you can deploy it on IBM Cloud.
Step 1: Configure the Watson IoT Platform Service logical interfaces
IBM Watson™ IoT Platform Service is used to register the IoT devices and set up the logical interface state change notifications that are used to filter the events.
If you already have your logical interface configured to not send notifications or to send notifications for all events:
- In the Watson IoT Platform Service dashboard, select Devices > Device types.
- Select the device type to modify.
- View the device type information and select Interface.
- Click the Edit icon.
- Click the Edit icon for the logical interface that you want to edit.
- Select Notification Preference.
- Select For State Changes to receive notifications only for state changes.
- Click Done.
- Click Activate and then click Done.
If you are setting up a new logical interface:
- In the Watson IoT Platform Service dashboard, select Devices > Device types.
- Create a physical interface for your device type.
- Select the device type to modify.
- View the device type information and click Interface.
- Click Advanced Flow to open the Advanced Flow Creator.
- Select Create Physical Interface and type a name and description of the physical interface. Click Next.
- Click Create Event Type and select the last cached event, an event from the list, or add an event manually and then click Add.
Important: If you are using Hyperledger Composer, you must change the$classattribute toclass. - Click Done.
- Create a logical interface for your device type.
- Select +Create Logical Interface.
- Enter a name, alias, and description for the logical interface.
Important: Record the interface alias that you set for the logical interface. You will need this when you configure the<alias>value in the Node-RED flow together with<device_type>and<device_id>. - Click Add Property and select the properties from the physical interface you created.
- Click Next.
- Select For State Changes to receive notifications only for state changes.
- Click Done.
- Click Activate and then click Done.
Step 2: Set up the Node-RED Flow
The Node-RED flow is used to connect to Watson IoT Platform Service and receive data from the registered IoT device.
We use the MQTT node to receive the notifications and to trigger a transaction to IBM® Watson™ - Blockchain Service with the IoT device event.
- Create an API key that has the Device Application role.
For more information on this step, see Connecting Applications.
To create a new API key and authentication token pair:- In the Watson IoT Platform Service dashboard, from the main navigation menu, click Apps and then select API Keys.
- Click Generate API Key.
- Add a description to identify the API key in the dashboard, for example: Key to connect my application.
- Click Next.
- Select the Device Application role for the key.
- Click Generate Key.
Important: Make a note of the API key and token pair. You will need this information later when you configure the Node-RED flow. Also, authentication tokens are non-recoverable. If you lose or forget this token, you will need to re-register the API key to generate a new authentication token.
- Deploy the Node-RED flow.
- Download the Event Filtering Node-RED flow source from Github.
- Open your Node-RED flow editor.
- From the Node-RED menu, select Import>Clipboard and paste the Event Filtering Node-RED flow source.
- Click Import.
- Configure the Listen for State Changes node.
This node filters the events from the IoT device. By connecting to the device type interface that was created with state change notifications, IoT device data is received only when a state change occurs.- Open the node for editing and click the edit icon next to the Server option.
- In the Server field, replace
<org>with your Watson IoT Platform Service organization ID. - In the Client ID field, replace
<org>with your Watson IoT Platform Service organization ID. - Select Security.
- In the Username field, enter the API key.
- In the Password field, enter the authentication token.
- Click Update.
- In the Topic field, replace
<device_type>,<device_id>, and interface<alias>with the values that you recorded in the previous steps. - Click Done.
- Configure the Post to IoT-BC node.
This node sends the IoT device events to your route in the IoT Blockchain Service and then to the blockchain.- Select the node and edit the URL field for your IoT Blockchain Service route by replacing the
<org>,<device_type>, and<event_id>entries with the corresponding values for your route.
Example:https://iot-blockchain.ibm.com/api/v1/<org>/tx/<device_type>/UNUSED/<event_id> - Select Use basic authentication.
- In the Username field, enter
use-token-auth. - In the password fields, enter your IoT Blockchain Service API key
- Click Done.
- Select the node and edit the URL field for your IoT Blockchain Service route by replacing the
- Deploy the configured Node-RED flow.
Congratulations, you are now filtering your events by using Watson IoT Platform Service and Node-RED!