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!
Ad Blocking Detected
Please consider disabling your ad blocker for our website.
We rely on these ads to be able to run our website.
You can of course support us in other ways (see Support Us).
What function could I use to send values to Arduino uno with node js?
Joined: 53 years ago
Posts: 0
Topic starter
December 28, 2021 9: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: 10 years ago
Posts: 2457
December 30, 2021 7: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. 😊
Joined: 53 years ago
Posts: 0
Ad Blocking Detected
Please consider disabling your ad blocker for our website.
We rely on these ads to be able to run our website.
You can of course support us in other ways (see Support Us).