For the software, I wanted to keep the UI loosely coupled from the control, I also have been wondering about blue tooth vs wifi. By using Node.js for the server side components that means the same language (javascript) can be used for all of the software which should make things simpler. Another advantage of Node is because it is multi-platform it can be written and tested without the need for the Edison. So I've been doing this on Windows so far.
Overview
My plan to achieve this is to use an MQTT broker to pass the commands. These will be simple text strings such as "L" for left, "R" for right, "F" for forward etc. Initially, the commands will be sent using WebSockets but it is also possible to send the messages over bluetooth via a client side API and an MQTT-SN to MQTT gateway such as Aquilla . I'm also wondering if I should change these commands to be JSON objects to simplify processing and also to allow the motion controller to queue up commands and hence make the system programmable.
The broker will be run on the Edison so we don't need to have any external dependencies. The idea is to use Mosca to do this. It is possible to use Mosca directly in the code, but my plan is to have a separate Node application which I'm calling the "motion controller" this will take the messages and call the appropriate APIs to make the hardware move.
Code
In its the simplest form the motion controller subscribes to a topic and listens for messages.
#!/usr/bin/env node // Listens for messages on a queue and moves car and driver var mqtt = require('mqtt') console.log('Connecting to Queue') var client = mqtt.connect('ws://localhost:1884'); client.on('connect', function () { client.subscribe('E14_UCDC/+/Commands') }) client.on('message', function (topic, message) { console.log(message.toString()); //Todo: Add movement code here. })
The client side uses a different library as it runs in the browser. In it's simplest form it can send a message to the queue, on response to a button press, as follows:
// Create a client instance var client = new Paho.MQTT.Client("ws://localhost:1884", "CarController"); var message = new Paho.MQTT.Message("F"); message.destinationName = "E14_UCDC/CarController/Commands"; client.send(message);
For connecting to the hardware I'm looking at MRAA which can be accessed from Node and provides a simple way to access digital pins, I2C and PWM. This will be the first area to test once the Edison has arrived.
var mraa = require('mraa'); var pin13 = new mraa.Gpio(13); pin13.dir(mraa.DIR_OUT); pin13.write(1);
Reference
https://github.com/mcollina/mosca
https://eclipse.org/paho/clients/js/
https://github.com/Rodmg/aquila-mqtt-sn-gateway
https://github.com/intel-iot-devkit/mraa
Writing JavaScript for an Intel Edison