Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Nodebotsday

mulderp
July 27, 2015

 Nodebotsday

Overview on Nodebots

mulderp

July 27, 2015
Tweet

More Decks by mulderp

Other Decks in Technology

Transcript

  1. Basic JavaScript var robot = {! color: ‘yellow’,! sound: ‘hello’,!

    ! blink: function() {! // action! }! }! ! robot.blink(); basic data functions as data
  2. event-loop var repl = require('repl');! ! var robot = {!

    blink: function(color, delay) {! setTimeout(function() {! console.log('** ' + color + ' **');! }, delay)! }! }! ! var local = repl.start('command> ');! local.context.robot = robot;!
  3. $ npm install --save firmata! ! // The Firmata protocol

    provides a simple protocol to an embedded system! var firmata = require('firmata');! ! // Change this line to the serial port of a device! var modem = '/dev/cu.usbserial-DJ008CEA';! ! // Main part! var board = new firmata.Board(modem, function(err){! console.log('connected: ' + modem);! var ledOn = true;! ! // Configure pin 13 as output! board.pinMode(13, board.MODES.OUTPUT);! ! ! // Blink the LED! setInterval(function() {! ! if (ledOn) {! console.log('ON');! board.digitalWrite(13, board.HIGH);! } else {! console.log('OFF');! board.digitalWrite(13, board.LOW);! }! ! ledOn = !ledOn;! ! }, 500);! });!
  4. Basic LED var five = require("johnny-five");! var board = new

    five.Board();! ! board.on("ready", function() {! ! var led = new five.Led(13);! ! // "blink" the led in 500ms on-off phase periods! led.blink(500);! }); https://github.com/rwaldron/johnny-five/blob/master/eg/led-blink.js
  5. Nodebot var five, temporal, Nodebot;! ! five = require("johnny-five");! !

    temporal = require("temporal");! ! five.Board().on("ready", function() {! ! Nodebot = new five.Nodebot({! right: 10,! left: 11! });! ! this.repl.inject({! n: Nodebot! });! ! })! ! https://github.com/rwaldron/johnny-five/blob/master/eg/nodebot.js
  6. Proximity var five = require("johnny-five");! var board = new five.Board();!

    ! board.on("ready", function() {! var proximity = new five.Proximity({! controller: "SRF10"! });! ! proximity.on("data", function() {! console.log(this.cm + "cm", this.in + "in");! });! ! proximity.on("change", function() {! console.log("The obstruction has moved.");! });! });
  7. Direct I2C https://www.npmjs.com/package/i2c var i2c = require('i2c');! var address =

    0x18;! var wire = new i2c(address, {device: '/dev/i2c-1'}); // point to your i2c address, debug provides REPL interface ! ! wire.scan(function(err, data) {! // result contains an array of addresses ! });! ! wire.writeByte(byte, function(err) {});! ! wire.writeBytes(command, [byte0, byte1], function(err) {});! ! wire.readByte(function(err, res) { // result is single byte }) ! ! wire.readBytes(command, length, function(err, res) {! // result contains a buffer of bytes ! });! ! wire.on('data', function(data) {! // result for continuous stream contains data buffer, address, length, timestamp ! });!
  8. Server and adding sockets: https://github.com/lmccart/itp-creative-js/blob/master/Spring-2014/week6/04_socket_server/server.js var http = require('http');! var

    fs = require('fs');! var port = 3474;! ! // we create the server here! http.createServer(function (req, res) {! // simple logging of an incoming request! console.log(req.url);! ! // simple routing! if (req.url == '/') {! res.writeHead(200, {'Content-Type': ‘text/html'});! ! // serving a file! res.end(fs.readFileSync('static/index.html'));! }! }).listen(port);!