Created
March 19, 2016 10:33
-
-
Save tomconte/47276b63b849fe70ad27 to your computer and use it in GitHub Desktop.
Control the colors of your Hue lights using the Adafruit TCS34725 RGB sensor. Requires a Web Service to convert RGB values to hue XY color encoding.
This file contains hidden or 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
| #include <Wire.h> | |
| #include <Console.h> | |
| #include "Adafruit_TCS34725.h" | |
| /* Example code for the Adafruit TCS34725 breakout library */ | |
| /* Connect SCL to analog 5 | |
| Connect SDA to analog 4 | |
| Connect VDD to 3.3V DC | |
| Connect GROUND to common ground */ | |
| /* Initialise with default values (int time = 2.4ms, gain = 1x) */ | |
| // Adafruit_TCS34725 tcs = Adafruit_TCS34725(); | |
| /* Initialise with specific int time and gain values */ | |
| Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_4X); | |
| // The IP address of your Hue bridge; find it at https://www.meethue.com/api/nupnp | |
| const char *hueIp = "192.168.0.133"; | |
| // The user for the Hue bridge; more info at http://www.developers.meethue.com/documentation/getting-started | |
| const char *hueUser = "3e395757167e1dbfb5235472713dcf"; | |
| // Watch out: global array | |
| char str_xy[48]; | |
| void setup(void) { | |
| Bridge.begin(); | |
| Console.begin(); | |
| /* | |
| while (!Console) { | |
| ; // wait for Console port to connect. | |
| } | |
| */ | |
| if (tcs.begin()) { | |
| Console.println("Found sensor"); | |
| } else { | |
| Console.println("No TCS34725 found ... check your connections"); | |
| while (1); | |
| } | |
| // Now we're ready to get readings! | |
| } | |
| void loop(void) { | |
| uint16_t red, green, blue, c, colorTemp, lux; | |
| tcs.getRawData(&red, &green, &blue, &c); | |
| //colorTemp = tcs.calculateColorTemperature(r, g, b); | |
| //lux = tcs.calculateLux(r, g, b); | |
| float r = red; r /= c; | |
| float g = green; g /= c; | |
| float b = blue; b /= c; | |
| r *= 256; g *= 256; b *= 256; | |
| convertRGB(r, g, b); | |
| sendHueCommand(4); | |
| delay(1000); | |
| } | |
| /* | |
| ** Send a "set state" command to the light | |
| */ | |
| void sendHueCommand(int light) | |
| { | |
| Process p; | |
| char cmd[128]; | |
| sprintf(cmd, "curl -XPUT http://%s/api/%s/lights/%d/state -d '%s'", hueIp, hueUser, light, str_xy); | |
| Console.println(cmd); | |
| p.runShellCommand(cmd); | |
| } | |
| /* | |
| ** Convert the RGB value to XY | |
| ** Use a Web Service cause I'm too damn lazy to convert the Java Hue SDK code to C! | |
| */ | |
| void convertRGB(int r, int g, int b) | |
| { | |
| Process p; | |
| char cmd[128]; | |
| char *s = str_xy; | |
| // Zero out the string | |
| memset(str_xy, 0, sizeof(str_xy)); | |
| // Assemble request | |
| sprintf(cmd, "curl \"http://huergb.azurewebsites.net/HueRGBConverter/convert?r=%d&g=%d&b=%d&modelid=LST001\"", r, g, b); | |
| Console.println(cmd); | |
| // Send request and read response | |
| p.runShellCommand(cmd); | |
| while (p.available() > 0) { | |
| char c = p.read(); | |
| *s++ = c; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment