Kubernetes with OpenShift World Tour: Get hands-on experience and build applications fast! Find a workshop!

Integrating cloud analytics and a dashboard app into your IoT solutions

In the first article in this series, “Integrating LPWAN networking into your IoT solutions,” you learned how to monitor hay barns for humidity and temperature to identify dangerous conditions. You learned how to get sensor readings for temperature and humidity from NodeMCU devices to the IBM Watson IoT Platform. In this article, you learn how to preserve those readings in a database, how to display them in an IoT dashboard, and how to generate alerts.

What you’ll need to build your application

Because you are adding on to the IoT system that you built in the first tutorial in this series, you need to have completed the first tutorial before working on this one. To do that, you need:

Architecture

This figure shows the addition of the IoT cloud analytics system, including an IoT dashboard, to our overall IoT monitoring system (which also includes the IoT edge analytics system).

Architecture diagram

alt

Create your IoT dashboard application

We will build our IoT dashboard application using Node-RED, and it will display the data that is sent to Watson IoT Platform and stored in a Cloudant database. We already have a Watson IoT Platform, so we just need to create a Node-RED application and a Cloudant Database.

Create the IBM Cloud app

  1. Log in to your IBM Cloud account.
  2. In the dashboard, click Create resource.
  3. Click Starter Kits > Node-RED Starter.
  4. Name the application (the name should be unique, so haybale-< your name > is a good choice), and click Create.
  5. Wait until the application starts, and then click Visit App URL.
  6. Click Next.
  7. Configure a user name and a password for your Node-RED dashboard. I selected to let everybody view the editor because it’s a demonstration; you don’t have to do that.
  8. Click Next twice, and then click Finish.
  9. Do not close the browser window (you will need it soon).

Store the sensor data in the database

The next step is to receive the events in a Node-RED flow. This flow then formats the event messages and stores the information in a Cloudant database.

Follow these instructions to create the flow on your own. If you want to copy the flow and customize it as you follow along, copy the file JSON file from here to the clipboard and then in the Node-RED editor, click Import > Clipboard from the hamburger menu and paste the flow. To see the flow created, click here.

Create an API key to access the IBM Watson IoT Platform

  1. Open the IBM Watson IoT Platform you created in the first article.
  2. Click Apps > Browse.
  3. Click Generate API Key.
  4. Describe the key (for example, Key for Node-RED dashboard) and click Next.
  5. Select the role Backend Trusted Applications and click Generate Key.
  6. Copy the API key and the authentication token somewhere safe.
  7. Click Close.

Read sensor information to Node-RED

  1. Open the Node-RED editor for your application (you can go to the cloud resources, find it under Cloud Foundry Apps, click it, and then click Visit App URL).
  2. Click the hamburger menu and then Manage palette (you might need to log on first).
  3. Click Palette and then Install.
  4. Search for the package node-red-contrib-scx-ibmiotapp and select it.
  5. Click Install and then to confirm, click Install again.
  6. After the installation ends, click Close.
  7. Drag an input > ibmiot in node from the palette on the left into the flow (the large blank area).
  8. Double-click the new item.
  9. Specify these properties:

    • Authentication: API Key
    • API Key: Add new ibmiot
    • Input Type: Device Event
    • Device Type: All
    • Device Id: All
    • Event: All
    • Format: All
  10. Click the pencil icon.

  11. Create an IoT with these parameters:

    • Name: Hay Bale IoT Platform
    • API Key and API Token: The values you created for the API Key previously
    • Server-Name: The host name you used in the first articles (six characters for your specific ID followed by .messaging.internetofthings.ibmcloud.com)
  12. Click Add and then Done.

  13. Drag common > debug into the flow and draw a line between the output of the IoT Platform and the input of the Debug.
  14. Double-click the new node and set the output to complete msg object.
  15. Click Deploy.
  16. Send a few sensor readings to your Watson IoT Platform.
  17. Return to the editor and view the debug tab in the right sidebar. Expand one of the messages; it should have the temperature and humidity.

Note: If you do not have a currently operating sensor, you can go here to submit an HTTP request. If you do that, use these parameters:

  • Method: POST
  • URL: https://.messaging.internetofthings.ibmcloud.com:8883/api/v0002/device/types/Hay-Sensor/devices//events/sensorReading
  • Authorization: Basic Auth
  • Username: use-token-auth
  • Password: The authentication token for that specific device
  • Content: { “temp”: 0, “humidity” : 0}

Modify the messages

In the database, we’d want a timestamp and the device identifier. So, we need to add two parameters to the messages we receive.

  1. Drag function > function, and connect it between the IoT node and the debug node. Select and delete the existing connection.
  2. Name the function node add params.
  3. Write this function to add the device ID and time to the payload:

     msg.payload.time = new Date().getTime();
     msg.payload.deviceId = msg.deviceId;
     return msg;
    
  4. Deploy, test, and see that the msg.payload now has the time and deviceId.

Write the messages to the Cloudant database

Now that we have the message payload, we can write it to the database. You can download the flow you have by the end of this section from here. You can see this flow here.

  1. Add a storage > cloudant out node. Connect it after the add params node, in parallel with the debug node.
  2. In the new node, name the database events, and select Only store msg.payload object.
  3. Deploy and send a few more events.
  4. Return to the IBM Cloud Console.
  5. Open the -cloudantNoSQLDB database service under Cloud Foundry Services.
  6. This is an alias for Cloudant-. Click that.
  7. Click Manage and then Launch Cloudant Dashboard.
  8. Click the databases icon on the left sidebar, and then the events database.
  9. Click Table to see the event information with all the parameters you entered, as shown here:
See event information

alt

Generate alerts

When either the temperature or the humidity exceeds a threshold, you want to issue an alert. You can download the flow from here. You see the flow here.

Looking at the article about hay bale fires, these are the alert conditions (assuming your system does not use the edge analytics in the second article in the series to deal with problems automatically). The article uses Farenheit degrees, so this table includes both Farenheit (⁰F) and Centigrade (⁰C).

Condition Alert
Humidity > 20% The hay is too wet, it needs to be dried (low priority).
Temperature > 150 ⁰F (~65 ⁰C) Entering danger zone, disassemble stacked hay bales to promote air circulation (medium priority).
Temperature > 175 ⁰F (~79 ⁰C) Call fire services about possible risk, stop all air movement to avoid fanning the flames (high priority).
Temperature > 190 ⁰F (~88 ⁰C) Remove hot hay with the help of fire services. Expect a fire (critical priority).

If your system does use edge analytics (as shown here), the first two alert conditions do not require an alert. The edge analytics controls a heater and a fan to solve them automatically. You only need alerts when the temperature is above 175 ⁰F, because you need to call fire services.

Convert temperature sensor data from Centigrade to Fahrenheit

The DHT11 that produces our temperature readings is Chinese. It uses Centigrade. However, the article about hay bale fires presents its data in Fahrenheit. So, the first step is to add a node that calculates the Fahrenheit temperature.

  1. Return to the Node-RED editor.
  2. Drag a function node to the flow.
  3. Double-click the function node, name it CtoF, and use this function:

     msg.payload.tempF = msg.payload.temp*9/5+32;
     return msg;
    
  4. Connect the add params node’s left side to the CtoF node’s right side.

Calculate the status

Next, we calculate the status string. This is important, because we only want to generate alerts when the status changes.

  1. Drag function > switch.
  2. Connect the CtoF node’s right side to the switch node.
  3. Double-click the switch node to configure it.

    1. Name the switch tempAlerts.
    2. Set the property to msg.payload.tempF.
    3. Set the top rule’s operator to >=, the data type to number, and the value 190.
    4. Click Add to create two more rules, both with the operator >= and the data type number. The first has the value 175, and the second 150.
    5. Click Add to create a fourth rule with the operator otherwise.
    6. Change the bottom value to stopping after first match.
    7. Click Done.
  4. Drag a function > template node to create a node with these parameters:

    • Name: On fire
    • Set property: msg. payload.status
    • Format: Plain text
    • Template: Call fire services now! Fire expected!
  5. Copy and paste the template node you just created to create two more nodes, and change some parameters:

    • Name:
      • For Template Node #2: Stop air movement
      • For Template Node #3: Start air movement
    • Template:
      • For Template Node #2: Fire likely, stop air movement to avoid fanning flames.
      • For Template Node #3: Dangerously hot, promote air circulation.
  6. Connect the new nodes to the tempAlerts switch node’s right (output) side. The order is important: On fire on top, then Stop air movement, then Start air movement.

  7. Create another switch node with these parameters:

    • Name: humidityAlert
    • Property: msg. payload.humidity
    • Rule #1: >= 20
    • Rule #2: Otherwise
    • Type: stopping after first match
  8. Connect the left (input) side of this humidityAlert switch node to the tempAlertsnode’s right side, to the bottom connector (for the “otherwise” case).

  9. Copy and paste the On fire node twice more to create these nodes:

    • Name:
      • Template Node #4: Too humid
      • Template Node #5: Reduce humidity
    • Template
      • Template Node #4: Fine
      • Template Node #5: Everytime’s fine, no alert
  10. Connect the Too humid node to the top output of the humidityAlert node and the Fine node to the bottom one.

Create alerts

Finally, we can create alerts.

  1. Create a function > rbe node. Set these parameters:

    • Mode: Block unless value changes
    • Property: msg. payload.status
    • Name: Report by Exception
  2. Connect the output of all five template nodes to the input for Report by Exception. This node stops the flow unless the property it watches changes. This prevents alerts for minor changes in temperature or humidity.

  3. For now, put a debug node after Report by Exception (with the default parameters). Disable the msg debug node above by clicking the checkbox at its right side.
  4. Output msg. payload.status
  5. Your flow is now complete and can send alerts. Deploy your Node-RED app.

Send these messages to verify that you get the correct alerts in the debug pane:

Message Expected result (in the debug tab of the right sidebar)
{“temp”:0, “humidity”:5} The status says that everything’s fine
{“temp”:0, “humidity”:15} Nothing, no change in status
{“temp”:0, “humidity”:25} New alert telling you to reduce humidify
{“temp”:50, “humidity”:25} Nothing, no change in status
{“temp”:70, “humidity”:25} New alert telling you to promote air circulation (remember, 70 Celsius is 158 Fahrenheit)
{“temp”:80, “humidity”:25} New alert telling you to stop air circulation
{“temp”:90, “humidity”:25} New alert telling you to call fire services
{“temp”:10, “humidity”:15} New alert telling you everything is fine

If you are using edge analytics, you can use the same messages. However, you will not get alerts telling you to reduce humidity or promote air circulation. You can skip messages 2 through 5 because they do not change the status.

Create a dashboard

Users of the Hay Barn Monitoring app want to have a dashboard to visualize and analyze the sensor data that the application collects and stores. In this step, we create that dashboard. You can download the entire flow from here. You can see the entire flow here.

Create the initial dashboard

To create the initial dashboard, it is necessary to first import a Node-RED dashboard library.

  1. Click the hamburger icon on the top right.
  2. Click Manage palette.
  3. Click the Install tab. Type dashboard in the search field to reduce the number of results. Locate the node-red-dashboard library, and then click install. Click Install again.
  4. Click Close.
  5. Click the dashboard > Layout tab in the right sidebar.
  6. Click +tab to create a tab.
  7. Within Tab 1, click + group to create a new group.

Note: On a production system, you would have more meaningful names for tabs and groups.

Show the alert status

  1. Drag dashboard > text.
  2. Set these parameters:

    • Group: [Tab 1] Group 1
    • Label: Status
    • Value: {{msg.payload.status}}
  3. Connect the Report by Exception node’s right side to the new Status node’s left side.

  4. Deploy the new flow.
  5. Open a new browser tab and browse to http:///ui.
  6. Generate an alert.
  7. See that the browser tab now has the alert text.
  8. Generate different alerts to see that the status changes.

Show the current readings

It could be useful to know what the current temperature and humidity values are. To do so, you create two dashboard > gauge nodes with these parameters:

Parameter Node #1 Node #2
Group Group 1 [Tab 1] Group 1 [Tab 1]
Type Gauge Gauge
Label Temperature Humidity
Value Format {{msg.payload.tempF}} {{msg.payload.humidity}}
Units F %
Range 32 – 212 0 – 100
Sectors 32… 150… 175 … 212 0… 20… 20… 100

Then:

  1. Connect both gauge nodes to CtoF.
  2. Deploy the flow.
  3. Publish new values and see the temperature and humidity gauges show the values.

Show recent temperature chart

Now, to our dashboard we add a chart of recent temperatures.

  1. To start a new column, click the dashboard > Layout tab on the right sidebar. Select Tab 1 and click + group.
  2. Click edit on Group 2 and rename it to Recent History. Click Update.
  3. The chart node draws a chart of the message payload, with different series identified by msg.topic. To have the appropriate values, create a new function node after the CtoF node with these parameters:

    • Name: getTemp
    • Function: msg.payload = msg.payload.tempF; msg.topic = msg.deviceId + ” temp”; return msg;
  4. Create a dashboard > chart node after the getTemp node with these parameters:

    • Group: [Tab 1] Recent History
    • Size: 6 x 6
    • Label: Temp
    • Type: Line chart
    • X-axis: last 1 hours
    • X-axis Label: HH:mm:ss
    • Y-axis: 0 – 240 (looks nicer than the actual range, 32-212)
    • Legend: Show
    • Interpolate: bezier
  5. Deploy and send some messages with different temperatures. You can see my dashboard here. The one for yours would be https:///ui.

Deployment considerations

In this example, the alerts go to the dashboard. That’s nice for testing purposes, but obviously it is not good enough in production. In a production application, you would probably also use the IBM Push Notifications service for your alerts. With this service, you can send alerts by SMS. It also includes an API for writing alert applications for devices that do not have a cellular SMS number.

Another issue is dealing with multiple devices. The recent history chart automatically adjusts and shows multiple lines. Unfortunately, the gauges and alerts just rely on the latest information received. For the gauges, you can solve this by using a switch node and creating gauges for each.

We could use a similar approach with the alerts, but then we’d have to replicate a fairly complicated workflow. A better solution would be to replace the Report by Exception node with a node that keeps a list of devices and their latest statuses and identifies whenever the status of a specific device changes.

Conclusion

With this tutorial, we added an IoT cloud analytics system, including an IoT dashboard that generates alerts to our IoT monitoring system. We stored the IoT sensor data in a Cloudant database, and we used Node-RED to generate alerts and create a dashboard for the user (a farmer) of our IoT solution. Throughout the series so far, we’ve explored integrating LPWAN networking and edge computing as well as pushing analytics out to the edge devices, and using the IoT data to generate alerts for the user of the IoT solution.

Ori Pomerantz