Created
December 26, 2013 00:22
-
-
Save finnp/8128225 to your computer and use it in GitHub Desktop.
REST API for an RGB LED.
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
String inputString = ""; // a string to hold incoming data | |
boolean stringComplete = false; // whether the string is complete | |
int red = 0; | |
int green = 0; | |
int blue = 0; | |
int ledR = 9; | |
int ledG = 10; | |
int ledB = 11; | |
void setup() { | |
// initialize serial: | |
Serial.begin(9600); | |
pinMode(ledR, OUTPUT); | |
pinMode(ledG, OUTPUT); | |
pinMode(ledB, OUTPUT); | |
// reserve 200 bytes for the inputString: | |
inputString.reserve(200); | |
} | |
void loop() { | |
// print the string when a newline arrives: | |
if (stringComplete) { | |
Serial.println(inputString); | |
red = getValue(inputString, ',', 0).toInt(); | |
green = getValue(inputString, ',', 1).toInt(); | |
blue = getValue(inputString, ',', 2).toInt(); | |
analogWrite(ledR, red); | |
analogWrite(ledG, green); | |
analogWrite(ledB, blue); | |
// clear the string: | |
inputString = ""; | |
stringComplete = false; | |
} | |
} | |
void serialEvent() { | |
while (Serial.available()) { | |
// get the new byte: | |
char inChar = (char)Serial.read(); | |
// add it to the inputString: | |
inputString += inChar; | |
// if the incoming character is a newline, set a flag | |
// so the main loop can do something about it: | |
if (inChar == '\n') { | |
stringComplete = true; | |
} | |
} | |
} | |
String getValue(String data, char separator, int index) | |
{ | |
int found = 0; | |
int strIndex[] = {0, -1}; | |
int maxIndex = data.length()-1; | |
for(int i=0; i<=maxIndex && found<=index; i++){ | |
if(data.charAt(i)==separator || i==maxIndex){ | |
found++; | |
strIndex[0] = strIndex[1]+1; | |
strIndex[1] = (i == maxIndex) ? i+1 : i; | |
} | |
} | |
return found>index ? data.substring(strIndex[0], strIndex[1]) : ""; | |
} |
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
var SerialPort = require('serialport').SerialPort; | |
var express = require('express'); | |
// Config | |
var localIP = '192.168.2.102'; | |
var arduino = '/dev/tty.usbmodem1411'; | |
var serialPort = new SerialPort(arduino, { | |
baudrate: 9600 | |
}); | |
var app = express(); | |
serialPort.on('open', function () { | |
console.log('Serial connection opened.'); | |
app.get('/color/:r/:g/:b', function(req, res){ | |
changeColor(req.params.r, req.params.g, req.params.b); | |
res.send('Changed color'); | |
}); | |
app.listen(8000, localIP); | |
}); | |
var changeColor = function(r, g, b) { | |
r = Math.floor(r); | |
g = Math.floor(g); | |
b = Math.floor(b); | |
var write = [r, g, b].join(','); | |
console.log('Sending: ' + write); | |
serialPort.write(write + '\n', function(err, results) { | |
if(err) { | |
console.log('Error:' + err); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment