copyright | lastupdated | ||
---|---|---|---|
|
2018-01-07 |
{:shortdesc: .shortdesc} {:new_window: target="_blank"} {:codeblock: .codeblock} {:screen: .screen} {:tip: .tip} {:pre: .pre}
This tutorial walks you through setting up an IoT device, gathering data in the Watson IoT Platform, exploring data and creating visualizations and then using advanced machine learning services to analyze data and detect anomalies in the historical data. {:shortdesc}
{: #objectives}
- Set up IoT Simulator.
- Send collection data to Watson IoT Platform.
- Create visualizations.
- Analyze the device generated data and detect anomalies.
![](images/solution16/Architecture.png)
{: #products}
This tutorial uses the following products:
- Internet of Things Platform
- Node.js Application
- Data Science Experience with Spark service and IBM Cloud Object Storage
- Cloudant NoSQL Database
{: #prereqs}
IBM Cloud Developer Tools - Script to install bx cli and required plug-ins
{: #iot_starter}
To begin, you will create Internet of Things Platform service - The hub which can manage devices, securely connect and collect data, and make historical data available for visualizations and applications.
- Go to the IBM Cloud Catalog and select Internet of Things Platform under the Internet of Things section.
- Enter
IoT demo hub
as the service name, click Create and Launch the dashboard. - From the side menu, select Security > Connection Security and choose TLS Optional under Default Rule > Security Level and click Save.
- From the side menu, select Devices > Device Types and + Add Device Type.
- Enter
simulator
as the Name and click Next and Done. - Next, click on Register Devices
- Choose
simulator
for Select Existing Device Type and then enterphone
for Device ID. - Click Next until the Device Security (under Security tab) screen is displayed.
- Enter a value for the Authentication Token, for example:
myauthtoken
and click Next. - After clicking Done, your connection information is displayed. Keep this tab open.
The IoT platform is now configured to start receiving data. Devices will need to send their data to the IoT Platform with the Device Type, ID and Token specified.
{: #confignodered} Next, you will deploy a Node.js web application and visit it on your phone, which will connect to and send device accelerometer and orientation data to the IoT Platform.
- Clone the Github repository:
git clone https://github.com/IBM-Cloud/iot-device-phone-simulator cd iot-device-phone-simulator
- Push the application to the IBM Cloud.
bx login bx target --cf bx cf push <PICK_UNIQUE_NAME>
- In a few minutes, your application will be deployed and you should see a URL similar to
<PICK_UNIQUE_NAME>.mybluemix.net
- Visit this URL on your phone using a browser.
- Enter the connection information from your IoT Dashboard tab under Device Credentials and click Connect.
- Your phone will start transmitting data. Back in the IBM Watson IoT Platform tab, check for new entires in the Recent Events section.
{: #createcards} Next, you will create a board and cards to display device data in the dashboard. For more information about boards and cards, see Visualizing real-time data by using boards and cards.
{: #createboard}
- Open the IBM Watson IoT Platform dashboard.
- Select Boards from the left menu, and then click Create New Board.
- Enter a name for the board,
Simulators
as example, and click Next and then Create. - Select the board that you just created to open it.
{: #cardtemp}
- Click Add New Card, and then select the Line Chart card type, which is located in the Devices section.
- Select your device from the list, then click Next.
- Click Connect new data set.
- In the Create Value Card page, select or enter the following values and click Next.
- Event: sensorData
- Property: ob
- Name: OrientationBeta
- Type: Float
- Min: -180
- Max: 180
- In the Card Preview page, select L for the line chart size, and click Next > Submit
- The card appears on the dashboard and includes a line chart of the live temperature data.
- Use your mobile phone browser to launch the simulator again and slowly tilt the phone forward and backward.
- Back in the IBM Watson IoT Platform tab, you should see the chart getting updated.
- Go to the IBM Cloud Catalog and create a new Cloudant NoSQL Database named
iot-db
. - Open the IBM Watson IoT Platform dashboard.
- Select Extensions from the left menu, and then click Setup under Historical Data Storage.
- Select the
iot-db
Cloudant database. - Enter
devicedata
for Database Name and click Done. - A new window should load prompting for authorization. If you don't see this window, disable your pop-up blocker and refresh the page.
Your device data is now saved in Cloudant. Launch the Cloudant dashboard to see your data.
{: #data_experience}
In this section, you will use the Jupyter Notebook that is available in the IBM Data Science Experience service to load your historical temperature data and detect anomalies using z-score.
- Go to the IBM Cloud Catalog and select Data Science Experience.
- Create the service and launch it's dashboard by clicking Get Started
- Create a New Project and enter
Detect Anomaly
as the Name. - Create and select Object Storage and Spark services. Refresh
- Create.
- Click on Assets > + Add to Project > Connection
- Select the iot-db Cloudant DB where the device data is stored.
- Check the Credentials then click Create
-
Click New notebook > From URL
-
Enter
Anomaly-detection-sample
for the Name. -
Enter
https://raw.githubusercontent.com/IBM-Cloud/iot-device-phone-simulator/master/anomaly-detection/Anomaly-detection-DSX.ipynb
in the URL. -
Create Notebook.
Check that the notebook is created with metadata and code.
Recommended version for this notebook is
Python 2 with Spark 2.1
. To update, Kernel > Change kernel. To Trust the notebook, File > Trust Notebook. {:tip}
- Select the cell that starts with
!pip install --upgrade pixiedust,
and then click Run or Ctrl + Enter to execute the code. - When the installation is complete, restart the Spark kernel by clicking the Restart Kernel icon.
- In the next code cell, Import your Cloudant credentials to that cell by completing the following steps:
- Click
- Select the Connections tab.
- Click Insert to code. A dictionary called credentials_1" is created with your Cloudant credentials. If the name is not specified as "credentials_1", rename the dictionary to
credentials_1
.credentials_1
is used in the remaining cells. - name that is required for the notebook code to run.
-
In the cell with the database name (
dbName
) enter the name of the Cloudant database that is the source of data, for example, iotp_yourWatsonIoTPorgId_DBName_Year-month-day. To visualize data of different devices, change the values ofdeviceId
anddeviceType
accordingly.You can find the exact database by navigating to your iot-db CloudantDB instance you created earlier > Launch Dashboard. {:tip}
-
Save the notebook and execute each code cell one after another or run all (Cell > Run All) and by end of the notebook you should see anomalies for device movement data (oa,ob, and og).
You can change the time interval of interest to desired time of the day. Look for
start
andend
values. {:tip} -
Along with anomaly detection, the key findings or takeaways from this section are
- Usage of Spark to prepare the data for visualization.
- Usage of Pandas for data visualization
- Bar charts, Histograms for device data.
- Correlation between two sensors through Correlation matrix.
- A box plot for each devices sensor, produced with the Pandas plot function.
- Density Plots through Kernel density estimation (KDE).
- Navigate to Dashboard > choose the region, Org and space where you have created the app and services. Under Cloud Foundry Apps, delete the Node.JS App your created above.
- Under Services, delete the respective Internet of Things Platform, Apache Spark, Cloudant NoSQL DB and Cloud Object Storage services which you created for this tutorial.
- Developing cognitive IoT solutions for anomaly detection by using deep learning - 5 post series