Created
April 4, 2011 15:21
-
-
Save karlosgliberal/901812 to your computer and use it in GitHub Desktop.
Conectando nodejs a arduino mediante el puerto serie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Ejemplo sencillo para poder manejar un motor servo conectado aun ardunio desde nodejs | |
//app.js fichero principal tipo express | |
var express = require('express'); | |
//npm install serialport | |
//https://github.com/voodootikigod/node-serialport | |
var serialport = require("serialport"); | |
var SerialPort = serialport.SerialPort; // localize object constructor | |
var app = module.exports = express.createServer(); | |
// Configuration | |
app.configure(function(){ | |
app.set('views', __dirname + '/views'); | |
app.set('view engine', 'jade'); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(app.router); | |
app.use(express.static(__dirname + '/public')); | |
}); | |
app.configure('development', function(){ | |
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | |
}); | |
app.configure('production', function(){ | |
app.use(express.errorHandler()); | |
}); | |
// Routes | |
app.get('/', function(req, res){ | |
res.render('index', { | |
title: 'Express' | |
}); | |
}); | |
//hacemos get desde jquery /public/javascripts/aplicacion.js | |
app.get('/volumen/:id', function(req, res){ | |
console.log(req.params.id); | |
var serial_port = new SerialPort("/dev/tty.usbserial-A4000PDf", {baudrate: 9600}); | |
serial_port.write(req.params.id); | |
serial_port.close(); | |
}); | |
// Only listen on $ node app.js | |
if (!module.parent) { | |
app.listen(3000); | |
console.log("Express server listening on port %d", app.address().port); | |
} | |
//aplication.js cliente jquery | |
-- VISUAL LINE -- | |
(function() { | |
$( "#slider-vertical" ).slider({ | |
orientation: "vertical", | |
range: "min", | |
min: 0, | |
max: 180, | |
value: 60, | |
slide: function( event, ui ) { | |
if(ui.value <=90){ | |
$.get("volumen/z"); | |
}else if(ui.value <=160){ | |
$.get("volumen/a"); | |
} | |
$( "#amount" ).val( ui.value ); | |
} | |
}); | |
$( "#amount" ).val( $( "#slider-vertical" ).slider( "value" ) ); | |
})(); | |
//layout.jade mirar documentación de express, pero añade cabecera y script | |
!!! 5 | |
html | |
head | |
title= 'Calculador Consumo Co2' | |
link(rel='stylesheet', href='/stylesheets/style.css') | |
link(rel='stylesheet', href='/stylesheets/aristo/jquery-ui-1.8.5.custom.css') | |
script(type='text/javascript', src='/javascripts/json.js') | |
script(type='text/javascript', src='https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js') | |
script(type='text/javascript', src='https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js') | |
script(type='text/javascript', src='/javascripts/jquery.dataTables.js') | |
script(type='text/javascript', src='/javascripts/jgcharts.js') | |
body | |
!= body | |
script(type='text/javascript', src='/javascripts/application.js') | |
//index.jade se accede cuando se hace una petición get a express | |
#demo | |
label(for="amount")Volume | |
input(type="text", id="amount", style="border:0; color:#f6931f; font-weight:bold;") | |
#slider-vertical(style="height:200px;") | |
//#########################Arduino########################## | |
//Sección arduino | |
#include <Servo.h> | |
#define LED 13 | |
Servo myservo; | |
int position = 0; | |
byte inbyte = 0; | |
boolean active = false; | |
void setup() | |
{ | |
myservo.attach(9); // attaches the servo on pin 9 to the servo object | |
pinMode(LED, OUTPUT); | |
Serial.begin(9600); | |
Serial.println("servo attached"); | |
myservo.write(position); | |
delay(1000); | |
} | |
void loop() | |
{ | |
inbyte = Serial.read(); //Read one byte (one character) from serial port. | |
if(inbyte == 'a'){ | |
position += 10; | |
Serial.print("datos"); | |
Serial.println (position); | |
int dato; | |
dato = myservo.read(); | |
Serial.println(dato); | |
myservo.write(position); | |
} | |
if(inbyte == 'z'){ | |
position -= 10; | |
Serial.print("datos"); | |
Serial.println (position); | |
int dato; | |
dato = myservo.read(); | |
Serial.println(dato); | |
myservo.write(position); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment