02 Lab3 IoT Cloud.v2.2
02 Lab3 IoT Cloud.v2.2
{ Lab 3 }
Agenda
Before Starting
This hands-on required to have an IBM Cloud account. If you don’t, you can create
one here: http://bluemix.net/.
• If needed, free resources (GB / #Services) in your IBM Cloud Organization & Spaces to run
the lab exercises.
If you encounter a resource contention (error message saying you are out of resources),
clean up your spaces by deleting existing Apps or Services.
1. Hands-on presentation
Section 1. Overview
In this hands-on session, you will create a Node-RED application in IBM Cloud to collect, store
and display virtual sensor data.
• Your Node-RED app is online (reachable from the Internet), & will be connected to a
temperature simulator (sensor)
• Optionally, you are able to provide 2 dashboards: one with voice alert implemented in
Node-RED, the second designed and hosted in Watson IoT Platform
Section 2. Prerequisites
The required file used during this lab can be downloaded the JSON files from
• http://ibmcloud-watson-day.mybluemix.net/files/Lab3_IoT_Flow.v2.1.json
• http://ibmcloud-watson-day.mybluemix.net/files/Lab3_IoT_Dashboard.v2.1.json
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 Cloud Foundry space of IBM Cloud.
__ 2. On the next screen, click the Deploy your app button (1) to enable the Continuous
Delivery feature for your application.
__ 3. You will need to create an IBM Cloud API key to allow the deployment process to
access your resources. Click the New button (1) to create the key. A message dialog
will appear. Read what it says and then confirm and close the dialog.
__ 4. The Node-RED Starter kit only supports deployment to the Cloud Foundry
space of IBM Cloud. Select the region (2) to deploy your application to. This
should match the region you created your Cloudant instance in.
__ 6. Click Create (4). This will take you back to the application details page
__ 7. After a few moments, the Continuous Delivery section will refresh with the
details of your newly created Toolchain. The Status field of the Delivery Pipeline
will show In progress. That means your application is still being built and
deployed.
__ 8. Open your IBM Cloud Resource list by selecting the sidebar menu (1) and then
selecting Resource List (2)
__ 9. You will see your newly created Node-RED Application listed under the Apps section
(1). You will also see a corresponding entry under the Cloud Foundry apps section
(2). Click on this Cloud Foundry app entry to go to your deployed application’s details
page.
__ 10. From the details page, click the Visit App URL link to access your Node-RED Starter
application.
__ 11. Run the wizard to configure authentication: secure your editor with your own credentials
so only authorized users can access it (Node-RED has its own authentication system).
Don’t check Allow anyone to view the editor, but not make any changes and Allow
anyone to view the editor.
__ 13. Node-RED is a browser-based editor that makes it easy to wire together flows that can
be deployed to the runtime. In the case of IoT, Node-RED is really powerful to quickly
test all the possibilities that IBM Cloud offers with different kind of services.
Your Node-RED app has a public URL like any web app (you defined it in step 2).
Click on Go to your Node-RED flow editor and use the credentials provided before.
The simulator (from IBM Cloud IoT Quickstart) connects automatically and starts
publishing data.
It must remain connected to visualize the data.
Use the simulator buttons to change the simulated sensor readings. Data is published
periodically.
Note: Instead of using your desktop browser, you can use your smartphone.
__ 3. Identify your virtual device ID (top right corner) : copy it. You will use it in next section.
Warning: if you reload this page, the device ID changes.
__ 4. Click Done & deploy your flow by clicking the Deploy button (top right).
__ 5. Check the Debug Panel on the right side while you are playing with the sensor
simulator. You should receive Device (sensor = web app. you opened in other window)
data as the IBM IoT Node subscribed to this particular Device topic.
__ 6. Delete the whole flow by selecting all the nodes & pressing the ‘Delete’ key.
__ 7. Now import a new flow. A flow can be exported and imported using JSON file.
Open link http://ibmcloud-watson-day.mybluemix.net/files/Lab3_IoT_Flow.v2.1.json to
display code in JSON format. You can also open the file you downloaded previously.
__ 1. Add a Cloudant Node (Cloudant OUT node in the Storage Category) & link it to the
temp function node
__ 2. Configure it:
• Service : Cloudant service name bound to your Node.js runtime.
As Node.js is already bound to a Cloudant Service, the service name should appear
in the Drop-down list.
• Database: name of your choice (lower case)
• Name (node): name of your choice
Click Done.
__ 4. From your IBM Cloud Dashboard (IBM Cloud window of your browser), start the
Cloudant dashboard by clicking on the line of Cloudant service
__ 6. Select Database icon in left panel, then your database name (defined in step 2).
__ 7. Have a look to the inserted data in the database. Click on record to see content.
__ 4. Accept the restage step to actually bind the service to the app.
__ 5. While it is restaging (~3 minutes), take a look at Service Credentials. This information
is useful if you want to invoke your Watson Service from any program (running in IBM
Cloud or outside IBM Cloud)
__ 6. Go back to Node-RED window.
If you get a connection error message, your application restaging is not finished. Wait.
Try to refresh page.
__ 3. In the sidebar that appears on the left-hand side of the page, click the Install tab:
__ 6. Click Install
Note: In the current configuration of your Node-RED application, you probably get an error
message
In that case, please use the following procedure to add additional nodes (like node-red-
dashboard) to your Node-Red App:
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-dashboard module.
1. On your application’s details page, click the url in the Continuous Delivery box.
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.
4. Add the following entry to the top of the dependencies section (1):
"node-red-dashboard": "2.x",
"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. This is an example, you have to copy the list of nodes you want to add. Above,
we are adding node-red-dashboard and ibmiotapp nodes.
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 (1) and the Deploy
section shows the progress of redeploying the application (2).
Note: If your Continuous Delivery Service expired, you’ll get a Warning Message. In
that case, go to your Resources List, choose your Node-red Cloud Foundry App,
delete and recreate the Continuous Delivery Service attached to your app as shown
below (Delivery Pipeline part) – specifying Existing Git Repository, unselecting the
Issue Tracking check box.
Once the Deploy stage completes, your application will have restarted and now have
the node-red-dashboard nodes preinstalled.
__ 8. Note also that there is a new dashboard tab in the right-hand sidebar:
TIP: This dashboard tab may be used to add new tabs, menus etc. to the visualization
dashboard. There are also two available themes – light and dark.
2 1
In this section, you will add a voice node allowing your app to tell say message when
temperature change. To do that, you will deploy a new Watson service: Text to Speech.
__ 4. Accept to restage application and wait for your Node-RED application to restart.
2 3
__ 4. Click Launch button to open your Watson IoT organization dashboard in a new
browser tab. You are now connected to the IBM Watson IoT Platform dashboard. With
the platform you can manage your devices, store and access your data.
__ 5. From left panel Settings menu, activate the Last Event Cache feature :
By using the Watson IoT Platform Last Event Cache API, you can retrieve the last
event that was sent by a device. This works whether the device is online or offline,
which allows you to retrieve device status regardless of the device's physical location or
use status. Last event data of a device can be retrieved for any specific event that
occurred up to 7/45 days ago.
__ 6. You are now going to register a new device in your organization: first adding a device
type, then the device. Click on Devices menu from left panel.
Click on Device Type, then Add device Type button
__ 7. Choose Device and put a name (case sensitive) for your device : virtualsensor
Click Next.
You don’t need to add Device Information.
Click Done.
__ 11. For the security part, it is recommended to you to provide a simple token (between 8
and 36 characters long and should contain a mix of lower and upper-case letters,
numbers and symbols). If you skip this, a token will be automatically generated but this
one won’t be easy to use for the next steps of this hands-on.
Click Next.
__ 12. A summary of your device details appears. Copy all these information in a text editor or
as a screenshot. The token is unrecoverable.
Click Done.
__ 13. In the Security menu on the left panel, click Connection Security and change security
settings to accept non-SSL connections : TLS Optional
__ 14. Your device is created. You are now going to update your Node-RED application.
__ 3. Open new node. Name it Format Device Payload and insert following code
Click Done.
__ 4. Add an ibmiot out node and connect it to Format Device Payload node.
Click Done.
__ 6. Click Deploy to deploy your updated Node-RED flow : ibmiot nodes should now be
connected.
__ 3. Click on Sensor1, your previously created device. Click on State to see the last device
status, and last values of your Sensor1 device (remember that Sensor1 receives data
from Node-RED, Node-RED receives data from your IoT sensor Web app).
__ 4. You can also click on Recent events to see the last 5 payloads
__ 5. We are now going to create a simple dashboard using IBM Watson IoT Platform.
From left panel, click on Board.
__ 6. You can see that you already have some card. Have a look at Usage Overview. You
can see cards about devices connected and data transferred.
__ 12. To collect data from your device, select Sensor1. Click Next.
__ 13. To select value to display, click on Connect new data set, and fill form as below.
Click Next.
__ 14. Change the card size to XL (you can change others parameters, if you want).
Click Next.
__ 16. Your new card is available and displays live temperature values from Sensor1
__ 17. Following same steps (from 11 to 16), create 2 cards (gauges) to display Humidity and
Object temperature.
Our recipes make it super easy to get devices connected to our Internet of Things cloud.
Your apps can then use our real-time and REST APIs to communicate with your devices and
consume the data you've set them up to collect.
Click on the IoT Platform service. This is launching the IoT platform console.
Type a name for the Device type : DeviceType1 and a device ID : 100i700
Click Next
Task 8. Summary
Note that every language provides MQTT Libraries for publishing and subscribing to MQTT
events – C , C++, C# , Python, Node.js & javascript, Java. etc.
In this exercise, we will use a Node.js library “ibmiotf” for connecting to our IoT Broker (Watson
IoT service).
We will also use a more generic and lower level library ‘mqtt.js” for publishing events.
mkdir DeviceSimulator
cd DeviceSimulator
cd device-simulator-for-ibm-iot
Edit the .env file with a text editor of your choice (nano, vi or notepad) and Replace all the
values with the one you saved earlier.
vi app.js
npm install
node app.js
At this point you should see this running application (simulating a device) is connected to the
IoT platform service and some data (temperature, pressure and humidity) is being sent to the
platform.
Our device should be connected and you should see some date coming.
Click on the device and scroll the page to the sensor information:
Click on the recent events: to see all the events coming from the simulated device (our
application) :
In the previous section, we used the ‘ibmiotf’ library for subscribing events to a MQTT Broker.
Let’s use a generic library ‘mqtt.js’ , that allows us to directly use the MQTT Broker and the
MQTT protocol with our Watson IoT Broker – an alternative to the ibmiotf library.
On a Micro-controller (ESP8266 Adafruit Huzzah, etc), we can use a similar library like
explained below:
The previous command install mqtt node.js packages, and the mqtt command line which allows
you to use mqtt.js without coding in Node.js
Use your OrgID , Device Type, Device ID, Token used in the previous section:
Replace the variables by your own values from your .env file. Below a sample script (bash for
Linux or Mac) . On Windows, remove the backslashes and use a single line command with
your variable set:
. .env
Note 1 : You can see all the MQTT protocol options by using mqtt –help
-i : DeviceID
-C mqtt | mqtts
-t : topic
-P: password or token
Note 2: the Watson IoT Broker respects naming conventions and format , documented here
https://www.ibm.com/support/knowledgecenter/SSQP8H/iot/platform/devices/mqtt.html
Check the received messages on your IoT Broker, like in the previous section:
Drag the ibmiot input node from the left section and drop it on to the center app development
environment. Drag the debug output node from the left section and drop it on to the center
app development environment to the right of the ibmiot node. Connect the 2 nodes. It should
look like this:
Double click the IBM IoT node. Update the Authentication section to Bluemix Service and
update the Device id section to the DeviceID of your simulated device i.e. 100i700
Click Done
Click Deploy on the top tight section (Note, anytime there is a change in the app, the Deploy
button turns red and it needs to be deployed for the change to take effect). The green icon and
connected should appear below the IBM IoT flow.
Go to the **debug tab **on the right section of the node-red and you will see the MQTT data
being sent from the device to the node-red. If you click on the cursor in front of the object, you
will see the temperature, pressure, humidity …
You can enhance this flow with a lot of features (logic, database …) and play with Node-RED.
Successful exercise !
You learned how to use the IBM Internet of Things, Node-Red and a Node application to
implement MQTT messages to send and collect metrics.