Search code examples
node.jsarduinoraspberry-piserial-portnode-serialport

Node serial port can not read data


I am trying to read the data from arduino mega to my raspberry pi 3 through serial port. The node code:

const five = require("johnny-five")
const Raspi = require("raspi-io")
const SerialPort = require("serialport")
const Readline = require('@serialport/parser-readline')


const arduinoSerialPort = new SerialPort("/dev/ttyACM0", {
  baudRate: 9600
})
const parser = arduinoSerialPort.pipe(new Readline({ delimiter: '\r\n' }))

const board = new five.Board({
  io: new Raspi(),
  repl: false,
})


board.on('ready', function() {    
  parser.on('data', console.log)
})

arduino code:

int pushButton = 7;

void setup() {
  Serial.begin(9600);
  pinMode(pushButton, INPUT);
}

void loop() {
  int buttonState = digitalRead(pushButton);

  if(buttonState == HIGH){
    Serial.println('ononon');
    digitalWrite(LED_BUILTIN, LOW);
  }else{
    Serial.println('offfff');
    digitalWrite(LED_BUILTIN, HIGH);
  }

  delay(100);
}

Arduino has a push button and and I am sending different data when the button is pushed or not.

On the node side I can't seem to read the data. All I get is some numbers. I tried to use toString() did not work either.

I have been stuck here for long, some help would be really appreciated.


Solution

  • Traditional silly mistake :p

    Serial.println('offfff');
    

    C/C++ uses double quote sign " for string.

    Serial.println("offfff");
    

    Too much JavaScripting :v

    Although in the arduino serial monitor I could see the line before so I did not think of looking into the arduino code.