nodejs-arduino-example
Simple example to show how to use arduino to communicate digital signals to a nodejs server
Arduino circuit used in this example
Simple example to show how to use arduino to communicate digital signals to a nodejs server
License: MIT License
Simple example to show how to use arduino to communicate digital signals to a nodejs server
Arduino circuit used in this example
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
fs = require('fs'),
url = require('url'),
SerialPort = require('serialport').SerialPort,
// initialize serialport using the /dev/cu.usbmodem1411 serial port
// remember to change this string if your arduino is using a different serial port
// To know your device on linux just execute this command line bellow
// dmesg | tail | grep tty
// Most important! To discover your device on the linux just execute on terminal this command line bellow
// dmesg | tail | grep tty
// In my case show: [30514.044931] cdc_acm 3-1.4:1.0: ttyACM0: USB ACM device
sp = new SerialPort('/dev/ttyACM0', {
baudRate: 9600
}),
// this var will contain the message string dispatched by arduino
arduinoMessage = '',
/**
helper function to load any app file required by client.html
@param { String } pathname: path of the file requested to the nodejs server
@param { Object } res: http://nodejs.org/api/http.html#http_class_http_serverresponse
*/
readFile = function(pathname, res) {
// an empty path returns client.html
if (pathname === '/')
pathname = '../client.html';
fs.readFile('../client/' + pathname, function(err, data) {
if (err) {
console.log(err);
res.writeHead(500);
return res.end('Error loading client.html');
}
res.writeHead(200);
res.end(data);
});
},
/**
*
This function is used as proxy to print the arduino messages into the nodejs console and on the page
@param { Buffer } buffer: buffer data sent via serialport
@param { Object } socket: it's the socket.io instance managing the connections with the client.html page
*
*/
sendMessage = function(buffer, socket) {
// concatenating the string buffers sent via usb port
arduinoMessage += buffer.toString();
// detecting the end of the string
if (arduinoMessage.indexOf('\r') >= 0) {
// log the message into the terminal
// console.log(arduinoMessage);
// send the message to the client
socket.volatile.emit('notification', arduinoMessage);
// reset the output string to an empty value
arduinoMessage = '';
}
};
// creating a new websocket
io.sockets.on('connection', function(socket) {
// listen all the serial port messages sent from arduino and passing them to the proxy function sendMessage
sp.on('data', function(data) {
sendMessage(data, socket);
});
// listen all the websocket "lightStatus" messages coming from the client.html page
socket.on('lightStatus', function(lightStatus) {
sp.write(lightStatus + '\r', function() {
// log the light status into the terminal
console.log('the light should be: ' + lightStatus);
});
});
});
// just some debug listeners
sp.on('close', function(err) {
console.log('Port closed!');
});
sp.on('error', function(err) {
console.error('error', err);
});
sp.on('open', function() {
console.log('Port opened!');
});
// L3T'S R0CK!!!
// creating the server ( localhost:8000 )
app.listen(8000);
// server handler
function handler(req, res) {
readFile(url.parse(req.url).pathname, res);
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.