Sample code to control a BB-8 Droid from anywhere in the world
Suppose your Star Wars BB-8 droid is in Melbourne and you are in San Francisco. You are controlling the BB-8 from an internet browser from San Francisco!! Wouldn't it be nice?
This is a sample project to control a BB-8 Droid using MQTT, the IBM internet of Things Foundation, and the sphero.js, . This project also uses the MQTT wrapper from the parrot-sample.
You can control the color of the droid using a Node-RED flow. Sample flow has been included below. The summary of functionalities is:
- BB-8 will change color to Green when connected to IoTF via BLE compatible laptop.
- You can issue color change command
- You can issue run command and droid will run for 1.5 seconds and then randomly change color every second.
Running The Sample
Hardware
You will require the following devices for this demo.- A BB-8 Droid
- A laptop which supports BLE. The BB-8 will be connected to the laptop via BLE. BB-8 controller code will run on the laptop. I used a MacBook Pro (Retina, 15-inch, Mid 2014).
iot-2/cmd/run/fmt/json
topic.Getting The Code
$ git clone https://github.com/shamimshossain/bb8-bluemix.git
Pre-requisite tasks
Create an instance of Internet of Things Foundation from Bluemix catalog.From IoTF Dashboard, perform the following tasks.
- Click the Devices tab and click Add Device.
- Create a new Device Type called "sphero".
- In the Device ID field enter a unique ID for your drone.
- Click Continue
The IoTF service can not retrieve these authentication details for you later. So make sure to store these in a secure place
Next get API keys for your application.
- Click on the API Keys tab
- Click the New API Key link
- You will see a Key and Auth Token displayed.
The IoTF service can not retrieve these authentication details for you later. So make sure to store these in a secure place
In the root of the repository you cloned you will find a file named
sphero-config.properties
. Following is the content of the properties file.#Device ID you use when you register with the IoT foundation
deviceid=yourdroneid
#For controller (device)
authtoken=yourauthtoken
#For the app (publisher)
apikey=yourapikey
apitoken=yourapitoken
Replace the values of the deviceid, authtoken, apikey, and apitoken
with the values you got when creating the device and app in the IoT
Dashboard.Following is a screenshot of IoTF dashboard.
Set up the application to run from your laptop
- Open a terminal window,
cd
to the root of the repository, and runnpm install
to install all the dependencies. - Place your BB-8 near your BLE compatible laptop.
- Now start up the controller code by running
node spheroDemo.js
- Your BB-8 droid will be connected to IoTF via BLE compatible laptop and change color to green. You will see an output similar to following from the terminal.
**** Start debug info *****
Connected to BB-8
BB-8 is changing color to green to indicate that it is connected
**** End debug info *****
r6co3p.messaging.internetofthings.ibmcloud.com
Output of ping command
{ sop1: 255,
sop2: 255,
mrsp: 0,
seq: 0,
dlen: 1,
data: <Buffer >,
checksum: 254 }
End of ping data
MQTT client connected to IBM IoT Cloud.
Connected Sphero ID: spheroownedbyshamim
subscribed to iot-2/cmd/run/fmt/json
- You are now ready to create an Node-RED Starter app from Bluemix catalog.
Node-RED
- Bind the IoTF service created before to this app. The resultant application dashboard looks like the diagram above.
- Now copy the following Node-RED flow and import into Node-RED.
[{"id":"180c60f4.e7f39f","type":"ibmiot out","z":"5fa8453c.a057bc","authentication":"boundService","apiKey":"","outputType":"cmd","deviceId":"spheroownedbyshamim","deviceType":"sphero","eventCommandType":"run","format":"json","data":"___","name":"IBM IoT App Out","service":"registered","x":756,"y":361,"wires":[]},{"id":"b7792b22.4886d8","type":"inject","z":"5fa8453c.a057bc","name":"Run command","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":false,"x":206,"y":444,"wires":[["f29b07a3.0d64f8"]]},{"id":"f29b07a3.0d64f8","type":"function","z":"5fa8453c.a057bc","name":"Construct RUN command","func":"msg.payload = JSON.stringify({\n d: {\n action : \"#spherorun\"\n }\n});\nreturn msg;","outputs":1,"noerr":0,"x":468,"y":449,"wires":[["180c60f4.e7f39f","d1c575b9.2e3a88"]]},{"id":"d313ea30.2cec18","type":"inject","z":"5fa8453c.a057bc","name":"Change color to RED","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":false,"x":205,"y":297,"wires":[["26e811fe.d917ee"]]},{"id":"26e811fe.d917ee","type":"function","z":"5fa8453c.a057bc","name":"Construct RED command","func":"msg.payload = JSON.stringify({\n d: {\n action : \"#red\"\n }\n});\nreturn msg;","outputs":1,"noerr":0,"x":473,"y":287,"wires":[["180c60f4.e7f39f","d1c575b9.2e3a88"]]},{"id":"61c2c549.9e3d3c","type":"inject","z":"5fa8453c.a057bc","name":"Change color to BLUE","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":false,"x":193,"y":369,"wires":[["18100517.e7effb"]]},{"id":"18100517.e7effb","type":"function","z":"5fa8453c.a057bc","name":"Construct BLUE command","func":"msg.payload = JSON.stringify({\n d: {\n action : \"#blue\"\n }\n});\nreturn msg;","outputs":1,"noerr":0,"x":485,"y":363,"wires":[["180c60f4.e7f39f","d1c575b9.2e3a88"]]},{"id":"d1c575b9.2e3a88","type":"debug","z":"5fa8453c.a057bc","name":"","active":true,"console":"false","complete":"false","x":777,"y":286,"wires":[]}]
Once you have imported the flow you will need to double click on the
IBM IoT output node to open the configuration properties and replace the
device ID with the device ID you registered your drone with in Bluemix.
For example, replace spheroownedbyshamim
with your deviceid.This is how the flow looks like.