Created
April 24, 2014 05:32
-
-
Save jacoby/11242631 to your computer and use it in GitHub Desktop.
Cheerlights Ethernet So Far
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
/* | |
* Cheerlights Ethernet - Dave Jacoby | |
* | |
* Using an Ethernet Shield to connect to Cheerlights and set the RGB LED light | |
* | |
* http://api.thingspeak.com/channels/1417/field/1/last.txt | |
* | |
* Sketch to set control of an RGB LED | |
* | |
* Black wire connecting 5v to LED pin 2 | |
* Red wire (red color) connecting A pin 1 to LED 1 | |
* Green wire (green color) connecting A pin 2 to LED 3 | |
* Blue wire (blue color) connecting A pin 3 to LED 4 | |
*/ | |
#include <SPI.h> | |
#include <Ethernet.h> | |
const int redPin = 3 ; | |
const int greenPin = 5 ; | |
const int bluePin = 6 ; | |
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Arduino's MAC | |
IPAddress ip(192, 168, 1, 177); // Arduino's Address | |
char server[] = "api.thingspeak.com"; | |
EthernetClient client; | |
int led = 0 ; | |
int delay_length = 1000 ; | |
// Cobble together these with ascii. | |
char blu[] = {13, 98, 108, 117} ; // blue | |
char cya[] = {13, 99, 121, 97} ; // cyan | |
char dar[] = {13, 100, 97, 114} ; // dark | |
char gre[] = {13, 103, 114, 101} ; // green | |
char mag[] = {13, 109, 97, 103} ; // magenta | |
char ora[] = {13, 111, 114, 97} ; // orange | |
char pur[] = {13, 112, 117, 114} ; // purple | |
char red[] = {13, 114, 101, 100} ; // newline r e d | |
char war[] = {13, 119, 97, 114} ; // warm white | |
char whi[] = {13, 119, 104, 115} ; // white | |
char yel[] = {13, 121, 101, 108} ; // yellow | |
// ================================================================================ | |
void setup() { | |
Serial.begin(9600); | |
delay(100) ; | |
// start the Ethernet connection: | |
if (Ethernet.begin(mac) == 0) { | |
Serial.println("Failed to configure Ethernet using DHCP"); | |
Ethernet.begin(mac, ip); | |
} | |
// give the Ethernet shield a second to initialize: | |
Serial.println("connecting..."); | |
pinMode( led , OUTPUT ) ; | |
c_dark() ; | |
} | |
// ================================================================================ | |
void loop() { | |
get_lights( server ) ; | |
delay( delay_length ) ; | |
} | |
// ================================================================================ | |
void get_lights( char server[] ) { | |
Serial.print( server ) ; | |
Serial.println( " ------------------------------" ) ; | |
if (client.connect(server, 80)) { | |
Serial.println("connected"); | |
// Make a HTTP request: | |
client.println("GET /channels/1417/field/1/last.txt HTTP/1.1"); | |
client.println("Host: api.thingspeak.com"); | |
client.println("Connection: close"); | |
client.println(); | |
delay( 500 ) ; // delay to make sure client is available | |
if (client.available()) { | |
char matchbuf[4] ; | |
char newline[] = {13, 10} ; | |
while (client.available() > 0) { | |
matchbuf[0] = matchbuf[1]; | |
matchbuf[1] = matchbuf[2]; | |
matchbuf[2] = matchbuf[3]; | |
matchbuf[3] = client.read(); | |
if ( matchbuf == blu ) { | |
c_blue() ; | |
} | |
if ( matchbuf == cya ) { | |
c_cyan() ; | |
} | |
if ( matchbuf == dar ) { | |
c_dark() ; | |
} | |
if ( matchbuf == gre ) { | |
c_green() ; | |
} | |
if ( matchbuf == mag ) { | |
c_magenta() ; | |
} | |
if ( matchbuf == ora ) { | |
c_orange() ; | |
} | |
if ( matchbuf == pur ) { | |
c_purple() ; | |
} | |
if ( matchbuf == red ) { | |
c_red() ; | |
} | |
if ( matchbuf == war ) { | |
c_warmwhite() ; | |
} | |
if ( matchbuf == whi ) { | |
c_white() ; | |
} | |
if ( matchbuf == yel ) { | |
c_yellow() ; | |
} | |
Serial.print( "=====" ) ; | |
Serial.print(blu) ; | |
Serial.print( " " ) ; | |
Serial.println(matchbuf) ; | |
} | |
} | |
else { | |
Serial.println(); | |
Serial.println("disconnecting."); | |
} | |
} | |
else { | |
Serial.println("connection failed"); | |
} | |
client.stop() ; | |
} | |
// ================================================================================ | |
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) { | |
Serial.print( red ) ; | |
Serial.print( "\t" ) ; | |
Serial.print( green ) ; | |
Serial.print( "\t" ) ; | |
Serial.println( blue ) ; | |
analogWrite( redPin , red ) ; | |
analogWrite( greenPin , green ) ; | |
analogWrite( bluePin , blue ) ; | |
} | |
// ================================================================================ | |
void c_red() { | |
setColourRgb( 0, 255, 255 ) ; | |
} | |
// ================================================================================ | |
void c_green() { | |
setColourRgb( 255, 0, 255 ) ; | |
} | |
// ================================================================================ | |
void c_blue() { | |
setColourRgb( 255, 255, 0 ) ; | |
} | |
// ================================================================================ | |
void c_cyan() { | |
setColourRgb( 255, 0, 0 ) ; | |
} | |
// ================================================================================ | |
void c_white() { | |
setColourRgb( 0, 0, 0 ) ; | |
} | |
// ================================================================================ | |
void c_warmwhite() { | |
setColourRgb( 0, 32, 32 ) ; | |
} | |
// ================================================================================ | |
void c_purple() { | |
setColourRgb( 128, 255, 128 ) ; | |
} | |
// ================================================================================ | |
void c_magenta() { | |
setColourRgb( 0, 255, 0 ) ; | |
} | |
// ================================================================================ | |
void c_yellow() { | |
setColourRgb( 0, 0, 255 ) ; | |
} | |
// ================================================================================ | |
void c_orange() { | |
setColourRgb( 0, 165, 255 ) ; | |
} | |
// ================================================================================ | |
void c_dark() { | |
setColourRgb( 255, 255, 255 ) ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment