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!
[Solved] What function could I use to send values to Arduino uno with node js?
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter
December 28, 2021 10:24 AM
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.
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
December 30, 2021 8:03 AM
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. 😊Â
(@Anonymous)
Joined: 1 second ago
Posts: 0