Close
Sign in to view the code!
Sign in to send to cube!
CheerLights
Created by alex on January 14, 2016
Twitter
Facebook
This program makes the cube interact with CheerLights -- when your cube i...
Comments:
Sign up, to be the first!
Up next
FirstApp
Update Demo
Sign up, make your own!
#define MAX_BRIGHTNESS 55 //how often the cube polls the cheerlights API #define POLLING_INTERVAL 3000 //this program makes the cube interact with CheerLights -- when your cube is running this, its colors are //synchronized with more than 15,000 other IoT devices around the world. //You can change the color of your cube and all the other CheerLights by tweeting the name of a color to //@CheerLights //for example, if you tweet // @CheerLights blue //your cube will immediately turn blue. //for more detail, check out http://cheerlights.com/about/ void fill(Color col); Cube cube=Cube(); void setup() { cube.begin(); cube.background(black); Serial.begin(115200); } void loop() { String response=getRequest("api.thingspeak.com", "/channels/1417/field/2/last.txt"); int red, green, blue; //if there's a valid hex color string from Cheerlights, update the color if(response.length()==7) { //convert the hex values from the response.body string into byte values red=hexToInt(response.charAt(1))*16+hexToInt(response.charAt(2)); green=hexToInt(response.charAt(3))*16+hexToInt(response.charAt(4)); blue=hexToInt(response.charAt(5))*16+hexToInt(response.charAt(6)); Serial.print(red); Serial.print(", "); Serial.print(green); Serial.print(", "); Serial.println(blue); //scale the values to max_brightness, so as not to draw too much current from the cube red=map(red,0,255,0,MAX_BRIGHTNESS); green=map(green,0,255,0,MAX_BRIGHTNESS); blue=map(blue,0,255,0,MAX_BRIGHTNESS); Color col=Color(red, green, blue); //actually update the color on the cube, with a cute animation fill(col); //cube.background is an immediate color change, where fill(col) does a little animation // cube.background(col); // cube.show(); } delay(POLLING_INTERVAL); } int hexToInt(char val) { int v = (val > '9')? (val &~ 0x20) - 'A' + 10: (val - '0'); return v; } //fills the LED cube with new new color col and a little animatiojn void fill(Color col) { for(int z=0;z<cube.size;z++) for(int y=0;y<cube.size;y++) for(int x=0;x<cube.size;x++) { int index=z*cube.size*cube.size+y*cube.size+x; cube.setVoxel(x,y,z,col); if((index%2==0)||(index==(cube.size*cube.size*cube.size)-1)) cube.show(); } } String getRequest(String hostname, String path) { TCPClient client; String response=""; Serial.println("connecting..."); if (client.connect(hostname, 80)) { Serial.println("connected"); client.print("GET "); client.print(path); client.println(" HTTP/1.0"); client.print("Host: "); client.println(hostname); client.println("Content-Length: 0"); client.println(); } else { client.stop(); Serial.println("connection failed"); } int timeout=500; int requestTime=millis(); while((client.available()==0)&&((millis()-requestTime)<timeout)) {} bool headers=true; char lastChar='\n'; while(client.available()>0) { char thisChar=client.read(); if(!headers) response.concat(String(thisChar)); else { if((thisChar=='\r')&&(lastChar=='\n')) { headers=false; client.read(); //kill that last \n } lastChar=thisChar; } } client.stop(); return response; }