Git Product home page Git Product logo

nodejs-arduino-example's Introduction

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

Circuit map

Photo

nodejs-arduino-example's People

Contributors

gianlucaguarini avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

nodejs-arduino-example's Issues

Fix the path and help about command line

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);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.