SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
1.
Controlling robots
using JavaScript
Sudar Muthu (@sudarmuthu)
Research Engineer, Yahoo! Labs
http://hardwarefun.com
http://github.com/sudar
2.
Who am I?
Research Engineer by profession
Creates robots as hobby
Prefers Arduino
Prefers JavaScript at work
Why not combine both of them?
.. and that’s what I am going to talk about today
4.
Let’s start with a demo
Demo
http://hardwarefun.com/projects/asimijs
5.
Participate in the demo
Visit
http://hardwarefun.com:3000
6.
Control the bot at stage
http://hardwarefun.com/projects/asimijs
#asimijs @hardwarefun
7.
Arduino
Visual Basic for hardware
Includes both Hardware and software
Photo credit Arduino team
8.
Interfacing Arduino with JavaScript
Using serial connection (node-serialport)
Using abstraction (like johnny-five or duino)
Using serial Bluetooth connection (asimijs)
9.
Using Serial Connection
You need node-serialport - https://github.com/voodootikigod/node-serialport
var SerialPort = require("serialport").SerialPort,
arduino = new SerialPort("/dev/tty/ACM1");
// when data is received from arduino
arduino.on("data", function (data) {
arduino.write(new Buffer[data]);
console.log("Got: " + data);
});
// if there was any error
arduino.on("error", function (data) {
console.log("Error: " + data);
});
10.
Using Abstraction
Build on top of node-serialport -
https://github.com/voodootikigod/node-serialport
Options include Johnny-five -
https://github.com/rwldrn/johnny-five and duino -
https://github.com/ecto/duino
Load the firmdata program into Arduino
It provides the necessary abstraction
Write code using a node.js library
Profit
12.
Code
var five = require("johnny-five"),
board = new five.Board();
board.on("ready", function() {
// Create an Led on pin 13
(new five.Led(13)).strobe(1000);
});
14.
Reading sensor data
What you need?
Any sensor and a LED
15.
Reading sensor data
var five = require("johnny-five"),
board, led, photoresistor;
board = new five.Board();
board.on("ready", function() {
// Create a new `photoresistor` hardware instance.
photoresistor = new five.Sensor({pin: "A2”, freq: 250});
led = new five.Led(13);
// "read" get the current reading from the photoresistor
photoresistor.on("read", function( err, value ) {
if (value > 50) {
led.on();
} else {
led.off();
}
console.log( value, this.normalized );
});
// Inject the `sensor` hardware into the Repl instance's context;
// allows direct command line access
board.repl.inject({
pot: photoresistor
});
});
16.
Using serial Bluetooth connection
Bluetooth shield gives wireless capability to Arduino
A Bluetooth connection appears as serial port in your
computer
Using node-serialport you can talk wirelessly to Arduino
And that’s what I did for demo ;)
17.
Using serial Bluetooth connection
var SerialPort = require("serialport").SerialPort,
bt = new SerialPort("/dev/cu.FireFly-CCFA-SPP");
// when data is received from bluetooth
bt.on("data", function (data) {
bt.write(new Buffer[data]);
console.log("Got: " + data);
});
// error reading bluetooth serial port
bt.on("error", function (data) {
console.log("Error: " + data);
});
18.
How the demo worked
Node.js Server
Node Client
Admin page
User page
User page
User page
19.
Links
AsimiJS – The demo that I showed initially
http://hardwarefun.com/projects/asimijs
Asimi – A simple bot using Arduino
http://hardwarefun.com/project/asimi
Getting started with hardware programming
http://hardwarefun.com/tutorials/getting-started-with-
hardware-programming
Getting started with Arduino
http://hardwarefun.com/tutorials/getting-started-with-arduino-
and-avr
Node-serialport https://github.com/voodootikigod/node-
serialport
Johnny Five https://github.com/rwldrn/johnny-five
20.
Questions
Thank You
Sudar Muthu (@sudarmuthu)
http://hardwarefun.com
http://gitbub.com/sudar