Paul Langdon
Published © GPL3+

BB-8's AWS Adventure

Sphero's BB-8 uses the Intel Edison and Amazon Web Services to communicate back to the rebel forces.

IntermediateFull instructions provided5,145
BB-8's AWS Adventure

Things used in this project

Story

Read more

Schematics

BB-8

Code

Find your BB-8node .

Plain text
This is how you find your BB-8 with an Intel Edison using node.js
To connect to your BB-8 or Ollie, you first need to determine its UUID. 
Once you have Noble installed, you can use the advertisement-discovery.js 
program to  determine the device's UUID:


$ node ./node_modules/noble/examples/advertisement-discovery.js
peripheral discovered (944f561f8cf441f3b5405ed48f5c63cf with address <unknown, unknown>, connectable true, RSSI -73:
    hello my local name is:
        BB-131D
    can I interest you in any of the following advertised services:
        []
    here is my manufacturer data:
        "3330"
    my TX power level is:
        -18

package.json

JavaScript
Package file
{
  "name": "BB-8-AWS",
  "description": "BB-8 sensor reading and transmit to AWS via Intel Edison",
  "version": "1.0.0",
  "main": "main.js",
  "engines": {
    "node": ">=0.10.0"
  },
  "dependencies": {
    "sphero" : "^0.7.0",
    "noble" : "^1.3.0",
    "aws-iot-device-sdk" : "^1.0.9"
  }
}

main.js

JavaScript
Main app
"use strict";

//node.js deps
// inherits
// mqtt
// minimist

//npm deps
// aws-iot-device-sdk


//app deps
var thingShadow = require('aws-iot-device-sdk/thing');
var sphero = require("sphero"),
    bb8 = sphero("FF:FF:FF:FF:FF:FF"); // change BLE address accordingly to the reading from Find your BB-8
    


//Define your device name
var Device_Name = 'BB8';


//define AWS certificate path paramenters
var args = {
	privateKey:'/home/root/aws_certs/privateKey.pem',
	clientCert:'/home/root/aws_certs/cert.pem',
	caCert:'/home/root/aws_certs/aws-iot-rootCA.crt',
	clientId:'bb8',
	region:'us-east-1',
	reconnectPeriod:'10' //asumming reconnect period in seconds
} 





//create global state variable

var reported_state={ xpos: 0, ypos: 0, xvel: 0, yvel: 0, sog: 0};

//create global sensor value variables:

var read_xpos = 0; //X Position
var read_ypos = 0; //Y Position
var read_xvel = 0; //X Velocity
var read_yvel = 0; //Y Velocity
var read_sog = 0; //Speed over ground

//launch sample app function 
update_state(args);

function update_state(args) {

//create a things Shadows object

var thingShadows = thingShadow({
  keyPath: args.privateKey,
  certPath: args.clientCert,
  caPath: args.caCert,
  clientId: args.clientId,
  region: args.region,
  reconnectPeriod: args.reconnectPeriod,
});

//When Thing Shadows connects to AWS server:


thingShadows
  .on('connect', function() {
  	console.log('registering device: '+ Device_Name)

  	//register device
  	thingShadows.register(Device_Name);

  	
  	bb8.connect(function() {
      // roll BB-8 in a random direction, changing direction 5 seconds 
      setInterval(function() {
        var direction = Math.floor(Math.random() * 360);
        bb8.roll(150, direction);
        //read sensor values and send to AWS IoT
        read_sensor(send_state); 
      }, 5000);
      
      //define function for reading BB-8's location data
      function read_sensor(cb){
         bb8.readLocator(function(err, data) {
          if (err) {
            console.log("error: ", err);
          } else {
            console.log("readLocator:");
            console.log("  xpos:", data.xpos);
            console.log("  ypos:", data.ypos);
            console.log("  xvel:", data.xvel);
            console.log("  yvel:", data.yvel);
            console.log("  sog:", data.sog);
            
            read_xpos = data.xpos; //X Position
            read_ypos = data.ypos; //Y Position
            read_xvel = data.xvel; //X Velocity
            read_yvel = data.yvel; //Y Velocity
            read_sog = data.sog; //Speed over ground
            
            cb();
          }
        }); 
      }
    });
  });


// motitor for events in the stream and print to console:

thingShadows 
  .on('close', function() {
    console.log('close');
  });
thingShadows 
  .on('reconnect', function() {
    console.log('reconnect');
  });
thingShadows 
  .on('offline', function() {
    console.log('offline');
  });
thingShadows
  .on('error', function(error) {
    console.log('error', error);
  });
thingShadows
  .on('message', function(topic, payload) {
    console.log('message', topic, payload.toString());
  });

};



//define function for updating thing state:

  function send_state(){

  	//define the payload with sensor values
  	reported_state ={ xpos: read_xpos, ypos: read_ypos, xvel: read_xvel, yvel: read_yvel, sog: read_sog};

  	//create state update payload JSON:
  	device_state={state: { reported: reported_state }};

  	//send update payload to aws:
  	thingShadows.update(Device_Name, device_state );

  };

Credits

Paul Langdon

Paul Langdon

49 projects • 261 followers
Working as a cloud architect for an IoT hardware company

Comments