Last active
August 29, 2015 14:19
-
-
Save djumaka/7cbcb1daaf98466dea5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = ""; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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