Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



What function could...
 
Share:
Notifications
Clear all

What function could I use to send values to Arduino uno with node js?

3 Posts
3 Users
0 Likes
584 Views
 Anonymous
Joined: 53 years ago
Posts: 0
Topic starter  

I have a problem, my code in Arduino allows me to send values to turn on a led with Serial.read (), but when I do it with node js port.write () I have tried several ways but it does not turn on the led.

Arduino code

int pin = 12;
String vali="OFF";
char val;

void setup() {
  pinMode(pin,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  val = Serial.read();
  if(val == '1'){
    digitalWrite(pin,HIGH);
    vali="ON";
  }
  else if(val == '0'){
    digitalWrite(pin,LOW);
    vali="OFF";
  }
    delay(1000);
    Serial.println(" estado:"+ vali );
}

 

js code

const Serialport = require('serialport');
const readline = Serialport.parsers.Readline;
var val = 0;
const port = new Serialport('COM3', {
    baudRate: 9600
});
const parse = port.pipe(new readline({ delimiter: '\r\n' }));

port.on('open', function() {
    console.log('conect ');
});

port.write("1\r\n");
parse.write("1\r\n");

parse.on("data", (data) => {
    console.log(data);
});

 

 

------------------------------------------

code from arduino books.


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 9 years ago
Posts: 2234
 

I honestly have never used JS to write to a serial port ...

However, and I could be very wrong of course, you're using a readline parser?
Somehow sounds wrong ...
Maybe these examples help you in the right direction?
Not sure if that is helpful. 😊 


ReplyQuote
 Anonymous
Joined: 53 years ago
Posts: 0
 

thank you for the tip


ReplyQuote
Share: