WARNING: This repository is no longer maintained
This repository will not be updated. The repository will be kept available in read-only mode.
Skill Level: Intermediate
N.B: All services used in this repo are Lite plans.
This Cognitive pattern demonstrates how to build a Cognitive IoT application. In cognitive IoT solutions, machine learning takes place in an edge computing architecture. Note that Edge computing means that you push computing away from the cloud or data center out towards the sensors. Computations happens on the edge gateway (e.g. Simulated Raspberry Pi in this scenario) near the sensors and actors. Let's use a simple scenario for illustration. Assume that you have a Washing Machine with 3 sensors.
- Fluid temperature
- Hardness of H2O
- Speed of the motor
One can use the sensors to monitor the washing machine for any abnormalities for these specific parts of the machine. When any of these become unstable, information is triggered to perform an action and to record for later analytics processing.
Application Workflow Diagram
- Sensors: The heart of every IOT solution is the sensors and actors. We sense the environment, make (cognitive) decisions, and act using actors. In the washing machine use case; the 3 sensors are 1) fluid temperature 2) hardness of H2O 3) Speed of the motor. These sensors would be in the washing machine.
- Node-RED Edge (Node-RED running on your device): edge means "it's near the device" which one manages; in this case that is the simulated Raspberry Pi; therefore it is called Node Red on the edge. Imagine the Raspberry Pi is attached to the washing machine. Data is flowing constantly from the sensors to the Raspberry Pi. The Node Red Edge (or Raspberry Pi) is hooked up to the Internet by ethernet and is streaming data to the Watson IoT Platform.
- Edge model: It's a built-in piece of code that filters and transforms values transitioned to the IoT Platform
- Watson IoT Platform: is using the MQTT Message broker. It publishes data back to the Node Red Edge (in a real world case, if it detects an anomaly, it would send a message back to the machine to deactivate its motor)
- Node-RED IBM Cloud (YOUR_APP_NAME.mybluemix.net): There is a second Node-RED on IBM Cloud and it is subscribing to the data coming from the IoT Platform.
- Node-Red Edge Node-RED is a flow-based programming tool.
- Watson IOT Platform Tools and services you need to connect, manage, analyze and secure IoT devices.
- MQTT Message Broker
These are high-level steps:
- Deploy NodeRED + AnomalyDetector app to the cloud
- Configure Watson IoT Components
- Stream data into Watson IoT Platform
- Analyze Edge-based Anomaly Detector using moving zscore in NodeRED Dashboard
Deploying the Application to IBM Cloud
A) Using the Deploy to IBM Cloud button
Clicking on the button will automatically deploy a NodeRED data flow tool acting as a device simulator in the IBM Cloud. It also creates a pre-configured "edge" implementation of the zScore algorithm.
Deploy to IBM Cloud button automates a majority of the deployment. Afterward, you need to wire up the Watson IoT Platform to your simulated device.
To see the app and services created and configured for this pattern, use the IBM Cloud dashboard. The app is named
CognitiveIoT-20170724231917178and it is running the following services:
So let's walk you through each element:
NodeRED is free, Open Source and runs everywhere! In the IBM Cloud, in every other cloud or data center, on your laptop and even on an IoT Gateway like a Raspberry Pi. So consider this flow to run on an IoT Gateway connected to an elevator and measuring voltage for the main driver motor. As we don’t have a Raspberry Pi in place we are just simulating these sensor values using an “Inject” node in NodeRED. Otherwise you would see a dedicated sensor node here
In addition we want to send data upstream to the cloud so let’s add a time-stamp. It is always good to generate a time-stamp (temporally and spatially) as close as possible to the sensor. So this value can be referred to as “event time” rather than “processing time”
To stream these data to the IBM Watson IoT Platform via MQTT only one simple node is necessary
In addition we want to create a little dashboard to monitor the voltage sensor values
The voltage dashboard displays the voltage data. In order to achieve this we need to extract the voltage value from the input message
Of course we want to plot the moving z-score as well – in parallel to the voltage in order to really understand what’s going on
Again we need to extract the z-score from the input message
Now we generate an alert messages in case the z-score drops below -0.5 which means some major fluctuation has been taken place recently
We just display this alert message under the two other charts
In order to get rid of the message once in a while we reset it
And delay the deletion 5s, so the message keeps displayed for 5s
Wait for some time until you observe a z-score below -0.5 and you’ll see that an alert message is being generated. Of course you can also trigger something more important like initiate an emergency shutdown of the system or raise an alert, either by sending an email/SMS from the Edge directly using a NodeRED node (see twilio or email for this) or you can also send the alert upstream to the cloud using MQTT. The latter would be a perfect example on how Edge analytics can reduce the amount of data transferred to the cloud by adding intelligence to the Edge gateway device:
Setting up your IBM Cloud Application with the Watson IoT Components
There are a few more steps required to wire up your Node-RED application to Watson IoT Platform. This gives you a dashboard that shows the real-time values of your simulated sensor, as well as shows connectivity over time and values over time.
Setting up the RemoteDevice Device Type in Watson IoT Platform
Each device connected to the IBM Watson IoT Platform is associated with a device type. Device types are intended to be groups of devices which share common characteristics. So in order to add devices in IBM Watson IoT Platform, one need to create a device type. What you are actually doing is registering a device in the IBM Watson IoT Platform.
Go to the menu in IBM Cloud console
Select your App's Name (not route) (CognitiveIoT-)
Under "connections", select "cognitive-iot-iotf-service"
Under the "Welcome to Watson IoT Platform" text, click "Launch"
On the WIoT page, Click the microchip icon on the left-hand side. It is labeled "Devices"
- Click the "+ Add Device" button on the right side.
Click the "Create device type" button.
Click the "Create device type" button on the next dialog as well.
On the "general information" dialog, enter the name "RemoteDevice", and click the "Next" button.
Let's skip this page, but this is where you'd fill in metadata about an actual physical device. Click "next".
Click "Next" again, then "Create"
Setting up the VoltageSensor01 Device in Watson IoT Platform
Now that your device type is created, you will add the device to the IoT Platform. A device can be defined that has a connection to the internet and has data it wants to get into the cloud. And devices can accept commands from applications as well. You need to add the device in IBM Watson IoT Platform Organization before connecting it to the IoT.
Select "RemoteDevice" in the dropdown under "Add Device", and click the "Next" button.
For Device ID, put "VoltageSensor01". This matches details under the ibmiot out node in Node-RED, which you can investigate by double-clicking the node.
Continue to click the "Next" button, until you get to the Summary view, and then click "Add".
You will now have a view of details about your VoltageSensor01 device. Scroll down to "Recent Events" and you should see voltage events occurring continuously. Under "Sensor Information", you can see the most recent data being received, including voltage, frequency, count, and timestamp ('ts'). Click the 'x' to the upper-right to close this view.
On the upper-left, click the menu item called "Boards".
- Click the "Device-Centric Analytics" board. You'll now see cards representing much of the same info we saw in the device details. However, this view is a dashboard to easily allow seeing multiple devices at the same time, and you could create additional cards to display further analytics.
In a production environment, you would run Node-RED on each of your edge devices, and wire each to Watson IoT Platform. There, you could monitor and manage all of them from a central dashboard.
For alerting, it you could run a Node-RED instance from IBM Cloud like above, and change only a few things to expand the Node-RED dashboard to show your other devices.
Another option would be to run the dashboard on your edge devices themselves, and check per-device. This is probably less than optimal though.
Note: This pattern was originally part of a larger tutorial which can be found HERE.
- Artificial Intelligence Code Patterns: Enjoyed this Code Pattern? Check out our other AI Code Patterns.
- Data Analytics Code Patterns: Enjoyed this Code Pattern? Check out our other Data Analytics Code Patterns
- AI and Data Code Pattern Playlist: Bookmark our playlist with all of our Code Pattern videos
- With Watson: Want to take your Watson app to the next level? Looking to utilize Watson Brand assets? Join the With Watson program to leverage exclusive brand, marketing, and tech resources to amplify and accelerate your Watson embedded commercial solution.
- Watson Studios: Master the art of data science with IBM's Watson Studios
- PowerAI: Get started or get scaling, faster, with a software distribution for machine learning running on the Enterprise Platform for AI: IBM Power Systems
- Spark on IBM Cloud: Need a Spark cluster? Create up to 30 Spark executors on IBM Cloud with our Spark service
- Kubernetes on IBM Cloud: Deliver your apps with the combined the power of Kubernetes and Docker on IBM Cloud
This code pattern is licensed under the Apache Software License, Version 2. Separate third party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the Developer Certificate of Origin, Version 1.1 (DCO) and the Apache Software License, Version 2.