copyright | lastupdated | ||
---|---|---|---|
|
2019-03-07 |
{:shortdesc: .shortdesc} {:new_window: target="_blank"} {:codeblock: .codeblock} {:screen: .screen} {:tip: .tip} {:pre: .pre}
{: #gather-visualize-analyze-iot-data} This tutorial walks you through setting up an IoT device, gathering data in the {{site.data.keyword.iot_short_notm}}, 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 {{site.data.keyword.iot_short_notm}}.
- Create visualizations.
- Analyze the device generated data and detect anomalies.
{: #services}
This tutorial uses the following runtimes and services:
- {{site.data.keyword.iot_full}}
- Node.js Application
- {{site.data.keyword.DSX_short}} with {{site.data.keyword.iae_full_notm}} and {{site.data.keyword.cos_full_notm}}
- {{site.data.keyword.cloudant_short_notm}}
This tutorial may incur costs. Use the Pricing Calculator to generate a cost estimate based on your projected usage.
{: #architecture}
- Devices send sensor data to {{site.data.keyword.iot_full}} using MQTT protocol
- Historical data is exported into a {{site.data.keyword.cloudant_short_notm}} database
- {{site.data.keyword.DSX_short}} pulls data from this database
- Data is analyzed and visualized through a Jupyter notebook
{: #prereqs}
{{site.data.keyword.Bluemix_notm}} Developer Tools - Run the script to install ibmcloud 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 {{site.data.keyword.Bluemix_notm}} 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
- Open the code in an IDE of your choice and change the
name
andhost
values in the manifest.yml file to a unique value. - Push the application to the {{site.data.keyword.Bluemix_notm}}.
ibmcloud login ibmcloud target --cf ibmcloud cf push
- In a few minutes, your application will be deployed and you should see a URL similar to
<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 {{site.data.keyword.iot_short_notm}} 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.
{: #createboard}
- Open the IBM {{site.data.keyword.iot_short_notm}} 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 Submit. - 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 {{site.data.keyword.iot_short_notm}} tab, you should see the chart getting updated.
- Go to the {{site.data.keyword.Bluemix_notm}} Catalog and create a new {{site.data.keyword.cloudant_short_notm}} named
iot-db
. - Under Connections:
- Create connection
- Select the Cloud Foundry location, organization and space where an alias to the {{site.data.keyword.cloudant_short_notm}} service should be created.
- Expand the space name in the Connection Location table and use the Connect button next to iot demo hub to create an alias for the {{site.data.keyword.cloudant_short_notm}} service in that space.
- Connect and restage the app.
- Open the IBM {{site.data.keyword.iot_short_notm}} dashboard.
- Select Extensions from the left menu, and then click Setup under Historical Data Storage.
- Select the
iot-db
{{site.data.keyword.cloudant_short_notm}} 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 {{site.data.keyword.cloudant_short_notm}}. After a few minutes, launch the {{site.data.keyword.cloudant_short_notm}} dashboard to see your data.
{: #data_experience}
In this section, you will use the Jupyter Notebook that is available in the IBM {{site.data.keyword.DSX_short}} service to load your historical mobile data and detect anomalies using z-score. z-score is a standard score that indicates how many standard deviations an element is from the mean
- Go to the {{site.data.keyword.Bluemix_notm}} Catalog and under AI, select {{site.data.keyword.DSX_short}}.
- Create the service and launch it's dashboard by clicking Get Started
- Create a Project > Select Data Science > Click Create project and enter
Detect Anomaly
as the Name of the project. - Leave the Restrict who can be a collaborator checkbox unchecked as there's no confidential data.
- Under Define Storage, Click on Add and choose an existing Cloud Object Storage service or create a new one (Select Lite plan > Create). Hit Refresh to see the created service.
- Click Create. Your new project opens and you can start adding resources to it.
- Click on Assets > + Add to Project > Connection
- Select the iot-db {{site.data.keyword.cloudant_short_notm}} where the device data is stored.
- Cross-check the Credentials and then click Create.
- Click Services on the top navigation bar > Compute Services.
- Click Add service.
- Click Add on {{site.data.keyword.iae_full_notm}}.
- Select the Lite plan.
- Select AE 1.1 Spark as software package
- Click Create.
- Select a resource group, change the service name if you want to and Confirm.
- Navigate to the
Detect Anomaly
project through Projects. - In the Settings, scroll to Associated services.
- Click Add service, select {{site.data.keyword.iae_full_notm}}.
- Select the {{site.data.keyword.iae_full_notm}} instance previously created. If the service is not showing in the list, make sure it is fully provisioned first.
- Click + Add to Project and add a new notebook.
- Enter
Anomaly-detection-notebook
for the Name. - Enter
https://github.com/IBM-Cloud/iot-device-phone-simulator/raw/master/anomaly-detection/Anomaly-detection-watson-studio-python3.ipynb
in the Notebook URL. - Select the {{site.data.keyword.iae_full_notm}} service associated previously as the runtime.
- Create Notebook. Set
Python 3.5 with Spark 2.3 (YARN Client Mode)
as your Kernel. Check that the notebook is created with metadata and code. 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 {{site.data.keyword.cloudant_short_notm}} credentials to that cell by completing the following steps:
- In the cell with the database name (
dbName
) enter the name of the {{site.data.keyword.cloudant_short_notm}} database that is the source of data, for example, iotp_yourWatsonIoTProgId_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 {{site.data.keyword.cloudant_short_notm}} 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).
{:removeresources}
- Navigate to Resource List > choose the Location, 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 {{site.data.keyword.iot_full}}, {{site.data.keyword.iae_full_notm}}, {{site.data.keyword.cloudant_short_notm}} and {{site.data.keyword.cos_full_notm}} services which you created for this tutorial.
{:related}
- Build, deploy, test, and retrain a predictive machine learning model
- Overview of IBM {{site.data.keyword.DSX_short}}
- Anomaly Detection Jupyter Notebook
- Understanding z-score
- Developing cognitive IoT solutions for anomaly detection by using deep learning - 5 post series