Arduino and Node.js via Serial port

Writing to serial in Arduino, reading from node.js

void setup() {
Serial.begin(9600); // Starts the serial communication
}
void loop() {
Serial.println("hello from arduino!");
delay(1000);
}
const SerialPort = require('serialport');
const Readline = require('@serialport/parser-readline');
const port = new SerialPort('/dev/ttyACM0', { baudRate: 9600 });
const parser = port.pipe(new Readline({ delimiter: '\n' }));
// Read the port data
port.on("open", () => {
console.log('serial port open');
});
parser.on('data', data =>{
console.log('got word from arduino:', data);
});
  • In order to use serialport you have to npm install serialport it.
  • You will have to find the name of the serial port in your computer. An easy way to do this is from Arduino IDE, tools > port menu.
  • Make sure to use the same baud rate as the arduino (defined in setup function).
  • Since we used Serial.println (print line), we used the \n delimiter in the Readline parser.
  • Notice how we subscribe to the data event on the parser, and not the port directly.

Reading from the serial in arduino

  1. define a max length for your messages.
  2. define a 1 character long message separator. ie: ‘\n’
  port.write('hello from node\n', (err) => {
if (err) {
return console.log('Error on write: ', err.message);
}
console.log('message written');
});
const byte DATA_MAX_SIZE = 32;
char data[DATA_MAX_SIZE]; // an array to store the received data
void receiveData() {
static char endMarker = '\n'; // message separator
char receivedChar; // read char from serial port
int ndx = 0; // current index of data buffer
// clean data buffer
memset(data, 32, sizeof(data));
// read while we have data available and we are
// still receiving the same message.
while(Serial.available() > 0) {
receivedChar = Serial.read(); if (receivedChar == endMarker) {
data[ndx] = '\0'; // end current message
return;
}
// looks like a valid message char, so append it and
// increment our index
data[ndx] = receivedChar;
ndx++;
// if the message is larger than our max size then
// stop receiving and clear the data buffer. this will
// most likely cause the next part of the message
// to be truncated as well, but hopefully when you
// parse the message, you'll be able to tell that it's
// not a valid message.
if (ndx >= DATA_MAX_SIZE) {
break;
}
}
// no more available bytes to read from serial and we
// did not receive the separato. it's an incomplete message!
Serial.println("error: incomplete message");
Serial.println(data);
memset(data, 32, sizeof(data));
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Showing tooltip only when “text-overflow: ellipsis” is active in Angular8.

Ellipsis is enabled with title.

Let’s Start XD Plugin Development with Yo XD-Plugin

JavaScript ES6 7 Awesome Features

Basic Ninja of JavaScript

How to remove unused CSS in your website

API Response error handling in the best way

The JavaScript Curse! Why do people think it’s difficult?

Airbnb js style

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gustavo Machado

Gustavo Machado

More from Medium

Check if IP Address is a Bot Client Threat in Node.js

How to Develop Linux Applications (Part 1)

Websocket with Cloudflare Tunnel Reversed Proxy to Self-hosted Ubuntu Server

Ruby on Rails 7 with Postgresql and bootstrap CSS, hosting on Heroku