Overview

Skill Level: Beginner

This tutorial will help you to connect your Sphero-BB8 droid to Watson IoT Platform and control it using Node-RED. For example, change the color of the BB8 to Red, Blue or Green.

Step-by-step

  1. Introduction

    In this recipe, we will show how to connect Sphero BB-8 Bluetooth Low Energy (BLE) droid to your local system using the Cylon API’s, connect it to Watson IoT Platform, send one or more events to the Platform and control it through a Node-RED application running in Bluemix.

    Following are the list of components involved in this recipe,

    bb-8-modi-arch-1

    In this recipe, we use Cylon driver to connect the computer to Sphero BB-8 BLE robot. Cylon is a JavaScript framework, provides a simple, but powerful way to create solutions around robotics and IoT. A Node-RED application running on the computer (for example Mac, Lenovo, Raspberry Pi and etc..) interacts with BB-8 using Cylon API and connects it to Watson IoT Platform such that applications running in the cloud (internet) can get access to the BB-8 data and control it with simple commands. After connecting BB-8 to the Watson IoT Platform, the Node-RED application running on the computer sends BB-8 events like collision events to Watson IoT Platform and subscribes to one or more commands from the application.

    A Node-RED application running on IBM Bluemix subscribes to BB-8 events through Watson IoT Platform and sends commands to control the BB-8. Following are the list of commands that are showcased in this recipe,

    • Command to change the color of the BB8
    • Command to spin
    • Command to roll

     

    Once the command is sent, the Node-RED application running on the local computer will receive the command from the Watson IoT Platform and forward it to BB-8 droid using the Cylon APIs. Upon receiving the command, the BB-8 droid performs the respective action.

    In the scope of this recipe, you shall be needing Watson IoT Platform and Node-RED services on Bluemix to control the device from Cloud. Click on the Create Toolchain button provided below, to quickly deploy the Watson IoT Platform – Node-RED Bluemix starter app. Post successful deployment, you shall have both the above mentioned services, up & running on Bluemix along with a Cloudant NoSQL DB at your disposal.

    Toolchain-6

  2. Install Node-RED and Cylon driver

    In this step, we will install the required modules on your laptop to interact with BB-8 and Watson IoT Platform.

    1. Download and install Python from https://www.python.org/downloads/ if not installed already.
    2. The sample uses Node-RED to connect the system to BB-8 and to Watson IoT Platform, so install Node-RED on your system using the steps mentioned here – http://nodered.org/docs/getting-started/installation.html.
    3. Navigate to the directory where Node-RED is installed and install the required Cylon packages.
      Note!
      In Windows usually Node-RED is installed under,
      C:UsersdminAppDataRoaming
      pm
      ode_modules
      ode-red
      ode_modules
    4. Install Cylon package using the following command:
      npm install cylon
    5. Install Cylon BLE package using the following command:
      npm install -g cylon-ble
      Note!
      If you hit the following error, then try installing the required version of the Python library by node-gyp. For example, python2.7.

      gyp ERR! stack Error: Python executable "python.EXE" is v3.5.2, which is not supported by gyp.
      gyp ERR! stack You can pass the --python switch to point to Python >= v2.5.0 & < 3.0.0.
      Note!
      If you hit the following error, then try installing Microsoft Visual Studio and proceed.

      MSBUILD : error MSB3428: Could not load the Visual C++ component "VCBuild.exe". To fix this, 1) install the .NET Framework 2.0 SDK, 2) install Microsoft Visual Studio 2005 or 3) add the location of the component to the system path if itis installed elsewhere.
    6. Install Cylon Sphero BLE package using the following command:
      npm install cylon-sphero-ble
    7. Open the settings.js file present in the directory where Node-RED is installed and add the following line into the functionGlobalContext. This will make the cylon package available to all functions.
      cylon:require('cylon')
    8. For example, the contents of the relevant section should look like the following,
      functionGlobalContext: {cylon:require('cylon')        // os:require('os'),        // j5board:require("johnny-five").Board({repl:false})    },

    In this step, we have successfully installed the required modules to interact with BB-8 and Watson IoT Platform.

  3. Connect BB-8 from your laptop

    In this step, we will create a Node-RED flow to connect to BB-8.

    Node-RED is a tool for wiring together the Internet of Things in new and interesting ways, including hardware devices, APIs, and online services. It is built on top of Node.js and takes advantage of the huge node module ecosystem to provide a tool that is capable of integrating many different systems.

    1. Before we program the BB-8, we will need to determine the BLE address(UUID) of the robot. One way to do is to use the cylon-ble-scan command line utility installed as part of cylon-ble. So, Navigate to the bin folder of the cylon-ble package – <node-red>/node_modules/cylon-ble/bin and run the following command,
      node blescan.js

      You might observe an output like below,

      Starting scan.Peripheral discovered!  Name: BB-A372  UUID: d9e1319ea372  rssi: -39
      Note!
      If you get a “LIBUSB_ERROR_NOT_SUPPORTED” error, try to resolve it using the solution present here.
    2. Note down the UUID of the droid, we will need this to connect to the BB-8 later.
    3. Go to the Node-RED application, Drag and drop inject node from the input panel and give a name.
    4. Drag and drop a function node, double-click the node and paste the following code. This code creates a connection to your Sphero BB-8 using Bluetooth. Note: Change the UUID of your BB-8 before connecting,
      var Cylon = context.global.cylon;Cylon.robot({  connections: {    bluetooth: { adaptor: 'central', uuid: 'd9e1319ea372', module: 'cylon-ble'}  },  devices: {    bb8: { driver: 'bb8', module: 'cylon-sphero-ble'}  },  work: function(my) {    my.bb8.color({ red: 0, green: 255, blue: 0 });    context.global.bb8=my.bb8;         my.bb8.on("collision", function(data) {      context.global.bb8.data=data;      node.status({text:"Collision detected"});    });    my.bb8.detectCollisions();  }}).start();return msg;
    5. As shown, the above code connects to BB-8 droid using Cylon driver API, defines the BB8 object in global context so that we can access it later to set different colors or do other actions and defines a method to detect collision. Once the collision is detected it sends the event to the status node.
    6. Connect the output of inject node to input of the function node as shown below and deploy the flow,
      connect-flow
    7. Click inject node to initiate the connection.

    Spin BB-8

    Let us create a simple flow to spin BB-8.

    1. In Node-RED flow editor, navigate to the menu at the top right of the screen and select Import from Clipboard. Copy the JSON string from the github location and paste it into the dialog box in Node-RED and select OK.
    2. Observe the following flow that configures the BB-8 to spin for 3 seconds. You can increase or decrease the spin window by editing the delay node (named as delay 3 s in the below image).spin-flow
    3. Click inject node to initiate the spin operation and observe that BB-8 spins for 3 seconds.

    Roll BB-8

    Let us create a simple flow to roll BB-8.

    1. In Node-RED flow editor, navigate to the menu at the top right of the screen and select Import from Clipboard. Copy the JSON string from the github location and paste it into the dialog box in Node-RED and select OK.
    2. Observe the following flow that configures the BB-8 to roll for 3 seconds. You can increase or decrease the roll window by editing the delay node (named as delay 3 s in the below image).init-roll
    3. Click inject node to initiate the roll operation and observe that BB-8 rolls for 3 seconds.

    In this step, we have connected BB-8 successfully from the laptop and performed a spin and roll operations. In the following sections, we will see how to connect BB-8 to Watson IoT and control it from Bluemix.

  4. Register your BB-8 Device in Watson IoT Platform

    In order to send BB-8 events and receive commands from Watson IoT Platform, we need to register the device(s) first, in the IBM Watson IoT Platform. This section guides you in the same.

    Open the Bluemix Dashboard and click on the Application service that you have currently deployed (using the Create Toolchain button), if you have already moved away from it. You should see the Watson IoT Platform listed under the Binded Services. Click on the WIoTP service and choose the click on Launch button to launch the WIoTP Dashboard.

    Carry out the steps present in this recipe to register your device(s) in IBM Watson IoT Platform. When the device is registered, you will be provided with the registration details shown below. Make a note of them, we need these details to connect the device to Watson IoT Platform later.

    device1

    Generate API Key and Token of Watson IoT Platform

    In order to connect applications to IBM Watson IoT Platform and to send commands to BB-8, we need to generate the API key and token first. This can be achieved by carrying out steps present in this section – Generate API Key in Watson IoT Platform.

    Note down the Key and Token, we need these later to connect the Node-RED application to Watson IoT Platform.

    At this step, we have successfully created the IBM Watson IoT Platform service, registered the device(s) in it and generated the API Key.

  5. Connect BB-8 to Watson IoT Platform

    In this step, we will use the Watson IoT Node to connect BB-8 to Watson IoT Platform and send its events.

    Watson IoT Node is a pair of nodes for connecting your device or gateway to the IBM Watson Internet of Things Platform. A device or gateway can use these nodes to send events and receive commands from the application.

    1. Run the following command in the user directory of your Node-RED install to install the Watson IoT Nodes.
       npm install node-red-contrib-ibm-watson-iot
    2. Restart the Node-RED and observe that the Watson IoT Nodes are present in the input and output section.
    3. Import the event flow by navigating to the menu at the top right of the screen and select Import from Clipboard. Copy the JSON string from the github location and paste it into the dialog box in Node-RED and select OK.
    4. Observe the following flow that connects the BB-8 to Watson IoT Platform, retrieves the collision event whenever the collision occurs and sends it across.event-flow
    5. Note that the flow is not completed, we need to configure the “Send Events to Watson IoT“(Watson IoT Output) node as follows:
      • Connect as Device in Registered mode
      • Add wiot-credentials by providing credentials obtained in the previous step
      • Set the Event type to event as shown below,1
    6. After successful configuration, the status of the Watson IoT node changes to connected.
    7. Trigger the spin/roll flow to create a collision. Observe that the events are sent to Watson IoT Platform once there is a collision.

    Viewing your device and events in Watson IoT Platform

    1. Open the Watson IoT Platform service that you created in the above step “Register your Device(s) In Watson IoT Platform” and click Launch Dashboard.
    2. Select Devices tab and observe that your device is connected to Watson IoT Platform.
    3. Click on the device to view the collision event sent by the Node-RED application running on your computer.device01

    In this section, we have successfully connected BB-8 device to Watson IoT Platform and sent its collision events.

  6. Create a Node-RED flow to receive commands from Watson IoT Platform

    In this step, we will create a flow to receive commands from the application.

    1. Import the command flow by navigating to the menu at the top right of the screen and select Import from Clipboard. Copy the JSON string from the github location and paste it into the dialog box in Node-RED and select OK.
    2. Observe that the following flow connects the BB-8 to Watson IoT Platform and subscribes to various commands.command-flow_1
    3. Note that the flow is incomplete. Double click the “Receive Commands from Watson IoT” (Watson IoT In) node and configure it as follows,
      • Connect as Device in Registered mode
      • Add wiot-credentials by providing credentials obtained in the registration step
      • Set command as “execute” as shown below,
        receive-commnd
    4. As this recipe supports 3 different commands, a switch node is added to route commands based on the action. As you observe in the flow, the 3rd route is already connected to the “set RGB color“(function) node that changes the color of the BB-8 based on the RGB values sent from the cloud.
    5. Connect the first route to the Spin flow and second route to the Roll flow as shown below,all-command-flow_1
    6. Click deploy to deploy the flow.
      Note!
      In case, if there is an error, import the complete flow from this github file.

    In this step, we have successfully created the flow to receive commands from the application.

  7. Create a Node-RED application in Bluemix to control BB-8 from cloud

    Accessing the Node-RED application in Bluemix

    1. You have deployed the Node-RED service on to the Bluemix by clicking on the Create Toolchain button in section #1. Access your Node-RED application by clicking on View App link and then choosing the option Go to you Node-RED flow editor, to access the editor.
    2. Parallelly, you can also access the Node-RED editor directly by accessing the following URL
       http://<your-application-name>.mybluemix.net/red

    Before proceeding with the flow creation, let us add a Node-RED dashboard package to input data through user interface.

    1. Click on the Git link available under CODE in the Overview section of your Bluemix Application dashboard as shown below to access the Git repository that is associated with this Node-RED application,

      Choose_Github_link

    2. Click on the package.json file and add the following line to install the Dashboard nodes,
      "node-red-dashboard":"x"
    3. Below is the sample screenshot showcasing the code changes,code-changes

      Note: You might be prompted to provide SSH Keys to Push the changes made to the package.json file (or any other edits to the Application)

    4. Commit the changes,
    5. Once the changes are Pushed through Commit operation, the Build & Deploy process gets initiated. You can choose to observe the log in the Delivery Pipeline option.
    6. Once the deploy operation is successful, open the Node-RED application (refresh if it is opened already) to see the dashboard nodes.

     In this step, we have created a Node-RED application in Bluemix and added the dashboard component into it.

  8. Create flow to initiate commands

    In this step, we will create a flow to trigger various commands to control BB-8.

    1. Go to the Bluemix Node-RED instance, navigate to the menu at the top right of the screen and select Import from Clipboard. Copy the JSON string from the github location and paste it into the dialog box in Node-RED and select OK.
    2. Observe the following flow that initiates the color, spin and roll commands,node-red-command-flow
    3. Open the Publish command to Watson IoT Node(IBM IoT App Out Node) and edit the configuration to include the following details:
      • Authentication: Bluemix Service,
      • Output Type: Device Command,
      • Device Type: The device type of BB-8 registered in Watson IoT Platform,
      • Device ID: The device Id of BB-8 registered in Watson IoT Platform,
      • Command Type: execute,
      • Format: json
    4. Select OK and Deploy.

    In this step, we have successfully created the flow that triggers various commands to BB-8.

  9. Trigger commands

    In this step, we will show how to trigger different commands to control BB-8.

    Change color of BB-8

    1. Open the dashboard for your Node-RED application by using the following URL – http://<your-application-name>..mybluemix.net/ui/
    2. Observe the following slider component to change the values of Red, Blue and Green components,slider
    3. Move the sliders to increase/decrease the values of each of the colour component and observe that the colour of the BB-8 changes as well.

    Instruct to Spin

    1. Go back to the Node-RED flow editor and click on the Initiate Spin node to initiate the spin action. Observe that BB-8 spins for a specified time.

    Instruct to Roll

    1. Similarly, click on the Initiate Roll node to initiate the roll action. Observe that BB-8 rolls around the place for a specified time.

    In this step, we triggered different commands to control BB-8.

  10. Conclusion and Next steps

    In this recipe, we learned the following,

    • How to connect BB-8 to your laptop
    • How to connect BB-8 to Watson IoT Platform using the Watson IoT Output node and send it’s events
    • A Node-RED Bluemix application to send commands to the device(BB-8) through Watson IoT Platform
    • How to configure the Watson IoT Input node to receive the commands from the application
    • How to configure BB-8 to roll, spin and change colors
       

    As a next step, go through the recipe Control Sphero-BB8 by Voice through IBM Watson IoT Platform that shows how one can control the BB-8 using voice.

    Also, We suggest you to look at the list of analytical and cognitive recipes around Watson IoT Platform to analyze the IoT events.

15 Comments on "Control your Sphero-BB8 using IBM Watson IoT Platform and Node-RED"

  1. context.global.bb8=my.bb8;
    I think saving bb8 into global context is not working and it is shown as ‘undefined’.
    Any help? (Node-Red v0.14.5, Node.js v6.3.1 @ Raspberry Pi 3)

    • YMDH_sathish_Palaniappan August 10, 2016

      Have you got functionGlobalContext set to anything in your settings file? The default file has it set to an empty object ({}). If it isn’t set, then there is no global context between the function nodes. Refer to this issue for more information – https://github.com/node-red/node-red/issues/405. Also, have you added the cylon package into the global context? functionGlobalContext: {
      cylon:require(“cylon”)
      // os:require(“os”),
      // j5board:require(“johnny-five”).Board({repl:false})
      },

  2. YMDH_sathish_Palaniappan August 11, 2016

    I got the following comment from @lockevi but nor sure why its not listed here.

    [SOLVED] It was SUDO less problem when i use cylon-ble. https://github.com/sandeepmistry/noble#running-on-linux

    noble warning: adapter state unauthorized, please run as root or with sudo or see README for information on running without root/sudo:

  3. […] the easy to use Node-RED with all the different devices and services. For example, check out how to control a Sphero-BB8 using Watson IoT Platform and […]

  4. GiseleCouto1 December 23, 2016

    Hi,
    After I run node blescan.js, I see this message:
    C:\Users\IBM_ADMIN\AppData\Roaming\npm\node_modules\cylon-ble\bin>node blescan.j
    s
    C:\Users\IBM_ADMIN\AppData\Roaming\npm\node_modules\cylon-ble\node_modules\noble
    \node_modules\bluetooth-hci-socket\lib\usb.js:70
    throw new Error(‘No compatible USB Bluetooth 4.0 device found!’);
    ^

    Error: No compatible USB Bluetooth 4.0 device found!
    at BluetoothHciSocket.bindUser (C:\Users\IBM_ADMIN\AppData\Roaming\npm\node_
    modules\cylon-ble\node_modules\noble\node_modules\bluetooth-hci-socket\lib\usb.j
    s:70:11)
    at BluetoothHciSocket.bindRaw (C:\Users\IBM_ADMIN\AppData\Roaming\npm\node_m
    odules\cylon-ble\node_modules\noble\node_modules\bluetooth-hci-socket\lib\usb.js
    :28:8)
    at Hci.init (C:\Users\IBM_ADMIN\AppData\Roaming\npm\node_modules\cylon-ble\n
    ode_modules\noble\lib\hci-socket\hci.js:99:35)
    at NobleBindings.init (C:\Users\IBM_ADMIN\AppData\Roaming\npm\node_modules\c
    ylon-ble\node_modules\noble\lib\hci-socket\bindings.js:83:13)
    at new Noble (C:\Users\IBM_ADMIN\AppData\Roaming\npm\node_modules\cylon-ble\
    node_modules\noble\lib\noble.js:50:18)
    at Object. (C:\Users\IBM_ADMIN\AppData\Roaming\npm\node_modules\c
    ylon-ble\node_modules\noble\index.js:4:18)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)

    Any idea how to solve this problem?

  5. MauricioL.M. February 10, 2017

    So as @lockevi said, I ran the command (I am running on a raspberry pi 3)
    sudo setcap cap_net_raw+eip $(eval readlink -f `which node`)
    Now I am able to run the examples from node-modules/cylon-sphero-ble/examples and I can also run some things on node-red using node-red-contrib-sphero package, but still, for the example from this page I get a “TypeError: Cannot read property ‘spin’ of undefined” error message when I try to spin or roll.
    Looking at the output from examples folder and from node-red (running node-red with sudo), it looks like node-red keeps waiting for a connection but never connects.
    Any help is appreciated.

    Thank you.

    • MauricioL.M. February 10, 2017

      I put some logs in the original work function (copied from this page):
      work: function(my) {
      my.bb8.color({ red: 0, green: 255, blue: 0 });
      context.global.bb8=my.bb8;

      my.bb8.on(“collision”, function(data) {
      context.global.bb8.data=data;
      node.status({text:”Collision detected”});
      });
      my.bb8.detectCollisions();
      }

      but the logs never show up. I am under the impression the work function is never being executed, hence context.global.bb8 is undefined.

      Any ideas?

      Thank you.

      • Hey I had the same problem. Or you are kind of right because my program was not even using that function and that is because my file was in a different directory. You can see the directory of the file that your program is using when you do node app.js or npm start.

        • MauricioL.M. February 11, 2017

          Sorry buddy I didn’t get your answer. I am running on node-red. The sample programs in examples folders all works, but the example from this page is not working on node-red (I can start node-red with sudo or not, it doesn’t work either way).
          Any ideas?
          Thank you.

Join The Discussion