This summer I was lucky enough to acquire a two month internship at SAP SE in Germany. Determined to prove myself I was ecstatic to have been assigned my first major task: to connect the new BB8 droid to the windows PC and create an application using Cordova to control it. On the outside the task seemed relatively easy. But of course like everything to do with, algorithms, programming and droids this was not the case. What made it even worse was that I could NOT find anyone on Google that had already completed the same task!?!? Now as an electrical engineering student with just some basic programming skills in C, JavaScript and HTML I thought I was more than prepared to take on this daunting task. But it turned out to be a lot more challenging than anticipated.

Screen Shot 2016-07-14 at 1.59.17 PM.png


Connecting the BB8 to your PC via BLE.

BB8 like most Sphero products works on BLE (Bluetooth low energy usually synonymous with Bluetooth 4.0+) However, if you have a Windows PC note that Bluetooth 4.0 does not have Bluetooth low energy and so you will need a BLE dongle (USB adapter).But, if you have a Mac life is good and BLE connectivity is already inbuilt. I used both Cylon and Sphero and so I have documented both. But Sphero has more functionality because some Cylon functions do not work.

First with Sphero:

1)  You should already have npm installed. If you do not have it you can download it here: Node.js

2)  Run the command: $ npm install sphero noble

3)  Then run to retrieve your devices UUID: $ node ./node_modules/noble/examples/advertisement-discovery.js

4)  Go to your .js file and add the following code:

Code Snippet 1

var sphero = require("sphero"), bb8 = sphero("UUID"); // change UUID accordingly

bb8.connect(function() { 

Interval(function() {   

var direction = Math.floor(Math.random() * 360);   

bb8.roll(150, direction); 

}, 1000); });

5)  Now run the .js file using node:node bb8.js. Your BB8 should connect and start rolling!

Now with Cylon:

1)  Install Cylon.js: $ npm install cylon cylon-firmata cylon-gpio cylon-i2c

2)  Install cylon-sphero-ble for Sphero robots: $ npm install cylon cylon-sphero-ble

3)  To add to your .js file:

Code Snippet 2

var Cylon = require('cylon');

Cylon.robot({  connections: {    bluetooth: { adaptor: 'central', uuid: ‘cc360e85785e’, module: ‘cylon-ble’}  }, 

devices: {    bb8: { driver: ‘bb8’, module: ‘cylon-sphero-ble’}  }, 

work: function(my) {    my.bb8.color(0x00FFFF);   

after(500, function() {      my.bb8.color(0xFF0000);    }); 

} }).start();

Now run this on npm the same way we ran it on Sphero and your BB8 should start to change colour.

Sending Data to IOT Services on HANA Cloud Platform.

1)  If you do not already have an account you need to create a trial account. You can refer to the  openSAP: SAP HANA Cloud Platform Essentials course or to an easy-to-follow 8 step tutorial on: http://go.sap.com/developer/tutorials/hcp-create-trial-account.html.

2)  Once you have your HCP trial account set up. We can start to send and receive messages through IOT Services.

First, IOT stands for the Internet of Things. Which is a network of Physical devices that are embedded with electronice devices like sensors and actuators so that they can exchange and send data via the internet. There is really good documentation on this in the form of videos on YouTube. (HCP Academy).

However, the Videos do not go through the basics in every video and so in order to reach the sending data via node.js section you’d have to go through atleast 5 or 6 of them. But that’s why you have me and I will walk you through it.

3)  So first we are going to assign your user the IoT-MMS-User role. you just need to go into authorizations, type in your ID (username eg: p1110002222trial, It’s highlighted below) and add the role to yourself.Next we are going to create our Device Type, Device and Message type in the IOT service cockpit. In order to do this you log into your trial account.  Then navigate to the tab on the top where your username is printed: p1110002222trial

Screen Shot 2016-07-13 at 9.26.53 AM.png

4)  Then on the right hand side navigate and click on services:

Screen Shot 2016-07-13 at 9.26.53 AM.png

5)  Then scroll down and select IOT Services:

Screen Shot 2016-07-13 at 9.28.13 AM.png

6)  Make sure you enable it by selecting the small enable icon under where it says Internet of Things. It will turn green when it is Enabled.

Screen Shot 2016-07-14 at 3.29.25 PM.png

7)  Now you will be able to select the “Go To Service”. You have now entered the IOT cockpit. (It would be beneficial to bookmark this as you will be returning to it)

Screen Shot 2016-07-14 at 3.30.58 PM.png

8)  Creating Device Type: Select Device type. Now select the little ‘+’ icon on the bottom left. Fill out the information you are prompted for: Device type could be anything like: Droids, ClimateDevices, Sensors etc

Screen Shot 2016-07-13 at 9.34.26 AM.png

And click on create.

9)    Now create your device. Go back to you IOT cockpit and select Device. Go ahead and do the same as above (I named my device BB8) and For Device type select the one you created above^. Also create attributes for your device like Collisiondata or Velocity etc. and make sure to fill out the correct data types or this might be a problem later on.

Screen Shot 2016-07-14 at 3.33.35 PM.png

10) Now go back to the IOT cockpit and navigate to Message Types. Do the same as above. And for Direction select From Device.

Screen Shot 2016-07-14 at 3.36.11 PM.png

11) Now back to the IOT cockpit again and Navigate to “Send and View messages and perform other actions”. I used WebSockets to send my data and so that is what I have documented.

NOTE: WebSocket is a protocol providing full-duplex communication channels over a single TCP connection. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011, and the WebSocket API in Web IDL is being standardized by the W3C. More info


Select send and receive messages through Websockets. Here you can see a sample of how the message sending works once you click on the send button on the right under the Send Message tab. You will see the reply on the server.Screen Shot 2016-07-14 at 3.39.26 PM.png

12) You can also view the reply if you go back a page and click on display stored messages. (under T_IOT_Config) Screen Shot 2016-07-14 at 3.42.13 PM.png

13) Now to actually send the data you have streamed from BB8. I streamed BB8’s collision data and managed to send it to the IOT service using Cylon. First I streamed the data and printed it to the console log, but you can skip this step if you are confident enough:

Screen Shot 2016-07-13 at 9.59.36 AM.png

14) Now: Type out the following:

Screen Shot 2016-07-13 at 10.06.11 AM.png

For Copy Pasting :):

Code Snippet 3

//sending to HANA IOT node.js

//using websocket protocol

//BB8 PROGRAM BEGINS

var Cylon = require('cylon');

Cylon.robot({    connections: {    bluetooth: { adaptor: 'central', uuid:  'UUID HERE', module: 'cylon-ble'}

  },  devices: {bb8: { driver: 'bb8', module: 'cylon-sphero-ble'}  }, 

work: function(my) { 

my.bb8.color("gold");   

after(500, function()   

{     

my.bb8.color("aqua");   

});   

after(1000, function()   

{     

my.bb8.roll(60, 0);         

});   

after(2000, function()   

{      my.bb8.roll(180, 180);      //Sensor streaming       

});         

after(6000, function()   

{      my.bb8.stop();   

});           

my.bb8.detectCollisions();                   

//When BB8 collides he turns green for a bit and then red   

// This function also grabs the collisions data and posts it to the console log           

my.bb8.on("collision",function(data)                \

{               

console.log("collision detected");                   

console.log("data:",data);                                   

my.bb8.color("green");                                   

after(1000, function()                   

{                     

my.bb8.color("red");                    });               

//TO SEND DATA HERE                   

var ws = require('nodejs-websocket');                       

var host = 'iotmms<YOUR ACCOUNT NAME(p0000000trial)>.hanatrial.ondemand.com';           

var device = '<YOUR DEVICE TOKEN FROM IOT SERVICE>';           

var oAuthToken = '<YOUR oAuthTOKEN FROM IOT SERVICE>';           

var messageType = '<YOUR messageType ID from IOT SERVICE>';                       

var path = '/com.sap.iotservices.mms/v1/api/ws/data/';                       

var options = {               

extraHeaders: {                   

'Authorization': 'Bearer ' + oAuthToken,                   

'Content-Type': 'application/json;charset=utf-8'               

}             

};                       

var connection = ws.connect('wss://' + host + path + device, options);                       

connection.on('connect', function() {                console.log("Connected!");               

var data1=JSON.stringify(data);// TO MAKE STRING               

var jsonData = {                   

"mode": "async",                   

"messageType":messageType,                   

"messages": [{                       

"Col":data1,// these are YOUR device attributes base on YOUR data types!!                       

"Vel": 3.2                       

}]               

};               

var strData = JSON.stringify(jsonData);                   

connection.sendText(strData);               

});                           

connection.on('close', function() {                   

console.log("Connection closed.");               

});                           

connection.on('text', function(text) {                   

console.log("From MMS:", text);                   

connection.close();               

});           

//ENDS HERE               

}); 

}     

}).start();

15) Now go to your Terminal and run the program like before node bb8.js

16) Your terminal should now look something like this:

Screen Shot 2016-07-13 at 10.16.31 AM.png

^ That is the collision data being gathered

Screen Shot 2016-07-13 at 10.16.45 AM.png

And that^ is the proof of your message being sent

Now if you navigate back to where your messages are stored you should see the data you sent there! Screen Shot 2016-07-14 at 3.51.15 PM.png

Good Luck! For more documentation on IOT have a look at: HCP Academy

To report this post you need to login first.

7 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply