Taxonomy Icon

IoT

With the Node RED service you can easily connect your IoT devices over Watson IoT Platform. What makes it easy? Node-RED provides a GUI with programmable buttons. We can create REST APIs and all our business logic with just a few clicks. With basic knowledge of JavaScript you can perform the bulk of the necessary app logic, like collecting data from IoT devices and manipulating these values in Node-RED.

When we’ve completed our application in Node-RED, we can then publish it and use API Connect to provide us with higher level REST API functionality. API Connect provides the following functionality:

  • Categorize APIs
  • Limit APIs with certain restrictions
  • Allows identifying different roles on APIs
  • Allows managing different web services together
  • Allows API versioning

Below is a creenshot of a API Connect UI:

API Connect

Learning objectives

This tutorial will demonstrate how to:

  • Connect devices from the Watson Internet of Things Platform to Node-RED
  • Perform an insert operation to a database with Node-RED
  • Create a REST API with Node-RED
  • Serve a Node-RED REST API with API Connect

Prerequisites

In order to use this how-to, the reader will need the following prerequisites:

Estimated time

  • Set up of the Watson IoT Platform is approximately 10 minutes
  • Set up of the Node-RED is approximately 10 minutes
  • Set up of the API Connect is approximately 10 minutes
  • Understanding how to use Node-RED with API Connect is approximately 15 minutes

Steps

Creating Node-RED flows

The easiest way to deploy Node-RED flows is to import a JSON file that contains flows to your Node-RED palette. But first we need to install a few packages. After creating a Node-RED instance we first try to install a node into our palette. You can install nodes with the “Manage Palette” button.

Manage Palette

When clicking the “Manage Palette” button, we first go to the “Install” section and search for “watson”. Then install both the node-red-contrib-ibm-watson-iot and node-red-contrib-ibm-wiotp-devices-ops packages.

Watson

We can now import flows into our editor. Dowload the allFlows.json file and copy the content of the file to clipboard then open your Node-RED palette. You will find the import section under the hamburger menu which is placed in the upper right corner. Click the clipboard button to import all the flows from the JSON file.

Importing flows

Before proceeding we will delete Flow 1, double click on the Flow 1 tab and use the “Delete” button to remove it.

Deleting Flow

Before continuing we need to configure the IoT nodes according to your service credentials. If you’re not sure how to create an API key and token for the IoT service, follow this How-to to find out how.

After creating your service credentials you need to open one of the IoT nodes and configure it with these values. See the screenshow below as an example.

iot-config

Be sure to change the authentication method to API Key if it is not already set. Then click to pencil icon next to API Key section and enter your API key and token. After entering these credentials all of your IoT nodes will be properly configured.

iot-config-2

Now let’s dig into the details for each flow, there are four different flows in the editor:

  • At the Mock Data to Watson flow, you are using a function to mock data instead of a real IoT device, and publish it to Watson IoT Platform.
  • At the Data From Watson IoT flow, you are connecting to Watson IoT for reading sensor data on Node-RED.
  • At the Watson IoT to Database flow, you are adding database to second flow.
  • At the REST API flow, you are opening your application database as a REST API.

Deep dive into Node-RED flows

Mock Data to Watson

In this flow we are using the API key authentication method for communicating with the IoT Platform. With this method you can connect to any IoT Platform, it doesn’t need to be in your IBM Cloud space.

Data From Watson IoT

In this flow we are subscribing to data from Watson IoT Platform. Just like in the previous step, we can use an API key.

Watson IoT to Database

The only difference between this flow and the previous is the database portion. Here we can use an existing Cloudant Database, which was created when creating the Node-RED instance, or we can connect to an external database. For the purposes of this how-to we will use the existing Cloudant database.

REST API

In this flow we see the HTTP input button which creates an HTTP endpoint for creating a web service and HTTP output which creates a response for your web service. When getting a request for the /sensor-data API we use the database button to get all sensor information from the database and then serve the data over the HTTP response button.

Testing Node-RED

After finishing the configuration we now need to test everything out. Click the red “Deploy” button which is placed at the upper right corner. You shouldn’t see any errors, but if there is an unexpected error you can see from your debug page which is placed at right part of the editor, next to “Info” section. If there are no errors click the little square next to interval button, this will launch the debug page. See the screenshot below as an example, the values in your debugger will be different.

interval

At this point, we can send messages to the IoT Platform and read it from Node-RED successfully. With this simple click you just tested three flow which are “Mock Data to Watson IoT”, “Data From Watson IoT” and “Watson IoT to Database”. Now we need to test our REST API flow. Copy your Node-RED instance url into a browser and add /sensor-data to at the end of it. The URL should look like this: cagataytanyildiz.mybluemix.net/sensor-data. We are expecting to see a JSON blob from the database. If everything is correct we can go through the API Connect part below.

Configuring the catalog with API Connect

API Connect has a hierarchy for collecting, creating and serving APIs publicly. See the figure below to understand how API Connect is structured. For this how-to, we will be focusing on the “Catalog” to serve our APIs.

API Connect Hierarchy

Let’s launch the API Connect instance from the IBM Cloud dashboard.

api-1

Once the API Connect UI is launched use the >> button, which placed next to the Dashboard button, then go to Drafts and select the API tab.

api-2

Click the ADD button, but before creating an API expand the Additional Properties and select Identify Using with None under the Security section.

apinew

Once completed, go to the Paths section and add a new /sensor-data path for your web service.

path

Now we need to bind our web service to an API. For this, go to the Assemble section and click on the invoke diagram. When you click the invoke diagram, you need to enter your web service url (for example, https://cagataytanyildiz-node-red.mybluemix.net/sensor-data) and you select request type, which is GET, for this scenario. Lastly set Cache Type to No Cache.

invoke

After adding the service to your API we need to “Save” and Validate the API design. Click the Save button on the right side of the page. After getting a success message, click on the Validate button which is placed next to the Save button. We do not expect any error message in this section. After preparing the API we need to prepare the Product as well, let’s go back to the Drafts page, but this time we will add a Product.

product-2

product-1

Give the Product a name and and title. Once complete, click the Save button located on theright side of the page. Then go to Design section and click the Save button first the Validate button. We do not expect any error messages in this section.

Now we need to add our API to our Product. Go to the catalog and click onto the newly created product. Navigate to the API section and bind your product with your API, to do so, click on the Add API button which is placed right upper corner of the API section and select the saved API.

productapi

After that, click the Save button and then Stage button. Staging will take a few minutes. Once completed, we can publish it by going to the Dashboard. To do so, click on the >> button and select Dashboard, then the Sandbox catalog. Now select the staged product and publish it using the three dots menu item.

pubprod

Testing our API Connect configuration

We can now test our API. Go back to your API Page through the Drafts section and open the Assemble section. Click the play button which placed next to search bar.

republish

You should be able to see your path under the Operation dropdown. Select the GET operation. The returned status code should be a 200 OK, this indicates everything was completed successfully. Congratulations!

Troubleshooting

  • Seeing a -1 error? Please make sure that you published your product in the API Connect catalog.

cors

Summary

Using API Connect and Node-RED we were able to easily publish an application that used the Watson IoT Platform with little to no coding. Furthermore, it’s possible to use API Connect to connect different services together, not just Node-RED or the Watson IoT Platform. We encourage the reader to continue to read more about the API Connect and Node-Red services.

In order to learn more about Node-RED and API Connect, check out the following resources.