2

I'm currently using a pulse sensor on an arduino, and trying to get data from the sensor and trying to display it on the browser. This is what my nodejs code looks like.

var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort("/dev/cu.usbserial-DA011OXR", {
  baudrate: 57600
});

serialPort.on('open', showPortOpen);
serialPort.on('data', saveLatestData);
serialPort.on('close', showPortClose);
serialPort.on('error', showError);

function showPortOpen() {
   console.log('port open. Data rate: ' + serialPort.options.baudRate);
}
 
function showPortClose() {
   console.log('port closed.');
}
 
function showError(error) {
   console.log('Serial port error: ' + error);
}

function saveLatestData(data) {
   console.log(data);
   latestData = data;
}

And this is the kind of data I'm getting on the terminal.

<Buffer 53 35 31 34 0d 0a>
<Buffer 53 35 31 33 0d 0a>
<Buffer 53 35 31 31 0d 0a>
<Buffer 53 35 31 30 0d 0a>
<Buffer 53 35 31 31 0d 0a>
<Buffer 53 35 31 31 0d 0a>
<Buffer 53 35 31 31 0d 0a>
<Buffer 53 35 31 32 0d 0a>
<Buffer 53 35 31 32 0d 0a>
<Buffer 53 35 31 33 0d 0a>
<Buffer 53 35 31 34 0d 0a>
<Buffer 53 35 31 34 0d 0a>
<Buffer 53 35 31 34 0d 0a>
<Buffer 53 35 31 34 0d 0a>
<Buffer 53 35 31 34 0d 0a>
<Buffer 53 35 31 33 0d 0a>
<Buffer 53 35 31 32 0d 0a>
<Buffer 53 35 31 31 0d 0a>

I've been trying to look on the web how I should interpret or parse this data, but I'm pretty clueless. Any help would be great!

fancy_avocado
  • 45
  • 1
  • 6
  • 3
    Since `data` is a `Buffer` object, just call a `toString()` on it, like `console.log(data.toString())`. – Rodrigo Medeiros Apr 06 '15 at 19:17
  • 2
    You should probably look at the arduino docs and see just what you are getting, i.e. the format of the output data. By default Buffer.toString() treats the buffer as utf-8 encoded text, which may well be incorrect; maybe (e.g.) you are getting a 6-byte signal and you need to turn it into a Number. – Plato Apr 06 '15 at 19:45

1 Answers1

0

Set right baudrate. in my case it was 9600.
If you make something like Serial.println(dataFromPin) you could data.toString('ascii')

How could you guess encoding: data[0].toString() and compare with values from arduino IDE Serial Monitor.

ada
  • 666
  • 6
  • 11