2021 Call for Code Awards: Live from New York, with SNL’s Colin Jost! Learn more

Create an Internet of Things Platform starter application

In this tutorial, learn how to create a Node-RED and Watson IoT Platform starter application that can be used to connect IoT devices and simulated IoT devices to the Watson IoT Platform. This combination of services will be assembled to work together, including a Node-RED Node.js web server, a Cloudant database to store the sensor data, and the IoT Platform service, so that you can easily connect IoT devices.

Prerequisites

To complete this tutorial, you need an IBM Cloud account.

This tutorial requires an IBM Cloud Pay-As-You-Go account. To upgrade your Lite account, go to your account settings. In the Account Upgrade section, click Add credit card to upgrade to a Pay-As-You-Go account, or click Upgrade for a Subscription account. See Upgrading your account for more information.

This tutorial provides instructions on deploying the app to IBM Cloud Code Engine, which is a fully managed, serverless platform that runs your containerized workloads and manages the underlying infrastructure for you. IBM Cloud Code Engine provides 100000 vCPU seconds per month at no charge. Your Node-RED flow will often scale to 0, which means that you won’t incur any charges for light to moderate usage. Review your consumption and confirm your billing on a regular basis.

Estimated time

You can complete this tutorial in less than 30 minutes.

Steps

  1. Create a Node-RED starter application
  2. Create an Internet of Things Platform service
  3. Enable the Continuous Delivery feature
  4. Open the Node-RED visual programming editor
  5. Configure your Node-RED application
  6. Add extra nodes to your Node-RED palette
  7. Launch the Watson IoT Platform

Step 1. Create a Node-RED starter application

Follow these steps to create a Node-RED starter application in the IBM Cloud.

  1. Log in to IBM Cloud.

  2. Open the catalog and search for node-red.

  3. Click on the Node-RED App tile.

    This will show you an overview of the Starter Kit and what it provides.

  4. On the Create tab, a randomly generated App name will be suggested. Either accept that default name or provide a unique name for your application. This will become part of the application URL.

    Note: If the name is not unique, you will see an error message and you must enter a different name before you can continue.

  5. The Node-RED starter application requires an instance of the Cloudant database service with IBM Cloud IAM and Cloudant credentials to store your application flow configuration. Select the region the service should be created in and what pricing plan it should use.

    Note: You can only have one Cloudant instance using the Cloudant Lite plan. If you have already got an instance, you will be able to select it from the Pricing plan select box. You can have more than one Node-RED starter application using the same Cloudant service instance.

  6. Click the Create button to continue. This will create your application and, if necessary a Cloudant database service instance, but it is not yet deployed to IBM Cloud.

Step 2. Create an Internet of Things Platform service

The next step is to create an Internet of Things Platform service instance.

  1. Click on the Create service button.

    Additional Service

  2. Scroll down in the list of services to the Internet of Things card.

  3. Select the Internet of Things card and click on the Next button.

    Add IoTP Service

  4. Select the Internet of Things Platform card and click on the Next button.

    Select IoTP Service

  5. Select the region (try to match the region you selected previously), and then click Done.

    Select IoTP Service

Step 3. Enable the Continuous Delivery feature

At this point, you have created the application and the resources it requires, but you have not deployed it anywhere to run. This step shows how to setup the Continuous Delivery feature that will deploy your application into the Code Engine space of IBM Cloud.

  1. Click the Deploy your app button to enable the Continuous Delivery feature for your application.

    Deploy App

  2. On the next screen, click the Code Engine tile.

  3. Scroll down after selecting the Code Engine tile. You will need to create an IBM Cloud API key to allow the deployment process to access your resources. Click the New button to create the key. A message dialog will appear. You can accept the default values and confirm to close the dialog.

  4. Select the Region and Container registry region to deploy your application to. This should match the region you created your Cloudant instance in.

  5. Provide a unique Project name or accept the default ‘project-name’. Click Next to continue.

  6. Configure the DevOps toolchain by selecting the region it should be created in. Again, try to match the region you selected previously.

    Click Create. This will take you back to the application details page. The application details page gives you a useful overview of your application including details of its external URL and the additional services it is connected to. Bookmark this page as you’ll need to come back to it later.

  7. After a few moments, the Deployment Automation section will refresh with the details of your newly created Delivery Pipeline. The Status field of the pipeline will eventually show In progress. That means your application is being built and deployed.

  8. The Deploy stage will take a few minutes to complete. You can click on the ci-pipeline Status link to check the progress of the Delivery Pipeline. Eventually the Deploy stage will display a green checkmark and a Success message to show it has passed. This means your Node-RED starter application is now running.

Step 4. Open the Node-RED application

Now that you’ve deployed your Node-RED application, let’s open it up! You may have to refresh your page.

On the application details page, you should now see the App URL, Source and Deployment target fields filled in.

Application details page for Node-RED

Click on the App URL to open up your Node-RED application in a new browser tab.

Step 5. Configure your Node-RED application

The first time you open your Node-RED app, you’ll need to configure it and set up security.

  1. A new browser tab will open with the Node-RED start page.

    Configure Node-RED app

  2. On the initial screen, click Next to continue.

  3. Secure your Node-RED editor by providing a username and password. If you need to change these at any point, you can either edit the values in the Cloudant database, or override them using environment variables. The documentation on nodered.org describes how to do this. Click Next to continue.

  4. The final screen summarizes the options you’ve made and highlights the environment variables you can use to change the options in the future. Click Finish to proceed.

  5. Node-RED will save your changes and then load the main application. From here you can click the Go to your Node-RED flow editor button to open the editor.

    Button to launch the Node-RED flow editor

    The Node-RED editor opens showing the default flow.

    Default flow in the Node-RED flow editor

Step 6. Add extra nodes to your Node-RED palette

Node-RED provides the palette manager feature that allows you to install additional nodes directly from the browser-based editor. This is convenient for trying nodes out, but it can cause issues due to the limited memory of the default Node-RED starter application.

The recommended approach is to edit your application’s package.json file to include the additional node modules and then redeploy the application.

This step shows how to do that in order to add the node-red-contrib-scx-ibmiotapp module to easily receive IoT data into your Node-RED flow.

  1. On your application’s details page, click Source url. This will take you to a git repository where you can edit the application source code from your browser.

  2. Scroll down the list of files and click on package.json. This file lists the module dependencies of your application.

    In list of files, find the package.json file

  3. Click the Edit button

    Edit button for package.json file

  4. Add the following entry to the top of the dependencies section (1):

     "node-red-contrib-scx-ibmiotapp": "0.x",
    

    Note: Do not forget the comma (,) at the end of the line to separate it from the next entry.

    Add a Commit message (2) and click Commit changes (3)

    Editing package.json file

  5. At this point, the Continuous Delivery pipeline will automatically run to build and deploy that change into your application. If you view the Delivery Pipeline you can watch its progress. The Build section shows you the last commit made and the Deploy section shows the progress of redeploying the application.

    Status of Node-RED starter app in continuous delivery pipeline

  6. Once the Deploy stage completes, your application will have restarted and now have the node-red-contrib-scx-ibmiotapp nodes preinstalled.

Step 7. Launch the Watson IoT Platform

Now that you’ve configured the services and setup the Node-RED Visual Programming Editor, you can launch the Watson IoT Platform.

  1. Return to the IBM Cloud Application Details page.

  2. In the Services section, for the Internet of Things Platform service, click the Open Dashboard link.

    App Overview page

  3. The Internet of Things service landing page opens. From the Manage page, click the Launch button.

    IoT Platform Landing Page

The Watson IoT Platform service opens in a new browser tab. You can now create an IoT device.

IoT Platform Device Page

Summary

Congratulations! You have now created a Node-RED and Watson Internet of Things Platform starter application that is hosted in the IBM Cloud. You have also learned how to edit the application source code and automatically deploy changes.