In the first part of this on-going series I was taking a look at controlling the Sphero BB8 droid from a local webpage just to get my feet in the plate. I spent the last couple weeks working on a better IoT oriented sample using a more realistic architecture.
Here is the idea, there are three main components to the project:
- A node.js server sitting in the cloud and listening for incoming connections
- Local controllers running an instance of a node.js script which connects to the cloud server. Those controllers can be any device able to run node.js code and with a bluetooth BLE-capable adapter. It can scan for surrounding BB8 devices and send back that information to the server
- A webpage served by the same cloud application that allows a user to visualise each controller and each device, then connect to any device and send commands to it through the appropriate controller
The diagram below illustrates further the concept. By the way I was using https://www.draw.io to quickly draft it and it looks really handy:
The connection between server and controllers is achieved using socket.io, so pretty standard node.js event oriented implementation. The most remarkable feature - if I may say so - is that I wrote the code using Es7 async feature, so it needs to be transpiled, which can be done on the fly when running the scripts. It is also highly promisified in order to get rid of the callbacks and leverage the async syntax.
The implementation is mostly contained in some "service" classes on the server and the controller, which are then used inside thin wrappers. Below are the implementations of the IoTSvc (server side) and the SpheroSvc (controller side):The UI and interaction with the viewer is the part I have a bit left behind at the moment: the model is actually not being updated if several devices are connected and no feedback is sent from the device to update the location in the 3d space.
It only exposes a panel which allows to visualise connected controllers and available devices, as well as a control panel for the BB8 device. The last enhancement was to play with automated path, so the droid is moving along a predefined path, in that case a square trajectory.
The next goal would be to play with data acquisition feature of the device (position, velocity, ...) and see if it can send some useful information in order to adapt trajectories for example. Also implement basic intelligence, thinking about contouring obstacles, sending video stream using HTML5 APIs is as well on my plate ...
You can grab the whole code from https://github.com/leefsmp/bb8
That little recording was shot in the office this morning, with the help of Kean's droid. Both devices are being controlled from https://iotea.herokuapp.com and affected to a square trajectory