├── .gitattributes ├── .gitignore ├── LICENSE.md ├── MAINTAINERS.md ├── README.md ├── assets ├── IoTool-dashboard.png ├── accel-flow.png ├── classifier-accel-flow.png ├── cloud-architecture.png ├── linear-accel-flow.png ├── notification-accel-flow.png ├── simple-flow.png └── training-flow.png └── tutorial ├── 1-SETUP.md ├── 2-PLATFORM.md ├── 3-NODERED.md ├── 4-DASHBOARD.md └── 5-MODEL.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .answers -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2020 IBM Corporation 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MAINTAINERS.md: -------------------------------------------------------------------------------- 1 | # MAINTAINERS 2 | 3 | Rodrigo Neumann Barros Ferreira - rneumann@br.ibm.com 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # IoT, Cloud and AI tutorial 2 | 3 | Integration of Internet of Things, Cloud Computing and Artificial Intelligence. 4 | 5 | ## Introduction 6 | 7 | This tutorial will teach you how to connect an Android device to the cloud and develop analytics models to analyse sensor data. 8 | By the end of this tutorial you should be able to set up your own IoT hub for streaming, storing and processing device data, besides becoming familiar with 9 | 10 | * Node-RED 11 | * IBM Cloud 12 | * MQTT protocol 13 | * IBM Watson Studio 14 | * Cloundant database 15 | * Continuous Delivery 16 | * Cloud Object Storage 17 | * IBM Watson IoT Platform 18 | * Application Starter Kits 19 | * Cloud Foundry applications 20 | 21 | ![cloud-architecture](assets/cloud-architecture.png) 22 | 23 | ## Prerequisites 24 | 25 | * Android smartphone 26 | * Internet connection 27 | 28 | ## Estimated time 29 | 30 | From 1h to 2h, depending on the familiarity of the reader with the concepts. 31 | 32 | ## Steps 33 | 34 | 1. Read [**Create IBM Cloud account and install IoTool apps**](tutorial/1-SETUP.md) and follow the steps. 35 | 1. [Set up IBM Cloud account](tutorial/1-SETUP.md#set-up-ibm-cloud-account) 36 | 1. [Install Android apps](tutorial/1-SETUP.md#install-android-apps) 37 | 1. Read [**Use IBM Watson IoT Platform**](tutorial/2-PLATFORM.md) and follow the steps. 38 | 1. [Create IBM Watson IoT Platform instance](tutorial/2-PLATFORM.md#create-ibm-watson-iot-platform-instance) 39 | 1. [Connect your device to IBM Watson IoT Platform](tutorial/2-PLATFORM.md#connect-your-device-to-ibm-watson-iot-platform) 40 | 1. [Monitor sensor data in the platform](tutorial/2-PLATFORM.md#monitor-sensor-data-in-the-platform) 41 | 1. Read [**Use Node-RED programming tool**](tutorial/3-NODERED.md) and follow the steps. 42 | 1. [Deploy Node-RED starter kit](tutorial/3-NODERED.md#deploy-node-red-starter-kit) 43 | 1. [Connect Node-RED app to IBM Watson IoT Platform](tutorial/3-NODERED.md#connect-node-red-app-to-ibm-watson-iot-platform) 44 | 1. [Create a simple Node-RED app](tutorial/3-NODERED.md#create-a-simple-node-red-app) 45 | 1. [Test simple Node-RED app](tutorial/3-NODERED.md#test-simple-node-red-app) 46 | 1. Read [**Create live dashboard on Node-RED**](tutorial/4-DASHBOARD.md) and follow the steps. 47 | 1. [Create live dashboard with acceleration data](tutorial/4-DASHBOARD.md#create-live-dashboard-with-acceleration-data) 48 | 1. [Enable linear acceleration sensors on your phone](tutorial/4-DASHBOARD.md#enable-linear-acceleration-sensors-on-your-phone) 49 | 1. [Enhance live dashboard with linear acceleration data](tutorial/4-DASHBOARD.md#enhance-live-dashboard-with-linear-acceleration-data) 50 | 1. [Add rudimentary shake detection to live dashboard](tutorial/4-DASHBOARD.md#add-rudimentary-shake-detection-to-live-dashboard) 51 | 1. Read [**Build an AI classifier using IBM Watson Studio**](tutorial/5-MODEL.md) and follow the steps. 52 | 1. [Store training data on Cloundant](tutorial/5-MODEL.md#store-training-data-on-cloundant) 53 | 1. [Create IBM Watson Studio project](tutorial/5-MODEL.md#create-ibm-watson-studio-project) 54 | 1. [Create Cloudant credentials for IBM Watson Studio](tutorial/5-MODEL.md#create-cloudant-credentials-for-ibm-watson-studio) 55 | 1. [Train classifier model](tutorial/5-MODEL.md#train-classifier-model) 56 | 1. [Test classifier model](tutorial/5-MODEL.md#test-classifier-model) 57 | 58 | 59 | 60 | ## Summary 61 | 62 | Congratulations, you have now built your own IoT hub for streaming, storing and analysing sensor data entirely on the cloud! 63 | If you want to keep learning about IoT, take a look at the link below where you can learn how to do something similar to what you just did but replacing your Android device by a single-board IoT device. 64 | 65 | ## Related links 66 | 67 | * [Analyze IoT ESP8266 sensor data](https://developer.ibm.com/tutorials/connect-a-soil-sensor-send-data-to-the-cloud-cfc-starter-kit-1/) -------------------------------------------------------------------------------- /assets/IoTool-dashboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/iot-cloud-ai-tutorial/fdd7310cf10a63f80934b1d9fead9078ec474f6d/assets/IoTool-dashboard.png -------------------------------------------------------------------------------- /assets/accel-flow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/iot-cloud-ai-tutorial/fdd7310cf10a63f80934b1d9fead9078ec474f6d/assets/accel-flow.png -------------------------------------------------------------------------------- /assets/classifier-accel-flow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/iot-cloud-ai-tutorial/fdd7310cf10a63f80934b1d9fead9078ec474f6d/assets/classifier-accel-flow.png -------------------------------------------------------------------------------- /assets/cloud-architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/iot-cloud-ai-tutorial/fdd7310cf10a63f80934b1d9fead9078ec474f6d/assets/cloud-architecture.png -------------------------------------------------------------------------------- /assets/linear-accel-flow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/iot-cloud-ai-tutorial/fdd7310cf10a63f80934b1d9fead9078ec474f6d/assets/linear-accel-flow.png -------------------------------------------------------------------------------- /assets/notification-accel-flow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/iot-cloud-ai-tutorial/fdd7310cf10a63f80934b1d9fead9078ec474f6d/assets/notification-accel-flow.png -------------------------------------------------------------------------------- /assets/simple-flow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/iot-cloud-ai-tutorial/fdd7310cf10a63f80934b1d9fead9078ec474f6d/assets/simple-flow.png -------------------------------------------------------------------------------- /assets/training-flow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/iot-cloud-ai-tutorial/fdd7310cf10a63f80934b1d9fead9078ec474f6d/assets/training-flow.png -------------------------------------------------------------------------------- /tutorial/1-SETUP.md: -------------------------------------------------------------------------------- 1 | # Create IBM Cloud account and install IoTool apps 2 | 3 | In this tutorial, you will create an account on IBM Cloud and install an application on your Android phone. 4 | 5 | ## Set up IBM Cloud account 6 | 7 | In this section, you will create a free account on [IBM Cloud](https://cloud.ibm.com/) which will allow you create your own applications, services, databases, etc. in the cloud. Any email address can be used to create a free account that has enough resources for the purposes of this tutorial. In case you have an email address issued by one of the registered academic institutions, you can ask for more resources through the [IBM Academic Initiative](https://www.ibm.com/academic). 8 | 9 | 1. Log in to [IBM Cloud](https://cloud.ibm.com/). If you do not have an account, [create one](https://cloud.ibm.com/registration) using your email address. 10 | 1. (Optional) In case you have an email address issued by an academic institution, go to [IBM Academic Initiative](https://www.ibm.com/academic) portal. 11 | * Click **Register now** in the center of the screen. 12 | * Enter your academic institution issued email in the text box and click **Submit**. 13 | * Fill in the form and click **Register**. 14 | * Follow the instructions to activate your account. 15 | * Once logged in return to the [main page](https://www.ibm.com/academic), access **IBM Cloud** under **Technology** in the top bar. 16 | * In the next page, scroll down and open the **Software** tab. 17 | * Follow the instructions to retrieve your **IBM Cloud Feature Code**. 18 | 1. (Optional) In case you have been able to retrieve your **IBM Cloud Feature Code**, follow these steps to add it to your IBM Cloud account. 19 | * Return to [IBM Cloud](https://cloud.ibm.com/) and click **Manage** in the top bar, then **Account**. 20 | * In the left side menu, choose **Account settings**. 21 | * In the new page, scroll down for **Subscription and feature codes** and click **Apply code**. 22 | * Enter your **IBM Cloud Feature Code** and click **Apply**. 23 | 24 | ## Install Android apps 25 | 26 | In this section, you will install the [IoTool](https://iotool.io/) app and its extensions on your Android phone. This app will read the sensor measurements and send them to IBM Cloud using the [MQTT](https://en.wikipedia.org/wiki/MQTT) protocol. 27 | 28 | 1. On your Android phone, open the **Play Store** app, search for **"IoTool"** and install the following apps: 29 | * [IoTool - Internet of Things (IoT) sensor platform](https://play.google.com/store/apps/details?id=io.senlab.iotoolapp) 30 | * [IoTool IBM Watson Cloud](https://play.google.com/store/apps/details?id=io.senlab.iotool.extension.ibmwatsoncloud) 31 | 1. Open the **IoTool** app and choose **allow** for every question it asks. 32 | 1. Familiarize yourself with the app by following the on-screen tutorial. 33 | 1. On the main app screen, where a 2x2 grid shows **AccelX**, **AccelY** and **AccelZ**, press the **Play** icon (triangle) on the top right to start a measurement. Move your phone and observe the numbers. 34 | 1. Click the 3 boxes with the acceleration readings **AccelX**, **AccelY** and **AccelZ** to enable a live graph. Move your phone and observe the live graph. 35 | 1. Press the **Stop** icon (square) on the top right to stop the measurement. 36 | 1. Long press the empty box to the right of **AccelZ** and select **Accelerometer Absolute Value** under the **Device internal** drop-down menu. Click the **AccelAbs** box to enable it in the live graph. 37 | 1. Press the **Play** icon (triangle) on the top right to start a new measurement. Observe that **AccelAbs** approximately corresponds to the norm of the acceleration `(AccelX, AccelY, AccelZ)` vector. 38 | * *Note*: Your screen should look [like this](../assets/IoTool-dashboard.png). 39 | 1. Press the **Stop** icon (square) on the top right to stop the measurement. 40 | -------------------------------------------------------------------------------- /tutorial/2-PLATFORM.md: -------------------------------------------------------------------------------- 1 | # Use IBM Watson IoT Platform 2 | 3 | In this tutorial, you will create an instance of the IBM Watson IoT Platform service, connect your device to it and monitor device data remotely. 4 | 5 | ## Create IBM Watson IoT Platform instance 6 | 7 | In this section, you will create an instance of the [IBM Watson IoT Platform](https://www.ibm.com/cloud/watson-iot-platform) service and register your Android device for secure communication over MQTT. 8 | 9 | 1. Log in to [IBM Cloud](https://cloud.ibm.com/). 10 | 1. Click **Create resource +** in the top right corner. 11 | 1. In the **Services** menu on the left, select the **Internet of Things** category. 12 | 1. Click the [Internet of Things Platform](https://cloud.ibm.com/catalog/services/internet-of-things-platform) card. 13 | 1. Select the following options and click **Create**. 14 | * *Region*: Choose the one closest to your location. 15 | * *Plan*: Lite. 16 | 1. In the next screen, click **Launch** to open the IBM Watson IoT Platform. 17 | 1. In the **Browse Devices** screen, click on **Create a device**. 18 | 1. In the **Add Device** screen, fill in the form with the **Identity** information and then click **Next**. 19 | * *Device Type*: `android`. 20 | * *Device ID*: `phone`. 21 | 1. In the **Add Device** screen, the **Device Information** form contains *optional* fields that are not used for authentication. Fill in whatever you see fit, just for the purpose of differentiating between devices, then click **Next**. 22 | 1. In the **Add Device** screen, you can enter a custom **Authentication Token** or leave it blank for automatically generating one, then click **Next**. 23 | 1. Confirm the information displayed before clicking **Finish**. 24 | 1. **Write down** the **Device Credentials** information. *You cannot retrieve the token after you leave this page*. 25 | * *Organization ID*: this value will be referred to as `ORG_ID`. 26 | * *Device Type*: this value will be referred to as `DEV_TYPE`. 27 | * *Device ID*: this value will be referred to as `DEV_ID`. 28 | * *Authentication Method*: this value will be referred to as `AUTH_METHOD`. 29 | * *Authentication Token*: this value will be referred to as `AUTH_TOKEN`. 30 | 31 | ## Connect your device to IBM Watson IoT Platform 32 | 33 | In this section, you will configure your Android phone to connect to your IoT Platform instance over MQTT using the IoTool app. The MQTT communication protocol works under the *publish-subscribe* model. Device(s) can *publish* (send) data onto a `topic` that other device(s) can *subscribe* (listen) to. 34 | 35 | 1. On your Android phone, open the **IoTool** app. 36 | 1. Open the **Drawer** by clicking the **Hamburger** icon (three horizontal lines) on the top left corner and choose **Settings**. 37 | 1. In the **Settings** menu, choose **Sensors**. 38 | 1. Make sure the **Record readings** and **Continuous internal sensors data** options are `ON`. 39 | 1. Return to the **Settings** menu. 40 | 1. In the **Settings** menu, choose **Cloud**. 41 | 1. Make sure the **Use cloud**, **Send data** and **Sync after session** options are `ON`. 42 | 1. Go to **Select cloud service** and pick **IBM Watson IoT** from the list. 43 | 1. Open the **IBM Watson IoT Settings** and fill in the information below. 44 | * *Send interval*: `1s` 45 | * *Hostname*: `ssl://ORG_ID.messaging.internetofthings.ibmcloud.com` 46 | * *Port*: `8883` 47 | * *Username*: `AUTH_METHOD` 48 | * *Password*: `AUTH_TOKEN` 49 | * *Client identification*: `d:ORG_ID:DEV_TYPE:DEV_ID` 50 | * *Topic*: `iot-2/evt/accel/fmt/json` 51 | * *Clean session*: `ON` 52 | * *Quality of Service*: `2` 53 | * *Condensed*: `OFF` 54 | 1. Return to the app dashboard (main screen) and press the **Play** icon (triangle) on the top right to start a measurement. 55 | 56 | ### Understanding your MQTT topic 57 | 58 | The `iot-2/evt/accel/fmt/json` MQTT topic string follows the format expected by IBM Watson IoT Platform and can be broken down as follows: 59 | 60 | * `iot-2` is the prefix associated to the IBM Watson IoT Platform MQTT server. 61 | * `evt` stands for *event* and represents the MQTT message type. There are two types of message: **events** and **commands**. In a nutshell, *events* are messages about something that happened in the past and *commands* are messages that trigger an action that should happen in the future. 62 | * `accel` is the event ID string. This could be any string, but we chose `accel` because we are streaming *acceleration* data. 63 | * `fmt` stands for **format**. 64 | * `json` is the **format string**, which means that the message will be formatted as a JSON. 65 | 66 | ## Monitor sensor data in the platform 67 | 68 | In this section, you will stream sensor data from your Android device to the cloud and monitor its contents in the IBM Watson IoT Platform. 69 | 70 | 1. Log in to [IBM Cloud](https://cloud.ibm.com/). 71 | 1. Click **View all** in the **Resource summary** card to open your [resource list](https://cloud.ibm.com/resources). 72 | 1. Expand the **Services** menu and click the name of the entry whose **Product** reads `Internet of Things Platform`. 73 | * *Note*: Do **not** click the **Cloud Foundry services** entry with the :link: icon after its name. This one is just an *alias* to the real **Service** entry. 74 | 1. In the next screen, click **Launch** to open the IBM Watson IoT Platform. 75 | 1. In the **Browse Devices** screen, check if the status of your device is **Connected**. 76 | 1. Click your device entry and open the **Recent events** tab. 77 | 1. Wait for events to start showing and click one event to inspect its contents. You should see something like the JSON below. 78 | 79 | ```JSON 80 | { 81 | "d": { 82 | "AccelerometerX@Device": 0.8376312255859375, 83 | "AccelerometerY@Device": -0.297119140625, 84 | "AccelerometerZ@Device": 9.83221435546875, 85 | "AccelerometerAbsolute@Device": 9.87230190916425 86 | } 87 | } 88 | ``` 89 | 90 | 1. Open the **State** tab, click the `>` icon to expand the contents of the `d` JSON object and observe the values changing in real time. 91 | 1. Click the white arrow icon (:arrow_right:) on the right side of the blue horizontal bar to see a different view on the same information in the **Device Drilldown** screen. 92 | -------------------------------------------------------------------------------- /tutorial/3-NODERED.md: -------------------------------------------------------------------------------- 1 | # Use Node-RED programming tool 2 | 3 | In this tutorial, you will deploy a Node-RED starter kit, connect it to IBM Watson IoT Platform, create and test your own app. 4 | 5 | ## Deploy Node-RED starter kit 6 | 7 | In this section, you will create a [Node-RED](https://nodered.org/) app using [IBM Cloud Starter Kits](https://cloud.ibm.com/docs/apps?topic=creating-apps-starter-kits). The deployment will be automated using the [Continuous Delivery](https://www.ibm.com/garage/method/practices/deliver/tool_continuous_delivery/) cloud service. 8 | 9 | 1. Log in to [IBM Cloud](https://cloud.ibm.com/). 10 | 1. Click **Create resource +** in the top right corner. 11 | 1. Open the **Software** menu on the left, then select the **Starter kits** software category. 12 | 1. Click the **Node-RED App** card and then the **Get started** button in the bottom of the page. 13 | 1. Select the following options and click **Create**. 14 | * *Region*: Choose the one closest to your location. 15 | * *Plan*: Lite. 16 | 1. Wait for the provision of the Cloudant service to finish. 17 | 1. In the **Deployment Automation** card, click the **Deploy your app** button to configure the Continuous Delivery feature. 18 | 1. Choose **IBM Cloud Foundry** as deployment target. 19 | 1. Click the **New +** button and then **OK** to create an API key for your app. 20 | 1. Set the **Region** to the one closest to your location and hit **Next**. 21 | 1. In the next screen, click **Create** and wait while the code is deployed. 22 | 1. When the **Status** field in the **Deployment Automation** card reads **Success**, the deployment is finished. 23 | 24 | ## Connect Node-RED app to IBM Watson IoT Platform 25 | 26 | In this section, you will connect the recently deployed [Cloud Foundry](https://www.ibm.com/cloud/cloud-foundry) application (i.e. your Node-RED app) to the Watson IoT Platform service. This will generate an API Key that will be used in the near future to authenticate your app. 27 | 28 | 1. Log in to [IBM Cloud](https://cloud.ibm.com/). 29 | 1. Click **View all** in the **Resource summary** card to open your [resource list](https://cloud.ibm.com/resources). 30 | 1. Expand the **Cloud Foundry apps** menu and click the name of the entry whose **Product** reads `SDK for Node.js™`. 31 | 1. Go to the **Connections** tab in the left side menu and click the **Create connection +** button in the top right corner. 32 | 1. Select the **Internet of Things Platform** entry and click **Next** at the bottom right corner. 33 | 1. In the new screen, click **Connect** to accept the automatic generation of a new API Key. 34 | 1. In the **Restage App** pop-up window, click the **Restage** button to reload the app with the new configuration. 35 | 1. Wait until the marker in the top center turns green and reads **Running**. 36 | 1. Go to the **Runtime** tab in the left side menu and select the **Environment variables** tab in the centre of the screen. 37 | 1. Click the 👁️ icon to show the **`VCAP_SERVICES`** content and you will find a big JSON object with two smaller objects (`iotf-service` and `cloudantNoSQLDB`), each one with their own `credentials` object. 38 | 1. Look for the `credentials` object inside `iotf-service` and take note of its contents. You may copy and paste it somewhere else or **Export** the full JSON to a file. 39 | 1. The `credentials` object in `iotf-service` should have, at least, the following fields: 40 | 41 | ```JSON 42 | "credentials": { 43 | "apiKey": "API_KEY", 44 | "apiToken": "API_TOKEN", 45 | "http_host": "ORG_ID.internetofthings.ibmcloud.com", 46 | "mqtt_host": "ORG_ID.messaging.internetofthings.ibmcloud.com", 47 | "mqtt_s_port": 8883, 48 | "mqtt_u_port": 1883, 49 | "org": "ORG_ID" 50 | } 51 | ``` 52 | 53 | * *Note*: If your `credentials` object does not have these fields above, your are probably looking at the credentials for the `cloudantNoSQLDB` service. 54 | 55 | ## Create a simple Node-RED app 56 | 57 | In this section, you will develop a simple application using the Node-RED web IDE. The application will receive acceleration data streamed from the device via MQTT. 58 | 59 | 1. Log in to [IBM Cloud](https://cloud.ibm.com/). 60 | 1. Click **View all** in the **Resource summary** card to open your [resource list](https://cloud.ibm.com/resources). 61 | 1. Expand the **Apps** menu and click the name of the entry whose **Product** reads `Cloud Application`. 62 | 1. In the **App details** screen, click the **App URL** link to open your Node-RED app. 63 | 1. Follow the instructions to protect your app from unwanted edits by defining a `username / password` pair. Click **Finish** when you are done. 64 | * *Note*: Write down this `username / password` pair. You will need it in the future. 65 | 1. In the Node-RED welcome screen, click the **Go to your Node-RED flow editor** and provide your login credentials to open the web IDE. 66 | 1. In the **Flow 1** editor tab, click and delete each one of the existing nodes to clear the screen. 67 | 1. In the node menu on the left, locate the `mqtt in` node under **Network**. 68 | 1. Drag and drop the `mqtt in` node to the editor tab. 69 | 1. Double-click the `mqtt in` node to open its configuration window. 70 | 1. In the **Properties** tab of the **Edit mqtt in node** window, click the :pencil2: icon (pencil) next to the **Server** options to configure the MQTT broker. 71 | 1. Fill in the **Connection** tab with the information below. 72 | * *Server*: `ORG_ID.messaging.internetofthings.ibmcloud.com`, as in `credentials.mqtt_host`. 73 | * *Port*: `8883`, as in `credentials.mqtt_s_port`. 74 | * *Client ID*: `a:ORG_ID:APP_ID` (where `ORG_ID` is `credentials.org` and `APP_ID` can be any string). 75 | * *Enable secure (SSL/TLS) connection*: `ON`. 76 | 1. Click the :pencil2: icon (pencil) next to **TLS Configuration** and fill the next screen with the information below. 77 | * *Verify server certificate*: `ON`. 78 | * *Server Name*: `ORG_ID.messaging.internetofthings.ibmcloud.com`, as in `credentials.mqtt_host`. 79 | 1. Click **Add** to close this window and return to the previous one. 80 | 1. Go to the **Security** tab and fill the fields with the information retrieved from the `credentials` JSON object. 81 | * *Username*: `API_KEY`, as in `credentials.apiKey`. 82 | * *Password*: `API_TOKEN`, as in `credentials.apiToken`. 83 | 1. Click **Add** to close this window and return to the previous one. 84 | 1. Back to the **Properties** tab, fill the rest of the fields with the information below. 85 | * *Topic*: `iot-2/type/DEV_TYPE/id/DEV_ID/evt/accel/fmt/json`. 86 | * *QoS*: `2`. 87 | * *Output*: `a parsed JSON object`. 88 | * *Name*: `Subscribe to MQTT event`. 89 | 1. Click **Done** to return to the flow editor. 90 | 1. In the node menu on the left, locate the `debug` node under **Common**. 91 | 1. Drag and drop the `debug` node to the editor tab. 92 | 1. Double-click the `debug` node to open its configuration window. 93 | 1. In the **Properties** tab of the **Edit debug node** window, change the **Output** field to `complete msg object`. 94 | 1. Click **Done** to return to the flow editor. 95 | 1. Click the gray circle on the right side of the `Subscribe to MQTT event` node and drag it to the gray circle on the left of the `msg` node. This will connect the two nodes. 96 | 1. Click the **Deploy** button in the top right corner. 97 | 1. In a few seconds a green marker should appear under the `Subscribe to MQTT event` node and display **connected** status. 98 | 1. Your Node-RED flow should look [like this](../assets/simple-flow.png). 99 | 100 | ## Test simple Node-RED app 101 | 102 | In this section, you will test the simple application you just created. The application will display the streamed data in the debug console. 103 | 104 | 1. Log in to [IBM Cloud](https://cloud.ibm.com/). 105 | 1. Click **View all** in the **Resource summary** card to open your [resource list](https://cloud.ibm.com/resources). 106 | 1. Expand the **Apps** menu and click the name of the entry whose **Product** reads `Cloud Application`. 107 | 1. In the **App details** screen, click the **App URL** link to open your Node-RED app. 108 | 1. In the Node-RED welcome screen, click the **Go to your Node-RED flow editor** and provide your login credentials to open the web IDE. 109 | 1. Click the :beetle: icon (bug) in the top right corner to open the **Debug** tab. 110 | 1. On your Android phone, open the **IoTool** app. 111 | 1. In the app dashboard (main screen), press the **Play** icon (triangle) on the top right to start a measurement. 112 | 1. On the Node-RED editor screen, note the JSON objects appearing periodically in the **Debug** tab on the right. 113 | 1. On your Android phone, press the **Stop** icon (square) on the top right to stop the measurement. 114 | 1. Pick one JSON object from the debug tab and click the `>` icon to expand it and open its contents. You should see something similar to the example below. 115 | 116 | ```JSON 117 | { 118 | "topic":"iot-2/type/android/id/phone/evt/accel/fmt/json", 119 | "payload":{ 120 | "d":{ 121 | "AccelerometerX@Device":-0.935272216796875, 122 | "AccelerometerY@Device":5.72149658203125, 123 | "AccelerometerZ@Device":7.0712890625, 124 | "AccelerometerAbsolute@Device":9.14403555675171 125 | } 126 | }, 127 | "qos":2, 128 | "retain":false, 129 | "_msgid":"afb1c702.e3e838" 130 | } 131 | ``` 132 | 133 | 1. Double-click the `debug` node to open its configuration window. 134 | 1. In the **Properties** tab of the **Edit debug node** window, change the **Output** field to `msg.payload`. 135 | 1. Click **Done** to return to the flow editor. 136 | 1. Click the **Deploy** button in the top right corner. 137 | 1. On your Android phone, press the **Play** icon (triangle) on the top right to start a measurement. 138 | 1. On the Node-RED editor screen, note the JSON objects appearing periodically in the **Debug** tab on the right. 139 | 1. On your Android phone, press the **Stop** icon (square) on the top right to stop the measurement. 140 | 1. Pick one JSON object from the debug tab and click the `>` icon to expand it and open its contents. You should see something similar to the example below. 141 | 142 | ```JSON 143 | { 144 | "d":{ 145 | "AccelerometerX@Device":-0.935272216796875, 146 | "AccelerometerY@Device":5.72149658203125, 147 | "AccelerometerZ@Device":7.0712890625, 148 | "AccelerometerAbsolute@Device":9.14403555675171 149 | } 150 | } 151 | ``` 152 | -------------------------------------------------------------------------------- /tutorial/4-DASHBOARD.md: -------------------------------------------------------------------------------- 1 | # Create live dashboard on Node-RED 2 | 3 | In this tutorial, you will create a live dashboard to display device acceleration data, enhance you dashboard with linear acceleration data and add a rudimentary shake detector algorithm. 4 | 5 | ## Create live dashboard with acceleration data 6 | 7 | In this section, you will create a live dashboard that will display the acceleration data streamed from the device in real-time. You will learn how to manipulate of the original `msg` object to extract the relevant information to be passed along to the dashboard nodes. 8 | 9 | 1. Log in to [IBM Cloud](https://cloud.ibm.com/). 10 | 1. Click **View all** in the **Resource summary** card to open your [resource list](https://cloud.ibm.com/resources). 11 | 1. Expand the **Apps** menu and click the name of the entry whose **Product** reads `Cloud Application`. 12 | 1. In the **App details** screen, click the **App URL** link to open your Node-RED app. 13 | 1. In the Node-RED welcome screen, click the **Go to your Node-RED flow editor** and provide your login credentials to open the web IDE. 14 | 1. Click the **Hamburger** icon (three horizontal lines) in the top right corner and pick **Manage palette**. 15 | 1. Go to the **Install** tab and search for `dashboard`. 16 | 1. Find the `node-red-dashboard` package and click the **Install** button to the right. Confirm the installation by clicking **Install** again in the pop-up screen. 17 | 1. Close the window to return to the **Flow 1** editor tab. 18 | 1. In the node menu on the left, locate the `change` node under **Function**. 19 | 1. Drag and drop the `change` node to the editor tab. 20 | 1. Double-click the `change` node to open its configuration window. 21 | 1. In the **Properties** tab of the **Edit change node** window, enter the information below. To add new rules, click the `+ add` button in the bottom left of the window. 22 | * *Name*: `Extract acceleration X`. 23 | * *Rule 1*: Set (msg) `payload` to (msg) `payload.d.AccelerometerX@Device`. 24 | * *Rule 2*: Set (msg) `topic` to (string) `X`. 25 | 1. Click **Done** to return to the flow editor. 26 | 1. Drag and drop another `change` node and fill it with the configuration below. 27 | * *Name*: `Extract acceleration Y`. 28 | * *Rule 1*: Set (msg) `payload` to (msg) `payload.d.AccelerometerY@Device`. 29 | * *Rule 2*: Set (msg) `topic` to (string) `Y`. 30 | 1. Click **Done** to return to the flow editor. 31 | 1. Drag and drop another `change` node and fill it with the configuration below. 32 | * *Name*: `Extract acceleration Z`. 33 | * *Rule 1*: Set (msg) `payload` to (msg) `payload.d.AccelerometerZ@Device`. 34 | * *Rule 2*: Set (msg) `topic` to (string) `Z`. 35 | 1. Click **Done** to return to the flow editor. 36 | 1. In the node menu on the left, locate the `chart` node under **Dashboard**. 37 | 1. Drag and drop the `chart` node to the editor tab. 38 | 1. Double-click the `chart` node to open its configuration window. 39 | 1. In the **Properties** tab of the **Edit chart node** window, fill in the configuration with the information below. 40 | * *Label*: `Accelerometer [m/s^2]`. 41 | * *Enlarge points*: `ON`. 42 | * *X-axis*: Last `2 minutes` or `200` points. 43 | * *legend*: `Show`. 44 | * *Name*: `Accelerometer`. 45 | 1. Click the :pencil2: icon (pencil) next to the **Group** options. 46 | 1. In the **Add new dashboard group config node** window, fill the *Name* field with `Acceleration` and click the :pencil2: icon (pencil) next to the **Tab** options. 47 | 1. In the **Add new dashboard tab config node** window, fill the *Name* field with `Acceleration dashboard`. 48 | 1. Click **Add** twice to close these windows and return to **Edit chart node**. Click **Done** to return to the flow editor. 49 | 1. In the node menu on the left, locate the `inject` node under **Common**. 50 | 1. Drag and drop the `inject` node to the editor tab. 51 | 1. Double-click the `inject` node to open its configuration window. 52 | 1. In the **Properties** tab of the **Edit inject node** window, fill in the form with information below. 53 | * *Payload*: (json) `[]`. 54 | * *Topic*: `clear`. 55 | * *Name*: `Clear graph`. 56 | 1. Click **Done** to return to the flow editor. 57 | 1. Connect the `mqtt in` node called **Subscribe to MQTT event** to each one of the 3 `change` nodes called **Extract acceleration X/Y/Z**. 58 | 1. Connect each one of the 3 `change` nodes called **Extract acceleration X/Y/Z** to the `chart` node called **Accelerometer**. 59 | 1. Connect the `inject` node called **Clear graph** to the `chart` node called **Accelerometer**. 60 | 1. Click the **Deploy** button in the top right corner. 61 | 1. Click the :bar_chart: icon (graph) in the top right corner to open the **Dashboard** tab and click the :arrow_upper_right: icon (open in new window) in the top right corner, to open the Node-RED dashboard in a new window. 62 | * *Note*: Your Node-RED flow should look [like this](../assets/accel-flow.png). 63 | 64 | ### Try it out: live dashboard 65 | 66 | 1. On your Android phone, open the **IoTool** app. 67 | 1. In the app main screen, press the **Play** icon (triangle) on the top right to start a measurement. 68 | 1. On the Node-RED dashboard screen, note the acceleration measurements appearing periodically in the chart. 69 | 1. On your Android phone, press the **Stop** icon (square) on the top right to stop the measurement. 70 | 71 | ## Enable linear acceleration sensors on your phone 72 | 73 | In this section, you will add linear acceleration to the list of sensor measurements streamed by your phone. 74 | 75 | 1. On your Android phone, open the **IoTool** app. 76 | 1. Open the **Drawer** by clicking the **Hamburger** icon (three horizontal lines) on the top left corner and choose **Settings**. 77 | 1. In the **Settings** menu, choose **Dashboard**. 78 | 1. In the **No. of buttons** option, set **7** as the new value. 79 | 1. Set the **3 empty buttons** to: 80 | * *Button 5*: `Device internal > Linear Acceleration X`. 81 | * *Button 6*: `Device internal > Linear Acceleration Y`. 82 | * *Button 7*: `Device internal > Linear Acceleration Z`. 83 | 1. Return to the app dashboard (main screen) and press the **Play** icon (triangle) on the top right to start a measurement. 84 | 1. Click the 3 new boxes with linear acceleration readings **LAccX**, **LAccY** and **LAccZ** to enable a live graph. Move your phone and observe the live graph. 85 | 1. Press the **Stop** icon (square) on the top right to stop the measurement. 86 | 87 | ## Enhance live dashboard with linear acceleration data 88 | 89 | In this section, you will add linear acceleration data to the live dashboard. You will learn how to copy, paste and edit existing nodes for better reuse of an existing flow. 90 | 91 | 1. Log in to [IBM Cloud](https://cloud.ibm.com/). 92 | 1. Click **View all** in the **Resource summary** card to open your [resource list](https://cloud.ibm.com/resources). 93 | 1. Expand the **Apps** menu and click the name of the entry whose **Product** reads `Cloud Application`. 94 | 1. In the **App details** screen, click the **App URL** link to open your Node-RED app. 95 | 1. In the Node-RED welcome screen, click the **Go to your Node-RED flow editor** and provide your login credentials to open the web IDE. 96 | 1. In the **Flow 1** tab, drag the cursor to select the **4 nodes** below. 97 | * `Extract acceleration X`. 98 | * `Extract acceleration Y`. 99 | * `Extract acceleration Z`. 100 | * `Accelerometer`. 101 | 1. After selecting the **4 nodes** mentioned above, press the keyboard shortcut associated with **Copy** in your operational system (`Ctrl+C` or `Cmd+C`). A pop-up message will confirm that those 4 nodes were copied successfully. 102 | 1. Press the keyboard shortcut associated with **Paste** in your operational system (`Ctrl+V` or `Cmd+V`). Move your cursor to place them in an empty area and confirm with a mouse click. A pop-down message will confirm that those 4 nodes were imported successfully. 103 | 1. Double-click the duplicated **Extract acceleration X** node to open its configuration window. In the **Properties** tab of the **Edit change node** window, **replace** the contents mentioned below and **keep** the contents of the unmentioned fields. 104 | * *Name*: `Extract linear acceleration X`. 105 | * *Rule 1*: (msg) `payload.d.LinearAccelerationX@Device`. 106 | 1. Click **Done** to return to the flow editor. 107 | 1. Double-click the duplicated **Extract acceleration Y** node to open its configuration window. In the **Properties** tab of the **Edit change node** window, **replace** the contents mentioned below and **keep** the contents of the unmentioned fields. 108 | * *Name*: `Extract linear acceleration Y`. 109 | * *Rule 1*: (msg) `payload.d.LinearAccelerationY@Device`. 110 | 1. Click **Done** to return to the flow editor. 111 | 1. Double-click the duplicated **Extract acceleration Z** node to open its configuration window. In the **Properties** tab of the **Edit change node** window, **replace** the contents mentioned below and **keep** the contents of the unmentioned fields. 112 | * *Name*: `Extract linear acceleration Z`. 113 | * *Rule 1*: (msg) `payload.d.LinearAccelerationZ@Device`. 114 | 1. Click **Done** to return to the flow editor. 115 | 1. Double-click the duplicated **Accelerometer** node to open its configuration window. In the **Properties** tab of the **Edit chart node** window, **replace** the contents mentioned below and **keep** the contents of the unmentioned fields. 116 | * *Label*: `Linear Accelerometer [m/s^2]`. 117 | * *Name*: `Linear Accelerometer`. 118 | 1. Click **Done** to return to the flow editor. 119 | 1. Connect the `mqtt in` node called **Subscribe to MQTT event** to each one of the 3 `change` nodes called **Extract linear acceleration X/Y/Z**. 120 | 1. Make sure that all 3 `change` nodes called **Extract linear acceleration X/Y/Z** are connected to the `chart` node called **Linear Accelerometer**. 121 | 1. Connect the `inject` node called **Clear graph** to the `chart` node called **Linear Accelerometer**. 122 | 1. Click the **Deploy** button in the top right corner. 123 | 1. Click the :bar_chart: icon (graph) in the top right corner to open the **Dashboard** tab and click the :arrow_upper_right: icon (open in new window) in the top right corner, to open the Node-RED dashboard in a new window. 124 | * *Note*: Your Node-RED flow should look [like this](../assets/linear-accel-flow.png). 125 | 126 | ### Try it out: enhanced live dashboard 127 | 128 | 1. On your Android phone, open the **IoTool** app. 129 | 1. In the app main screen, press the **Play** icon (triangle) on the top right to start a measurement. 130 | 1. Place your phone on a table and slide it laterally. 131 | 1. On the Node-RED dashboard screen, observe both the acceleration and linear acceleration measurements appearing periodically in the chart. 132 | 1. On your Android phone, press the **Stop** icon (square) on the top right to stop the measurement. 133 | 134 | ## Add rudimentary shake detection to live dashboard 135 | 136 | In this section, you will add a rudimentary shake detection feature to the dashboard using the absolute acceleration as a figure-of-merit. When a shake is detected, a notification will be displayed. 137 | 138 | 1. Log in to [IBM Cloud](https://cloud.ibm.com/). 139 | 1. Click **View all** in the **Resource summary** card to open your [resource list](https://cloud.ibm.com/resources). 140 | 1. Expand the **Apps** menu and click the name of the entry whose **Product** reads `Cloud Application`. 141 | 1. In the **App details** screen, click the **App URL** link to open your Node-RED app. 142 | 1. In the Node-RED welcome screen, click the **Go to your Node-RED flow editor** and provide your login credentials to open the web IDE. 143 | 1. In the node menu on the left of the **Flow 1** tab, locate the `change` node under **Function**. 144 | 1. Drag and drop the `change` node to the editor tab. 145 | 1. Double-click the `change` node to open its configuration window. 146 | 1. In the **Properties** tab of the **Edit change node** window, enter the information below. To add new rules, click the `+ add` button in the bottom left of the window. 147 | * *Name*: `Extract absolute acceleration`. 148 | * *Rule 1*: Set (msg) `payload` to (msg) `payload.d.AccelerometerAbsolute@Device`. 149 | * *Rule 2*: Set (msg) `topic` to (string) `abs(A)`. 150 | 1. Click **Done** to return to the flow editor. 151 | 1. In the node menu on the left of the **Flow 1** tab, locate the `switch` node under **Function**. 152 | 1. Drag and drop the `switch` node to the editor tab. 153 | 1. Double-click the `switch` node to open its configuration window. 154 | 1. In the **Properties** tab of the **Edit switch node** window, enter the information below. To add new rules, click the `+ add` button in the bottom left of the window. 155 | * *Name*: `0.5g < abs(A) < 1.5g`. 156 | * *Rule 1*: Is between (number) `4.9` and (number) `14.7`. 157 | * *Rule 2*: Otherwise. 158 | 1. Click **Done** to return to the flow editor. 159 | 1. In the node menu on the left, locate the `gauge` node under **Dashboard**. 160 | 1. Drag and drop the `gauge` node to the editor tab. 161 | 1. Double-click the `gauge` node to open its configuration window. 162 | 1. In the **Properties** tab of the **Edit gauge node** window, fill in the configuration with the information below. 163 | * *Label*: `Absolute acceleration`. 164 | * *Value format*: `{{value | number:2}}`. 165 | * *Units*: `m/s^2`. 166 | * *Range*: min `0`, max `20`. 167 | * *Colour gradient*: `red - green - red`. 168 | * *Sectors*: `0 - 4.9 - 14.7 - 20`. 169 | * *Name*: `Absolute acceleration`. 170 | 1. In the **Group** menu, select `Add new ui_group` and click the :pencil2: icon (pencil). 171 | 1. In the **Add new dashboard group config node** window, fill the *Name* field with `Shake detection` and click **Update**. 172 | 1. Click **Done** to return to the flow editor. 173 | 1. In the node menu on the left, locate the `notification` node under **Dashboard**. 174 | 1. Drag and drop the `notification` node to the editor tab. 175 | 1. Double-click the `notification` node to open its configuration window. 176 | 1. In the **Properties** tab of the **Edit notification node** window, fill in the configuration with the information below. 177 | * *Topic*: `Shake detected!`. 178 | * *Name*: `Shake notification`. 179 | 1. Click **Done** to return to the flow editor. 180 | 1. Connect the `mqtt in` node called **Subscribe to MQTT event** to the `change` node called **Extract absolute acceleration**. 181 | 1. Connect the `change` node called **Extract absolute acceleration** to the `switch` node called **0.5g < abs(A) < 1.5g** and to the `gauge` node called **Absolute acceleration**. 182 | 1. Connect the `Otherwise` (second) output of the `switch` node called **0.5g < abs(A) < 1.5g** to the `notification` node called **Shake notification**. 183 | 1. Click the **Deploy** button in the top right corner. 184 | 1. Click the :bar_chart: icon (graph) in the top right corner to open the **Dashboard** tab and click the :arrow_upper_right: icon (open in new window) in the top right corner, to open the Node-RED dashboard in a new window. 185 | * *Note*: Your Node-RED flow should look [like this](../assets/notification-accel-flow.png). 186 | 187 | ### Try it out: rudimentary shake detection 188 | 189 | 1. On your Android phone, open the **IoTool** app. 190 | 1. In the app main screen, press the **Play** icon (triangle) on the top right to start a measurement. 191 | 1. Shake your phone **gently**, at first, and then **vigorously**. 192 | 1. On the Node-RED dashboard screen, observe both the **Absolute acceleration** gauge meter and the notification in the top right corner whenever a shake is detected. 193 | 1. On your Android phone, press the **Stop** icon (square) on the top right to stop the measurement. 194 | -------------------------------------------------------------------------------- /tutorial/5-MODEL.md: -------------------------------------------------------------------------------- 1 | # Build an AI classifier using IBM Watson Studio 2 | 3 | In this tutorial, you will populate a Cloudant database with a training dataset, create an IBM Watson Studio project, connect it to Cloudant and use the stored dataset to train and test a classifier model for shake detection. 4 | 5 | ## Store training data on Cloundant 6 | 7 | In this section, you will create a Node-RED flow that stores the measured acceleration data into a Cloudant database. The sensor data is labelled with a Boolean `class` identifier that represents whether the device was being shaken or not during data collection. 8 | 9 | 1. Log in to [IBM Cloud](https://cloud.ibm.com/). 10 | 1. Click **View all** in the **Resource summary** card to open your [resource list](https://cloud.ibm.com/resources). 11 | 1. Expand the **Apps** menu and click the name of the entry whose **Product** reads `Cloud Application`. 12 | 1. In the **App details** screen, click the **Visit App URL** link to open your Node-RED app. 13 | 1. In the Node-RED welcome screen, click the **Go to your Node-RED flow editor** and provide your login credentials to open the web IDE. 14 | 1. In the **Flow 1** tab, click the `mqtt in` node called **Subscribe to MQTT event** and press the keyboard shortcut associated with **Copy** in your operational system (`Ctrl+C` or `Cmd+C`). A pop-up message will confirm that 1 node was copied successfully. 15 | 1. Click the **Add Flow** button, a :heavy_plus_sign: icon (plus) to the right of the **Flow 1** tab, to create a new tab called **Flow 2**. 16 | 1. In the **Flow 2** tab, press the keyboard shortcut associated with **Paste** in your operational system (`Ctrl+V` or `Cmd+V`). Move your cursor to place the node in an empty area and confirm with a mouse click. A pop-down message will confirm that 1 node was imported successfully. 17 | 1. In the node menu on the left of the **Flow 2** tab, locate the `change` node under **Function**. 18 | 1. Drag and drop the `change` node to the editor tab. 19 | 1. Double-click the `change` node to open its configuration window. 20 | 1. In the **Properties** tab of the **Edit change node** window, enter the information below. To add new rules, click the `+ add` button in the bottom left of the window. 21 | * *Name*: `Build complete JSON object`. 22 | * *Rule 1*: Set (msg) `payload` to the following (expression) content 23 | 24 | ```Javascript 25 | msg.payload.{ 26 | "time" : $millis(), 27 | "ax" : d."AccelerometerX@Device", 28 | "ay" : d."AccelerometerY@Device", 29 | "az" : d."AccelerometerZ@Device", 30 | "lax" : d."LinearAccelerationX@Device", 31 | "lay" : d."LinearAccelerationY@Device", 32 | "laz" : d."LinearAccelerationZ@Device", 33 | "a" : d."AccelerometerAbsolute@Device" 34 | } 35 | ``` 36 | 37 | * *Rule 2*: Set (msg) `payload.class` to (number) `0`. 38 | 39 | 1. Click **Done** to return to the flow editor. 40 | 1. In the node menu on the left, locate the `debug` node under **Common**. 41 | 1. Drag and drop the `debug` node to the editor tab. 42 | 1. In the node menu on the left, locate the `cloudant out` node under **Storage**. 43 | 1. Drag and drop the `cloudant out` node to the editor tab. 44 | 1. Double-click the `cloudant out` node to open its configuration window. 45 | 1. In the **Properties** tab of the **Edit cloudant out node** window, provide the following information below. 46 | * *Service*: If not already selected automatically, pick your Cloudant service from the menu. 47 | * *Database*: `training`. 48 | * *Only store msg.payload object?*: `ON`. 49 | * *Name*: `Training dataset`. 50 | 1. Click **Done** to return to the flow editor. 51 | 1. Connect the `mqtt in` node called **Subscribe to MQTT event** to the `change` node called **Build complete JSON object**. 52 | 1. Connect the `change` node called **Build complete JSON object** to the `debug` node called **msg.payload** and to the `cloudant out` node called **Training dataset**. 53 | 1. Click the **Deploy** button in the top right corner. 54 | 1. Make sure your Node-RED flow looks [like this](../assets/training-flow.png). 55 | 56 | ### Store class `0` examples in the training database 57 | 58 | 1. On your Android phone, open the **IoTool** app. 59 | 1. Place your phone horizontally on a flat surface, with the screen poiting upwards. 60 | 1. In the app main screen, press the **Play** icon (triangle) on the top right to start a measurement. 61 | 1. Let the experiment running for **120 seconds**. 62 | 1. On your Android phone, press the **Stop** icon (square) on the top right to stop the measurement. 63 | 64 | ### Store class `1` examples in the training database 65 | 66 | 1. Double-click the `change` node called **Build complete JSON object** to open its configuration window. 67 | 1. In the **Properties** tab of the **Edit change node** window, change first rule according to the instructions below. 68 | * *Rule 1*: Set (msg) `payload.class` to (number) `1`. 69 | 1. Click the **Deploy** button in the top right corner. 70 | 1. On your Android phone, open the **IoTool** app. 71 | 1. In the app main screen, press the **Play** icon (triangle) on the top right to start a measurement. 72 | 1. Shake your phone vigorously for **120 seconds**. 73 | 1. On your Android phone, press the **Stop** icon (square) on the top right to stop the measurement. 74 | 75 | ### Reset the training database 76 | 77 | **Warning: Do not perform these steps unless you have invalid data in your training database!!!** 78 | 79 | 1. Log in to [IBM Cloud](https://cloud.ibm.com/). 80 | 1. Click **View all** in the **Resource summary** card to open your [resource list](https://cloud.ibm.com/resources). 81 | 1. Expand the **Services** menu and click the name of the entry whose **Product** reads `Cloudant`. 82 | * *Note*: Do **not** click the **Cloud Foundry services** entry with the :link: icon after its name. This one is just an *alias* to the real **Service** entry. 83 | 1. In the next screen, click **Launch Dashboard**. 84 | 1. In the **Databases** screen, locate the `training` database and click the rightmost trashbin icon in the **Actions** column. 85 | 1. Enter `training` as the name of the database and click **Delete Database**. 86 | 1. Redo the previous two steps to populate the training database with examples of both classes. 87 | 88 | ## Create IBM Watson Studio project 89 | 90 | In this section, you will create an empty project on [IBM Watson Studio](https://www.ibm.com/cloud/watson-studio) alongside its companion [Cloud Object Storage](https://www.ibm.com/br-pt/cloud/object-storage) service instance. You will create an empty notebook to delevop a classifier model. 91 | 92 | 1. Log in to [IBM Cloud](https://cloud.ibm.com/). 93 | 1. Click **Create resource +** in the top right corner. 94 | 1. In the **Services** menu on the left, select the **AI / Machine Learning** category. 95 | 1. Click the [Watson Studio](https://cloud.ibm.com/catalog/services/watson-studio) card. 96 | 1. Select the following options and then click **Create**. 97 | * *Region*: Choose the one closest to your location. 98 | * *Plan*: Lite. 99 | 1. In the next screen, click **Get Started** to open the IBM Watson Studio. 100 | 1. In the loading screen, click on **Continue** and, when the deployment is done, click the **Go to IBM Watson Studio**. 101 | 1. In the **Welcome** screen, skip the tutorials and click on **New project +**. 102 | 1. In the **Create a project** screen, click on **Create an empty project**. 103 | 1. In the **New project** screen, you should configure your project by providing the information below. 104 | * *Define project details*: Give it a name and a description of your choosing. 105 | * *Define storage*: Choose an existing **Cloud Object Storage** (COS) service instance if you already have one. Otherwise, click **Add**. A new window will appear, in which you should select the **Lite** plan, then click **Create** and **Confirm**. Once you go back to the **New project** screen, click **Refresh** after a few moments to display the COS instance name. 106 | 1. In the **New project** screen, finish the project creation by clicking **Create**. 107 | 1. In the project overview page, skip the tour and click the **Add to project +** button on the top right. 108 | 1. In the **Choose asset type** window, pick **Notebook**. 109 | 1. In the **New notebook** screen, stay in the **Blank** tab and configure your notebook by providing the information below. 110 | * *Name*: Give it a name of your choosing. 111 | * *Select runtime*: `Default Spark 3.0 & Python 3.7 (Driver: 1 vCPU 4 GB RAM, 2 Executors: 1 vCPU 4 GB RAM)`. 112 | 1. Click **Create** on the bottom right. 113 | 1. After a short loading time, you should see a Jupyter-like notebook in your screen. 114 | 115 | ## Create Cloudant credentials for IBM Watson Studio 116 | 117 | In this section, you will create a service credential to allow your IBM Watson Studio project access the data stored in your Cloudant database. 118 | 119 | 1. Log in to [IBM Cloud](https://cloud.ibm.com/). 120 | 1. Click **View all** in the **Resource summary** card to open your [resource list](https://cloud.ibm.com/resources). 121 | 1. Expand the **Services** menu and click the name of the entry whose **Product** reads `Cloudant`. 122 | * *Note*: Do **not** click the **Cloud Foundry services** entry with the :link: icon after its name. This one is just an *alias* to the real **Service** entry. 123 | 1. In the next screen, choose **Service credentials** from the menu on the left side. 124 | 1. In the **Service credentials** window, click the **New credential +** button. 125 | 1. In the **Add new credential** window, change the name to `Credentials-WS` to make it easier to keep track of where each credential is being used. Click **Add** to confirm. 126 | 1. After a few seconds, a new entry should appear in the list. Click the twistie `>` to display its contents. 127 | 1. Make sure the JSON object has, at least, these **3 fields** below. 128 | 129 | ```JSON 130 | { 131 | "host": "CLOUDANT_HOST", 132 | "password": "CLOUDANT_PASSWORD", 133 | "username": "CLOUDANT_USERNAME" 134 | } 135 | ``` 136 | 137 | 1. Copy the contents of the credentials JSON to a file for future use. 138 | 139 | ## Train classifier model 140 | 141 | In this section, your will load the sensor data from Cloudant into a [Pandas](https://pandas.pydata.org/) `DataFrame` and use [PySpark](https://spark.apache.org/docs/latest/api/python/index.html) machine learning functions to train a logistic regression classifier model. 142 | 143 | 1. Log in to [IBM Cloud](https://cloud.ibm.com/). 144 | 1. Click **View all** in the **Resource summary** card to open your [resource list](https://cloud.ibm.com/resources). 145 | 1. Expand the **Services** menu and click the name of the entry whose **Product** reads `Watson Studio`. 146 | 1. In the next screen, click **Get Started** to open the IBM Watson Studio. 147 | 1. In the **Welcome** screen, click on your project name in the **Recent projects** card. 148 | 1. Go to the **Assets** tab, and click the name of your notebook in the **Notebooks** card. 149 | 1. In the notebook visualisation screen, click the :pencil2: icon (pencil) in the top horizontal bar to enable editing. 150 | 1. In an empty cell, paste and execute (with `Shift+Enter`) the Python code below to install the [SQL-Cloundant Connector](https://developer.ibm.com/clouddataservices/docs/ibm-data-science-experience/integrate/use-python-notebook-to-load-cloudant-data-into-spark/). 151 | 152 | ```Python 153 | !pip install --upgrade pixiedust 154 | import pixiedust 155 | pixiedust.installPackage("org.apache.bahir:spark-sql-cloudant_2.11:2.3.3") 156 | ``` 157 | 158 | 1. After successfully installing `pixiedust` and `spark-sql-cloudant`, you will be asked to restart the kernel by accesing **Kernel** in the top menu and selecting **Restart**. 159 | 1. In an empty cell, paste and execute the Python code below to instantiate and start a Spark session. 160 | 161 | ```Python 162 | import pixiedust 163 | from pyspark.sql import SparkSession 164 | spark = SparkSession.builder.getOrCreate() 165 | ``` 166 | 167 | 1. In an empty cell, paste and execute the Python code below to define the reader function. The `CLOUDANT_HOST`, `CLOUDANT_USERNAME` and `CLOUDANT_PASSWORD` variables are those defined in the `Credentials-WS` JSON file. 168 | 169 | ```Python 170 | def readDataFrameFromCloudant(database): 171 | 172 | cloudantdata = spark.read.format("org.apache.bahir.cloudant")\ 173 | .option("cloudant.host",'CLOUDANT_HOST')\ 174 | .option("cloudant.username", 'CLOUDANT_USERNAME')\ 175 | .option("cloudant.password",'CLOUDANT_PASSWORD')\ 176 | .load(database) 177 | 178 | return cloudantdata 179 | ``` 180 | 181 | 1. In an empty cell, paste and execute the Python code below to load the data from Cloudant and display the class breakdown. 182 | * *Note*: It is desirable to have approximately the same number of entries for each class. 183 | 184 | ```Python 185 | df = readDataFrameFromCloudant('training') 186 | df.createOrReplaceTempView('df') 187 | df = df.dropna() 188 | spark.sql('select class, count(class) from df group by class').show() 189 | ``` 190 | 191 | 1. In an empty cell, paste and execute the Python code below to display the dataset contents. 192 | 193 | ```Python 194 | display(df) 195 | ``` 196 | 197 | 1. In an empty cell, paste and execute the Python code below to import the machine learning libraries. 198 | 199 | ```Python 200 | from pyspark.ml.feature import StringIndexer, OneHotEncoder 201 | from pyspark.ml.linalg import Vectors 202 | from pyspark.ml.feature import VectorAssembler 203 | from pyspark.ml.feature import Normalizer 204 | from pyspark.ml import Pipeline 205 | from pyspark.ml.classification import LogisticRegression 206 | from pyspark.ml.evaluation import MulticlassClassificationEvaluator 207 | ``` 208 | 209 | 1. In an empty cell, paste and execute the Python code below to train the machine learning model. 210 | 211 | ```Python 212 | vectorAssembler = VectorAssembler(inputCols=["a","ax","ay","az","lax","lay","laz"], outputCol="features") 213 | lr = LogisticRegression(maxIter=1000).setLabelCol("class") 214 | pipeline = Pipeline(stages=[vectorAssembler, lr ]) 215 | model = pipeline.fit(df) 216 | result = model.transform(df) 217 | ``` 218 | 219 | 1. In an empty cell, paste and execute the Python code below to print the parameters of the trained model. 220 | 221 | ```Python 222 | print('intercept = ', model.stages[1].intercept) 223 | print('coefficients = ', model.stages[1].coefficients) 224 | ``` 225 | 226 | 1. In an empty cell, paste and execute the Python code below to evaluate the classification accuracy. 227 | 228 | ```Python 229 | binEval = MulticlassClassificationEvaluator().setMetricName("accuracy").setPredictionCol("prediction").setLabelCol("class") 230 | binEval.evaluate(result) 231 | ``` 232 | 233 | 1. In an empty cell, paste and execute the Python code below to verify the classification predictions. 234 | 235 | ```Python 236 | new_df = readDataFrameFromCloudant('training') 237 | new_df.createOrReplaceTempView('new_df') 238 | result = model.transform(new_df) 239 | result.createOrReplaceTempView('result') 240 | spark.sql("select a, ax, ay, az, lax, lay, laz, class, prediction from result").show(50) 241 | ``` 242 | 243 | ## Test classifier model 244 | 245 | In this section, you will translate the trained classifier model into a [Javascript](https://www.javascript.com/) function and use it to add a shake detection probability visualisation to your Node-RED dashboard. 246 | 247 | 1. Log in to [IBM Cloud](https://cloud.ibm.com/). 248 | 1. Click **View all** in the **Resource summary** card to open your [resource list](https://cloud.ibm.com/resources). 249 | 1. Expand the **Apps** menu and click the name of the entry whose **Product** reads `Cloud Application`. 250 | 1. In the **App details** screen, click the **Visit App URL** link to open your Node-RED app. 251 | 1. In the Node-RED welcome screen, click the **Go to your Node-RED flow editor** and provide your login credentials to open the web IDE. 252 | 1. In the **Flow 2** tab, delete the connection between the `change` node called **Build complete JSON object** and the `cloudant out` node called **Training dataset**. This will prevent the next measurements from being stored in the Cloudant databse. 253 | 1. In the node menu on the left of the **Flow 2** tab, locate the `function` node under **Function**. 254 | 1. Drag and drop the `function` node to the editor tab. 255 | 1. Double-click the `function` node to open its configuration window. 256 | 1. In the **Properties** tab of the **Edit function node** window, enter the information below. 257 | * *Name*: `Apply classification model`. 258 | * *Function*: Enter the Javascript code below, replacing the blanks "`__`" by the floating-point numbers printed in the notebook. 259 | 260 | ```Javascript 261 | const intercept = __; 262 | const coefficients = new Float32Array([__,__,__,__,__,__,__]); 263 | 264 | const x = intercept + 265 | coefficients[0] * msg.payload.d["AccelerometerAbsolute@Device"] + 266 | coefficients[1] * msg.payload.d["AccelerometerX@Device"] + 267 | coefficients[2] * msg.payload.d["AccelerometerY@Device"] + 268 | coefficients[3] * msg.payload.d["AccelerometerZ@Device"] + 269 | coefficients[4] * msg.payload.d["LinearAccelerationX@Device"] + 270 | coefficients[5] * msg.payload.d["LinearAccelerationY@Device"] + 271 | coefficients[6] * msg.payload.d["LinearAccelerationZ@Device"]; 272 | 273 | msg.payload = 100.0 / (1.0 + Math.exp(-x)); 274 | 275 | return msg; 276 | ``` 277 | 278 | 1. Click **Done** to return to the flow editor. 279 | 1. In the **Flow 1** tab, click the `gauge` node called **Absolute acceleration** and press the keyboard shortcut associated with **Copy** in your operational system (`Ctrl+C` or `Cmd+C`). A pop-up message will confirm that 1 node was copied successfully. 280 | 1. In the **Flow 2** tab, press the keyboard shortcut associated with **Paste** in your operational system (`Ctrl+V` or `Cmd+V`). Move your cursor to place the node in an empty area and confirm with a mouse click. A pop-down message will confirm that 1 node was imported successfully. 281 | 1. Double-click the `gauge` node to open its configuration window. 282 | 1. In the **Properties** tab of the **Edit gauge node** window, change the configuration using the information below. 283 | * *Label*: `Classifier model`. 284 | * *Units*: `%`. 285 | * *Range*: min `0`, max `100`. 286 | * *Colour radient*: `red - yellow - red`. 287 | * *Sectors*: `0 - 45 - 55 - 100`. 288 | * *Name*: `Classifier model`. 289 | 1. Click **Done** to return to the flow editor. 290 | 1. In the node menu on the left of the **Flow 2** tab, locate the `switch` node under **Function**. 291 | 1. Drag and drop the `switch` node to the editor tab. 292 | 1. Double-click the `switch` node to open its configuration window. 293 | 1. In the **Properties** tab of the **Edit switch node** window, enter the information below. To add new rules, click the `+ add` button in the bottom left of the window. 294 | * *Name*: `Probability > 50%`. 295 | * *Rule 1*: `>` (number) `50`. 296 | * *Rule 2*: Otherwise. 297 | 1. Click **Done** to return to the flow editor. 298 | 1. In the node menu on the left of the **Flow 2** tab, locate the `notification` node under **Dashboard**. 299 | 1. Drag and drop the `notification` node to the editor tab. 300 | 1. Double-click the `notification` node to open its configuration window. 301 | 1. In the **Properties** tab of the **Edit notification node** window, fill in the configuration with the information below. 302 | * *Layout*: `Bottom right`. 303 | * *Topic*: `Shake detected by classifier!`. 304 | * *Name*: `Classifier notification`. 305 | 1. Click **Done** to return to the flow editor. 306 | 1. Connect the `mqtt in` node called **Subscribe to MQTT event** to the `function` node called **Apply classification model**. 307 | 1. Connect the `function` node called **Apply classification model** to the `gauge` node called **Classifier model** and to the `switch` node called **Probability > 50%**. 308 | 1. Connect the `> 50` (first) output of the `switch` node called **Probability > 50%** to the `notification` node called **Classifier notification**. 309 | 1. Click the **Deploy** button in the top right corner. 310 | 1. Click the :bar_chart: icon (graph) in the top right corner to open the **Dashboard** tab and click the :arrow_upper_right: icon (open in new window) in the top right corner, to open the Node-RED dashboard in a new window. 311 | * *Note*: Your Node-RED flow should look [like this](../assets/classifier-accel-flow.png). 312 | 313 | ### Try it out: classifier model 314 | 315 | 1. On your Android phone, open the **IoTool** app. 316 | 1. In the app main screen, press the **Play** icon (triangle) on the top right to start a measurement. 317 | 1. Leave your phone on the table, at first, and then pick it up and shake it. 318 | 1. On the Node-RED dashboard screen, observe both the **Classifier model** gauge meter and the notification in the bottom right corner whenever a shake is detected. 319 | 1. On your Android phone, press the **Stop** icon (square) on the top right to stop the measurement. 320 | 321 | 324 | 325 | 328 | --------------------------------------------------------------------------------