Arduino et le web

Meetup arduino du 27/11/2012

Arduino

Arduino

Arduino

Mais jusqu'à récemment il fallait coder un peu de C.

Firmata

Firmata a generic protocol for communicating with microcontrollers like the Arduino from software on a host computer.

Firmata

Des librairies existent pour dialoguer en Python, en Ruby...

Firmata

Et en Javascript !

npm install -g firmata
var firmata = require('firmata');
var board = new firmata.Board('path to usb',function(){
  //arduino is ready to communicate
});

Serial Port

It provides a very simple interface to the low level serial port code necessary to program Arduino chipsets, X10 wireless communications, or even the rising Z-Wave and Zigbee standards.
Le projet sur Github

Johnny Five

Firmata + Serial Port

Johnny Five

npm install johnny-five

var five = require("johnny-five"),
    board = new five.Board();

board.on("ready", function() {
  // Create an Led on pin 13 and strobe it on/off
  (new five.Led(13)).strobe();
});

Dialoguer avec le web


Démos

Dialoguer avec le web

Dans un sens : Arduino ➽ Web

Dialoguer avec le web

Dans un sens : Arduino ➽ Web

var five = require('johnny-five'),
    io = require('socket.io').listen(8080),
    board, potentiometer;

board = new five.Board();

board.on("ready", function() {

  potentiometer = new five.Sensor({
    pin: "A0"
  });

  // "read" get the current reading from the potentiometer
  potentiometer.on("read", function( err, value ) {
    board.emit('value', this.normalized);
  });
});


io.sockets.on('connection', function (socket) {
  board.on('value', function(val){
      socket.emit('value', {val: val});
  });
});

Web ➽ Arduino

Démo

Web ➽ Arduino

var twitter = require('ntwitter'),
    config = require('./config').config,
    five = require("johnny-five"),
    board = new five.Board();

var twit = new twitter(config.twitter);

twit.stream(
    'statuses/filter',
    {'track': 'paris'},
    function(stream) {
        stream.on('data', function (data) {
            board.emit('tweet');
        });
    });

board.on("ready", function() {
    var led = (new five.Led(8));

    board.on('tweet', function(){
        if(led.value) return ;
        led.on();
        setTimeout(function () {
            led.off();
        }, 1000);
    });
});

Démo

Dialoguer avec le web

ou dans l'autre : Web -> Arduino

var five = require('johnny-five'),
    board, servo, socket,
    io = require('socket.io').listen(8081);

io.sockets.on('connection', function (socket) {
    socket.on('move', function(options){
        servo.move(options.val);
    });
});

board = new five.Board();

board.on("ready", function() {
  servo = new five.Servo({
        pin: 8
  });
});
Les possibilités sont infinies

Johnny Five LED display

Aller plus loin

Chrome Web Apps

Serial Access

Serial Access

const device = '/dev/tty.usbmodem621';
const serial = chrome.serial;
const timeout = 100;

var ser = new SerialConnection();
ser.connect(device, function() {
  log('connected to: ' + device);
  ser.write('hello arduino', function() {
  });
  readNextLine();
});

function readNextLine() {
  ser.readLine(function(line) {
    log('readline: ' + line);
    readNextLine();
  });
}

var is_on = false;
document.querySelector('button').addEventListener('click', function() {
  is_on = !is_on;
  ser.write(is_on ? 'y' : 'n');
});

Serial Access

THE END

BY Jonathan Blanchet

jblanche.fr / @jblanchefr

Happy member of the Lab212 collective / @Lab212