Tuesday, September 2, 2014

Node.js send string to Arduino Uno

Example to send Hello message from Node.js to Arduino Uno. Due to the Automatic (Software) Reset on Arduino Uno board, it will be reset Node.js open a SerialPort. So we have to insert a dummy delay (3 sec in the example) before first string sent.

reference:
Arduino Uno
DisablingAutoResetOnSerialConnection


Node.js code, nodejsUno.js
//To install 'serialport' locally, enter the command:
//$ npm install serialport
//Otherwise, Error: Cannot find module 'serialport' will reported

var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort('/dev/ttyACM0', 
    {   baudrate: 9600
    });
 
serialPort.on("open", function () {
    console.log('open');

    
    setTimeout(function() {
        serialPort.write("Hello...", function(err, results) {
            console.log('err ' + err);
            console.log('results ' + results);
        });
        
        setTimeout(function() {
            serialPort.write("...from Node.js", function(err, results) {
                console.log('err ' + err);
                console.log('results ' + results);
            });     
        }, 1000);
        
    }, 3000);
    

});

Sketch on Arduino Uno, refer to the post "Read from Arduino Serial port, and write to 2x16 LCD".


No comments:

Post a Comment