Mais jusqu'à récemment il fallait coder un peu de C.
Firmata a generic protocol for communicating with microcontrollers like the Arduino from software on a host computer.
Des librairies existent pour dialoguer en Python, en Ruby...
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
});
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
Firmata + Serial Port
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();
});
Dans un sens : Arduino ➽ 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});
});
});
Démo
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);
});
});
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
});
});
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');
});