Skip to content

Instantly share code, notes, and snippets.

@djumaka
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djumaka/7cbcb1daaf98466dea5b to your computer and use it in GitHub Desktop.
Save djumaka/7cbcb1daaf98466dea5b to your computer and use it in GitHub Desktop.
int ledPin = 13;
String cmd = "";
String tmpCmd = "";
boolean readCmd = false;
void setup () {
Serial.begin (9600);
pinMode(ledPin, OUTPUT);
}
void loop () {
while (Serial.available()) {
char c = Serial.read();
if(c == '$' && !readCmd) {
readCmd = true;
cmd = tmpCmd;
tmpCmd = "";
}
else {
tmpCmd += c;
}
if(readCmd) {
if(cmd == "LED13") {
Serial.println("LED ON");
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
} else {
Serial.println("Got:" + cmd);
}
readCmd = false;
cmd = "";
}
}
}
console.log("Starting");
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
process.stdin.resume();
process.stdin.setEncoding('utf8');
var util = require('util');
// list serial ports:
serialport.list(function (err, ports) {
ports.forEach(function(port) {
console.log(port.comName);
});
});
var myPort = new SerialPort("/dev/ttyACM0", {
baudRate: 9600,
// look for return and newline at the end of each data packet:
parser: serialport.parsers.readline("\n")
});
function saveLatestData(data) {
console.log("Serial data: " + data);
}
function showPortClose() {
console.log('port closed.');
}
function showPortOpen() {
console.log('port opened');
myPort.write(process.argv[2]+"$");
}
function showError(error) {
console.log('Serial port error: ' + error);
}
myPort.on('open', showPortOpen);
myPort.on('data', saveLatestData);
myPort.on('close', showPortClose);
myPort.on('error', showError);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment