Ömer Alkin
Ömer Alkin

Reputation: 303

NodeJS serial port value reading issue

I am new to Node.JS and Arduino. I have an Arduino setup and a couple of sensors on it. I am reading temperature and humidity values with Arduino. My serial monitor output like this:

Humiditiy (%): 44.00
Temperature (Celcius): 26.00
Temperature (Kelvin): 299.00
Temperature (Fahrenheit): 58.00
Gas Value: 341

Humiditiy (%): 44.00
Temperature (Celcius): 26.00
Temperature (Kelvin): 299.00
Temperature (Fahrenheit): 58.00
Gas Value: 341

I want three things:

  1. Using NodeJS and pulling Serial Monitor outputs.
  2. Storing values with MongoDB
  3. Sending values to the website that I created.

I did try to pull the values from serial monitor with this NodeJS file and put the output to the console.

// Setup basic express server
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);

// Routing
var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort("/dev/ttyACM0", {
    baudrate:115200
}, false); // this is the openImmediately flag [default is true]

serialPort.open(function () {
  serialPort.on('data', function(data) {
    console.log('Receiving data' + data);
  });
});

But the output in the terminal is weirdly broken, yet this was the best output I get somehow.

Receiving dataty (%)
Receiving data: 44.00
Temperature (Celcius): 26.00
Temperature 
Receiving data(Kelvin): 299.00
Temperature (Fahrenheit): 58.00
Receiving data

Receiving dataz
Receiving dataas Value: 380

And now I am working on how to use Mongo with the serial port. Any help will be appreciated.

Upvotes: 0

Views: 4036

Answers (2)

Vishnu VS
Vishnu VS

Reputation: 1

The below code works with latest serialport libray changes

var serialport = require('serialport');
serialport.list(function (err, ports) {

    ports.forEach(function(port) {
    console.log(port.comName);
    });
});
var portName="COM1";
var myPort = new serialport(portName, 9600);
var Readline = serialport.parsers.Readline; // make instance of Readline parser
var parser = new Readline(); // make a new parser to read ASCII lines
myPort.pipe(parser); // pipe the serial stream to the parser
myPort.on('open', showPortOpen);
parser.on('data', readSerialData);
function showPortOpen(){
    console.log("Port opened");
}

function readSerialData(data){
    console.log("data received "+data);
}

Upvotes: 0

Ömer Alkin
Ömer Alkin

Reputation: 303

I found the answer of my problem.

The way the program posted above is implemented, serialport will fire 'data' events as often and as quickly as it can. It will not wait for a complete line of text to be received before firing the 'data' event which is what I think you are expecting. If the Arduino sketch is outputting information as quickly as it can at 115200 baud then the Node.js program is going to have difficulties when attempting to start reading. Perhaps this is the problem. The Node.js program and Arduino should probably coordinate their communication.

There is a text reading line by line concept in Serial Port that called Parsers. Which fixed my issue too.

var serialPort = new SerialPort("/dev/ttyACM0", {
    autoOpen: false,
    parser: SerialPort.parsers.readline('\n'),
    baudrate:115200
}); 

Changing code like this worked because now it is trying to read line by line.

Upvotes: 1

Related Questions