Overview

Skill Level: Intermediate

Requires an understanding of Arduino Hardware and mobile application development.

Learn how to build a smart wearable sensor and connect it to the Watson IoT cloud through a mobile app.

Ingredients

The source code is available at the following GitHub link.  Hardware requirements include:

  1. An active Bluemix Account (register here)
  2. Arduino Circuit Playground Classic (from Adafruit
  3. Bluetooth LE Module (from Adafruit)
  4. Lithium Polymer Battery (Optional) (from Adafruit)
  5. USB LiIon/ LiPoly Charger (Optional) (from Adafruit)
  6. iPhone/ Android Phone with Bluetooth Support.

Note that this tutorial can apply to any Arduino

Step-by-step

  1. Setting up the Arduino Hardware

    The first step is to connect the Circuit Board Classic (CB) to the Bluetooth Module (BLE). To do so, connect the Rx of CB to Tx of the BLE, and the Tx of the Cb to the Rx of the BLE. Then connect the Vcc and GND of CB to the Vcc and GND of the BLE. 

     

    Arduino

     

    (Optional)

    If you would like to add a battery, you can use the recommended charger indicated in the requirements, this charger allows you to have the battery permanently connected, and charge it by attaching a USB cable to the charger. To complete the connection, connect the battery to the port labeled BAT, and connect the port labeled LOAD to the CB (note that you will need a female-female JST connector to connect the charger to the Arduino). The battery used is a 500 mAh battery; tests have shown that it can run for at least half a day on one charge.

  2. Programming the Arduino

    The Arduino code shows you how to read from the onboard sensors and send the data to the Bluetooth module, it also includes 2 test applications to check the operation of the CB and BLE. The two required libraries for the CB and BLE are included the most up to date version can be obtained from Adafruit.

    The source code for the Arduino is available on the following Github link. It does the following:

     

    void init(){
    initCircuitBoard();
    initBluetooth();
    setupDoubleTap();
    }

    void loop(){
    data = readSensors();
    printDebug(data);
    sendToBluetooth(data);
    input = readBluetooth();
    printDebug(input);
    }  

    More details can be found in the source code.

  3. Building the Mobile App

    Your mobile device acts as a gateway between the Watson IoT platform and the CB. Currently, there is only an iOS application, the application is made up of two major pages: the main user interface and a view to show the list of available Bluefruit Bluetooth devices. Upon selecting the correct device, the application will output the received data on to the main screen. Each piece of data received is parsed by looking for an ‘a’ char at the end of every string. This character appended by the Arduino code to ensure there is no wrong parsing of data in case of data loss.

     

    Scan Page    Main View

     

    You can download the code from here to install the application. However, before doing that, a small change must be done to the code in watson-iot-with-arduino/iOSApp/iOS-watson-iot-ble-master/BLEDemo/MainViewController.swift:

    //CHANGE THESE TO INFORMATION FROM WATSON IOT
    let ORG_ID = "flstl1"//organization ID
    let ioTHostBase = "messaging.internetofthings.ibmcloud.com"//IoT host address
    let DEV_TYPE = "type01" //device type
    let DEV_ID = "dev01" //device ID
    let IOT_API_KEY = "abcdefg"// API key that is found in the IoT service credentials
    let IOT_AUTH_TOKEN = "abcdefg"// API token that is found in the IoT service credentials
    let TOPIC = "iot-2/type/type01/id/dev01/evt/status/fmt/json"

    This information must be obtained from the Watson IoT platform, which will be set up in the next steps.

  4. Setting up the Watson IoT Platform

    The first step in connecting to the Watson IoT Platform is to head over https://console.bluemix.net/catalog/services/internet-of-things-platform and provision a service instance. After doing so, launch the IoT tool from the Manage tab then head to devices. Click on the add new Device, then create new device type. Create a device type, give it a name, and then keep proceeding to skip the remaining options. When you are back at the Add Device screen, select the device type you just created, then click on next, enter a device name, and keep proceeding until you reach the device credentials page. Make note of all the device credentials and fill up the following details in the app.

    let ORG_ID = "flstl1"//organization ID
    let DEV_TYPE = "type01" //device type
    let DEV_ID = "dev01" //device ID
    let IOT_AUTH_TOKEN = "abcdefg"// API token that is found in the IoT service credentials

    The TOPIC field would be “iot-2/type/DEV_TYPE/id/DEV_ID/evt/status/fmt/json”. To obtain the “IOT_API_KEY”, head to Apps in the right navigation menu, and click on generate API key.

    Devices  Add DeviceDevice Details  API Key

     

    When this is done, compile and build your app and now you should be able to see the Arduino on the Bluetooth Scan page. When connected and data starts flowing in, you should be able to start seeing it in the Watson IoT Device page.

  5. Reading from Watson IoT

    Having set up the connection, the next step is to read this information and store it in a database. To do this, Node Red is a suitable platform to build a quick web app that reads the data and stores it in a database.

    To do this head over to Bluemix and create a Node Red boilerplate. When it is ready, find `<app name>-cloudantNoSQLDB` service under the application services. Head over to the manage tab on the left, and launch the toolkit. When the toolkit opens, you should see on a database called nodered, create a new database and remember the name.

     

    Node red

    Cloudant

    DB

    Then type the following URL to start using Node Red “https://<app-name>.mybluemix.net/red” and go through all the steps until you reach the flow diagram. Start off by adding an IBM IoT input node, and configure it to use an API key. Edit the API key and fill in the key, token, and server name from the previously obtained information. Select device status as the input type, and then fill up the device type and ID.

     

    IoT App

    IoT API

     

    After setting up the IoT Node, add a function node and a Cloudant node and connect them all together. To configure Cloudant, make sure you select the same service name you used earlier and enter the same name as the database you created. Also, ensure the function includes the following code which appends the current time to the data before saving it to the database:

    time = new Date().toString();
    msg.payload = time + ": " + msg.payload;
    return msg;

    Flow

    When done, deploy the app and everything should be ready!!

    Congratulations, you have just connected your Arduino to the Watson IoT Platform!

Join The Discussion