This is a slide talking about nodeboat workshop at JSConf Asia 2014.
If you are interested in controlling hardware via javascript, then spark.io is truely a great idea for you to try.
13. - Register at spark.io
- Install spark-cli
- Pair up spark core with your spark.io account
- Try using cli to control the LED (which is D7 pin)
on spark core
Step by step
$ spark setup
$ npm install -g spark-cli
$ spark call {SPARK_CORE_NAME} digitalwrite D7,HIGH
14. - Try request to spark cloud api with http methods
Remote access
GET : respond all available methods
POST : send command
$ curl -X POST -H "Authorization: Bearer {ACCESS_TOKEN}" https://
api.spark.io/v1/devices/{DEVICE_ID}/digitalwrite -d
params=D7,HIGH
$ curl -X GET -H "Authorization: Bearer {ACCESS_TOKEN}" https://
api.spark.io/v1/devices/{DEVICE_ID}
15. - Reasons to use TCP
- faster
- faster, almost realtime
- Load voodoospark firmware
Use TCP instead
$ spark flash {DEVICE_ID} voodoo
17. - Require npm modules
- spark-io
- johnny-five
- Try to control D7 pin
node.js app
var five = require("johnny-five");
var Spark = require("spark-io");
var board = new five.Board({
io: new Spark({
token: process.env.SPARK_TOKEN,
deviceId: process.env.SPARK_DEVICE_ID
})
});
board.on("ready", function() {
var led = new five.Led("D7");
// This js injects the led variable into the
// repl you get afterscript finishes execution.
board.repl.inject({
led: led
});
});
>> led.on()
>> led.off()
35. - express.js web server
Nodejs app
// start
app.get('/start', function (req,
res) {
power = 255;
if (motorR) {
motorR.start(power);
}
if (motorL) {
motorL.start(power)
}
return
res.status(200).send('start');
})
// 80 is center for us // plus degree, max 130
app.get('/left', function (req, res) {
var v;
if (d < 80) {
d = 80;
}
d += 10;
console.log('turn left: ', d);
if (d > 130) {
d = 130;
console.log(d);
return res.status(200).send('maxium 130
degree matched');
}
if (servo) {
v = servo.to(d);
return res.status(200).send(d.toString());
}
})
36. - XMLHttpRequest ajax
Web app
// start
$('#start').click(function () {
defer = $.get('/start', function () {
$('#status').text('Boat started! Speed at 255');
});
});
37. - Rudder didn’t work well
- Motor output from 100 -> 255!
Trial run