Arduino talks back to Node.js - Drama on the Serial Port

Download this episode

Download Video

Description

Ever wanted to communicate with an Arduino using Node? We'll explore how to write values from an Arduino and get capture those values in a Node app using serial communication. 

This tutorial will show you how to use the node-serialport module https://github.com/voodootikigod/node-serialport to communicate with an Arduino, in this case an Arduino Uno.

You will need:

 

1. Program Your Arduino

[00:27]

 Create a new Arduino sketch in the Arduino IDE, and set the Serial to begin at 9600. Then have it write back a few lines.

void setup(){
Serial.begin(9600);
}

void loop(){
  Serial.println("test");
  delay(5000);
}

To upload this to your Arduino board, you need to make sure the right board is selected under Tools>Board and that you have the right serial port selected. If you don't know what serial port your Arduino is on, you can do the following: 

Windows

Open your device manager. Look at the COM ports. See what com port the Arduino shows up on. 

OSX:

Open terminal. Type: 

ls /dev/tty.*
ls /dev/cu.*

 [01:34]

2. Install the node-serialport module 

npm install serialport --save

 [02:05]

3. Create an appj.js script in the same folder that you installed the module. Require the module. 

var serialport = require("serialport");

 [02:34]

4. We will pass in the port that the Arduino is running on via the command line as an argument when we run this node application. We will need to get that from the arguments in the script.

We will call the script like this:

node app.js COM8

And we will capture that argument in the node application: 

var port = process.argv[2];

[03:00]

5. We will create an instance of the serialport module and then we will add listeners to some of its events that it emits. We are configuring the instance to listen at a baud rate of 9600 ( baud rate is how fast the data is transmitted and received ) and we will configure the instance to use the read line parser, so we can get the whole line and not data bits. Whatever baud rate  you set your arduino to communicate at , you must also use here. 

var sp = new SerialPort(port, {
  baudrate: 9600,
  parser: serialport.parsers.readline("\n")
});


sp.on('open', onPortOpen);
sp.on('data', onData);
sp.on('close', onClose);
sp.on('error', onError);


function onPortOpen(){
    console.log("YESSIR THE PORT IS OPEN COS CAPS");
}

function onData(d)
{
    console.log("data dis, data dat "+d)
}

function onClose(){
    console.log("Port is closed, yo");
}
function onError(){
    console.log("something went horribly wrong");
}

 And that's it. Simple communication between Node.js and an Arduino. Now time to make all the weather apps and blink blink blanks you can possibly imagine! 

 

Embed

Format

Available formats for this video:

Actual format may change based on video formats available and browser capability.

    The Discussion

    • User profile image
      jasonrwalte​rs

      So much awesome can be made from this tutorial, thank you! 

    • User profile image
      Jim Stanley

      Thanks for your tutorial. I'm having problems installing serialport on my system -- I get compile errors.

      I'm using Visual Studio 2015 on Windows 8.1 and node version 4.1.1.

      Please tell me what versions of these tools you are using.

      Thanks.

    Comments closed

    Comments have been closed since this content was published more than 30 days ago, but if you'd like to send us feedback you can Contact Us.