In order to use serialport in Node.js to send message to serial port, enter the command on Raspberry Pi command line to install serialport.
$ npm install serialport
Modify index.js in last post to add handles for serialport.
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort('/dev/ttyACM0',
{ baudrate: 9600
});
serialPort.on("open", function () {
console.log('open');
});
app.get('/', function(req, res){
res.sendfile('index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
serialPort.write(msg, function(err, results) {
console.log('err ' + err);
console.log('results ' + results);
});
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
For the sketch on Arduino Uno, refer to the post "Read from Arduino Serial port, and write to 2x16 LCD".
No comments:
Post a Comment