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?


(@cloudytechi147)
New Member
Joined: 4 weeks ago
Posts: 1
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)
Prominent Member Admin
Joined: 9 years ago
Posts: 996
 

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
(@amidr)
New Member
Joined: 2 months ago
Posts: 1
 

thank you for the tip


ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: