I am trying to rink Raspberry Pi and Arduino by serial communication. My purpose is that the user controls a LED of Arduino from Raspberry Pi.
I found an example code of serial communication and it sends a String to Arduino automatically every 2sec. But I want to make two things:
- Change the value sent instead of 'hello'.
- And the user can send the value any time he wants, not automatically.
Can you help me please? I am not good on node.js.
var SerialPort = require("serialport")
var serialPort = new SerialPort('/dev/ttyACM0',
{ baudrate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1,
flowControl: false
});
serialPort.on("open", function () {
console.log('open');
serialPort.on('data', function(data) { // 아두이노로부터 전달된 데이터
console.log('data received: ' + data);
});
serialPort.write("Hello from Raspberry Pi\n", function(err, results) {
console.log('err ' + err);
console.log('results ' + results); //전송한 바이트 수
});
setInterval(
function() { // 2초마다 아두이노에게 문자열을 전송하는 예
serialPort.write('hello');
}, 2000);
});