/* Govee LED lights that were purcahsed from Amazon.com 100ft for 15USD. * 24Vdc 6 LED in series with current limiting resistor every 19.5". * * +----B----B----B----B----B----B----2K4-------------+ * | | * 24Vdc ----+----R----R----R----R----R----R----2K4-----------------+ * | | | * +----G----G----G----G----G----G----3K3---------------------+ * | | | | * | | | | * | | | | * +---------+----B----B----B----B----B----B----2K4---+ | | * | | | | * +----R----R----R----R----R----R----2K4-------+ | * | | | | * +----G----G----G----G----G----G----3K3-----------+ * | | | | * | control bus: blue red green * | | | | * ... ... ... ... * * We installed about 150ft so about 90 led circuits will need power. ESP8266 can * do 1KHz PWM so we can use 3 pins if we want. Another idea I had was to use lots * of pins to control 20mA LED driver ICs. With 15 pins one could make a circuit to * drive 32 levels of current to each color giving 32,768 colors. Each color would * have 5 npn controlling 1,2,4,8,16 LED drivers. One could then step through 0..620mA. * * Instead we will just use PWM to give us color control. We just need to calculate how * much current we want to send to our LED strip. Because each circuit runs in parallel * we need to determine how much current each branch will use. The current limiting * resistors means we can probably just use a fixed voltage supply. With a 48Vdc supply * at max brightness green will see 9mA, blue will see 12mA, and red will see 15mA. These * values are all pretty safe for the LED but the red would blow out a PN2222A with 1080mA. * * We should be careful about the inductance of the wire. Assuming 1/4mm diameter wire we * might have 47uH. We can use a diode on the collector of each color to send the spikes to * the +48Vdc rail. Each color will use an NPN-BJT, probably 3 parallel PN2222A with * ballast resitors, as buffers for the PWM signal from the esp8266. * * In the end I ended up using optoisolated pwm driving three power mosfets. This ended * up being a nuisance since the mosfet gates took forever to discharge resulting in * the LEDs staying lit when the esp8266 pulled the signal down. I did not really fix this * but I did add a few resistors on the gate to bleed off the gate charge. In effect the * max brightness is 0xfc instead of 0xff since the led does not turn off on 0xfd. But that * is not really a deal-breaker, just annoying. * * test using: * curl -X POST http://rain-gutter-rgb.lan.rome7.com/rgb -d "color=010101" */ #include #include #include #include #include ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti' uint8_t hexStr[7] = "010101"; /* 131 allows for frame count, 16 colors(3), 16 transistions(1), and a null terminator * the user provides the input as utf-8 encoded hexidecimal, we need twice * the storage space, but parsing is a little easier: 2 + (2 * (3 * 16 + 16)) + 1 = 131 * [?] there are 16 transistions because there is an extra transition when we * reach the end an loop to the begining * [?] frame count allows user to have an animation that is not the maximum length, * as of this note, the max frame count is 0x0f, '0f' when using a */ #define HOW_MANY_ANIMATION_FRAMES 16 #define MAX_ANIMATE_FRAME_COUNT_LEN 2 #define NULL_TERMINATOR_LEN 1 #define FRAME_LEN (3 + 1) #define ANIMATE_BUFFER_LEN (MAX_ANIMATE_FRAME_COUNT_LEN + (2 * HOW_MANY_ANIMATION_FRAMES * FRAME_LEN) + NULL_TERMINATOR_LEN) #define MAX_ANIMATE_FRAME_COUNT ((ANIMATE_BUFFER_LEN - 2 - 1) / 8) uint8_t animateStr[ANIMATE_BUFFER_LEN]; uint8_t animate = 0; uint8_t red = 1; uint8_t green = 1; uint8_t blue = 1; uint8_t brightness = 50; #define DISABLE_FASTLED_CORRECTION 0 #define ENABLE_FASTLED_CORRECTION 1 #define DISABLE_GAMMA_CORRECTION 0 #define USE_PHILIP_GAMMA_CORRECTION 1 #define USE_BG100_S_CURVE_GAMMA_CORRECTION 2 uint8_t gammaCorrectionType = DISABLE_GAMMA_CORRECTION; uint8_t useFastLedCorrection = DISABLE_FASTLED_CORRECTION; // quick fix for gamma correction from Adafruit's Phillip Burgess static uint8_t phillip_adafruitGamma8[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25, 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36, 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50, 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68, 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89, 90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114, 115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142, 144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175, 177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213, 215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 }; // another implementation of gamma correction produced by bg100: f(x) = 1/(1+EXP(((A2/21)-6)*-1))*255 static uint8_t bg100_sCurveGamma8[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x07, 0x07, 0x07, 0x08, 0x08, 0x08, 0x09, 0x09, 0x0A, 0x0A, 0x0B, 0x0B, 0x0C, 0x0C, 0x0D, 0x0D, 0x0E, 0x0F, 0x0F, 0x10, 0x11, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1F, 0x20, 0x21, 0x23, 0x24, 0x26, 0x27, 0x29, 0x2B, 0x2C, 0x2E, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3A, 0x3C, 0x3E, 0x40, 0x43, 0x45, 0x47, 0x4A, 0x4C, 0x4F, 0x51, 0x54, 0x57, 0x59, 0x5C, 0x5F, 0x62, 0x64, 0x67, 0x6A, 0x6D, 0x70, 0x73, 0x76, 0x79, 0x7C, 0x7F, 0x82, 0x85, 0x88, 0x8B, 0x8E, 0x91, 0x94, 0x97, 0x9A, 0x9C, 0x9F, 0xA2, 0xA5, 0xA7, 0xAA, 0xAD, 0xAF, 0xB2, 0xB4, 0xB7, 0xB9, 0xBB, 0xBE, 0xC0, 0xC2, 0xC4, 0xC6, 0xC8, 0xCA, 0xCC, 0xCE, 0xD0, 0xD2, 0xD3, 0xD5, 0xD7, 0xD8, 0xDA, 0xDB, 0xDD, 0xDE, 0xDF, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xED, 0xEE, 0xEF, 0xEF, 0xF0, 0xF1, 0xF1, 0xF2, 0xF2, 0xF3, 0xF3, 0xF4, 0xF4, 0xF5, 0xF5, 0xF6, 0xF6, 0xF6, 0xF7, 0xF7, 0xF7, 0xF8, 0xF8, 0xF8, 0xF9, 0xF9, 0xF9, 0xF9, 0xFA, 0xFA, 0xFA, 0xFA, 0xFA, 0xFB, 0xFB, 0xFB, 0xFB, 0xFB, 0xFB, 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF } /* Fast-Led typical smd5050 led RGB color correction (0xFFB0F0 red 255, green 176, blue 240) * measured brightness from typical LED vary by pn junction semiconductor material. * * [?] I am not too sure if this is measured by human eye or by device. Human eyes have thee * issues that need to be accounted for, one is the non-linear brightness that our nerves send * to our brains. Our eyes are more sensitive to green light. The last problem is not really * something we can handle in code as it in the realm of psychophysics, Weber–Fechner laws. The * Weber–Fechner has to do with the way our brains have a big impact on human perception. * * I believe that this color correction has to do with mechanical measurements as they are linear * and I know that the human eye sensitivities are anything but. Otherwise this is very crude. */ #define FASTLED_RED_CORRECTION = 0x00FF; #define FASTLED_GREEN_CORRECTION = 0x00B0; #define FASTLED_BLUE_CORRECTION = 0x00F0; uint8_t correctColor(uint8_t input, uint16_t factor) { uint16_t out; uint16_t in; in = input; out = in * factor; out /= 255; return ENABLE_FASTLED_CORRECTION == useFastLedCorrection ? gammaCorrection((uint8_t) out) : gammaCorrection(input); } uint8_t gammaCorrection(uint8_t input) { switch(gammaCorrectionType) { case USE_PHILIP_GAMMA_CORRECTION: return phillip_adafruitGamma8[input]; case USE_BG100_S_CURVE_GAMMA_CORRECTION: return bg100_sCurveGamma8[input]; case DISABLE_GAMMA_CORRECTION: default: } return input; } ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80 void handleRoot(); // function prototypes for HTTP handlers void handleLogin(); void handleNotFound(); void setup(void){ Serial.begin(115200); // Start the Serial communication to send messages to the computer delay(10); Serial.println('\n'); wifiMulti.addAP("n-phone-number-upstairs", "3103229909cedar"); // add Wi-Fi networks you want to connect to wifiMulti.addAP("n-phone-number", "3103229909cedar"); Serial.println("Connecting ..."); int i = 0; while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above delay(250); Serial.print('.'); } Serial.println('\n'); Serial.print("Connected to "); Serial.println(WiFi.SSID()); // Tell us what network we're connected to Serial.print("IP address:\t"); Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer /* if (MDNS.begin("rain-gutter-rgb")) { // Start the mDNS responder for esp8266.local Serial.println("mDNS responder started"); } else { Serial.println("Error setting up MDNS responder!"); } */ server.on("/", HTTP_GET, handleRoot); // Call the 'handleRoot' function when a client requests URI "/" //server.on("/login", HTTP_POST, handleLogin); // Call the 'handleLogin' function when a POST request is made to URI "/login" server.on("/rgb", HTTP_POST, handleRGB); // Call the 'handleRGB' function when a POST request is made to URI "/login" server.on("/dec", HTTP_GET, handleGetDec); server.on("/echo", HTTP_GET, handleEchoHex); server.on("/animate", HTTP_GET, handleAnimate); server.on("/cfg", HTTP_GET, handleGetCfg); server.on("/cfg", HTTP_POST, handleCfg); server.on("/brightness", HTTP_POST, handleGetBrightness); server.on("/brightness", HTTP_POST, handleBrightness server.onNotFound(handleNotFound); // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound" server.begin(); // Actually start the server Serial.println("HTTP server started"); /* esp8266 12-f breakout board has pins mapped a bit odd, and arduino esp8266 libs use GPIO pin number */ #define PIN_5 14 /* GPIO_14 */ #define PIN_6 12 /* GPIO_12 */ #define PIN_7 13 /* GPIO_13 */ /* slowing the PWM frequency decreases the error caused by the power mosfets * having a slow turn-off * this comes at a cost of flickering. One might think that LED PWM at * 1000Hz is not noticeable by the human eye, but one can see the flicker * appear as multiple spots when moving. It isn't really a flicker but one * can tell. */ //analogWriteFreq(1000); analogWriteFreq(250); // lower brightness to minimal value brightness = 0; updateBrightness (); // turn on the onboard led for testing brightness analogWrite(/* esp12f onboard led */ 2, 255); // setup pins with a test pwm analogWrite(PIN_5, red); analogWrite(PIN_6, green); analogWrite(PIN_7, blue); } void loop(void){ // keep track of where we are in the animation uint8_t frame = 0; uint8_t frameCount = 0; #define KEY_FRAMES 10 #define MS_IN_ANIMATION_DELAY 250 #define TEMPORAL_LENGTH (MS_IN_ANIMATION_DELAY / KEY_FRAMES) uint8_t redSubFrames[KEY_FRAMES]; uint8_t greenSubFrames[KEY_FRAMES]; uint8_t blueSubFrames[KEY_FRAMES]; // keep track of where we are in the subframe uint8_t subframe = KEY_FRAMES; /* because subframes occur every quarter second and animation delays can be long (up to 64 seconds) * we need to guess what the next color should be for the next second based on where we are now */ uint8_t animationDelayCounter = 0; uint16_t totalSubframes = 0; uint8_t frameAnimationDelay = 0; uint8_t frameStartRed = 0; uint8_t frameStartGreen = 0; uint8_t frameStartBlue = 0; uint8_t frameEndRed = 0; uint8_t frameEndGreen = 0; uint8_t frameEndBlue = 0; // tracking temporal length so that we do not run too fast or slow based on CPU frequency uint8_t delayCount = 0; while(true) { // listen for HTTP requests from clients server.handleClient(); // check if we are supposed to animate if(0 == animate) break; /* we will transition several times a second (technically serveral times per animation delay) * * [?] this delay is not really necesarry, we just need a way to not run too fast * * [!] this delay also reduces power consumption of the esp8266 significantly * not something we want necessarily, but worth mentioning in case one questions * what the heck is happening */ delay(1); if(delayCount++ < TEMPORAL_LENGTH) break; // do we have any subframes to animate? if(0 == subframe) { // reset the subframe counter subframe = KEY_FRAMES; // are we done with this frame? if(0 == animationDelayCounter) { // move to the next frame if(0 == frame) frame = (ANIMATE_BUFFER_LEN - 1) / 8; frame--; // get the next animation delay frameAnimationDelay = animationDelayCounter = // is this a stop frame if(0xff == frameAnimationDelay) { animate = 0; break; } // is this a loop frame if(0x00 == frameAnimationDelay) { frame = 0; continue; } // set our start and end frame colors frameStartRed = 0; frameStartGreen = 0; frameStartBlue = 0; frameEndRed = 0; frameEndGreen = 0; frameEndBlue = 0; } // using the start, stop, and animation delay counter, compute subframes // track progress through animation delay animationDelayCounter--; } // set color setColor(redSubFrames[subframe -1], greeenSubFrames[subframe -1], blueSubFrames[subframe -1]) // track progress through subframes subframe--; } } void handleRoot() { // When URI / is requested, send a web page with a button to toggle the LED //server.send(200, "text/html", "


Try 'John Doe' and 'password123' ...

"); server.send(200, "text/html", "rain-gutter-rgb http-post (hex values): /rgb color=\'RRGGBB\'"); } void handleEchoHex() { // make sure null terminated, then send as string hexStr[6] = '\0'; server.send(200, "text/html", (char*) hexStr); } void handleAnimate() { // turn on animate animate = 1; } void handleGetDec() { String hex = String(""); server.send(200, "text/html", hex + red + "," + green + "," + blue + '\n'); } void handleGetBrightness() { String info = ""; server.send(200, "text/html", info + brightness + '\n'); return; } /* we are going to cheat the brightness by adjusting pwm resolution * * in effect this will change the brightness and we will not need to perform any math * the way this works is that PWM resolution is 10-bit but when we set the duty-cycle * we only use values [0..255]. By increasing the resoltion past 255 we reduce the * brightness. Cool. * * [?] because our gate drive voltage source is a weak reverse biased zener * regulator and we just use resistors to turn off the power mosfets we * have a hard time with power losses turning on and off the mosfet. When * we drive all three mosfets on with 100% duty cycle the voltage source * drops well below the needed drive voltage and the mosfet Rds becomes * pretty terrible. It works but is really bad. If we run less than * 50% duty we should be okay. Using 1023 as the starting point means that * at 0 brightness mosfets are driven at 25% duty (255/1032). At 100 * brightness they are driven at 60% duty 255/432. */ void updateBrightness () { // default range is 0..255 analogWriteRange(1023); // brightness is inverted, technically it is darkness uint8_t brightnessValue = 6 * (100 - brightness); // reducing PWM range will increase brightness analogWriteRange(1023 - brightnessValue); } void handleBrightness() { char buffer[4]; if( ! server.hasArg("brightness")) { server.send(400, "text/plain", "400: Invalid Request"); // The request is invalid, so send HTTP status 400 return; } // else server.arg("brightness").getBytes(buffer, 3); brightness = (nibbler(buffer[0]) << 4) + nibbler(buffer[1]); // limit brightness to 100 brightness = 100 < brightness ? 100 : brightness; updateBrightness(); handleGetBrightness(); return; } void handleCfg() { uint8_t valid = 0; char buf[3]; if(server.hasArg("quickGamma")) { server.arg("quickGamma").getBytes(buf, 2); gammaCorrectionType = '1' == buf[0] ? USE_BG100_S_CURVE_GAMMA_CORRECTION : DISABLE_GAMMA_CORRECTION; valid += 1; } if(server.hasArg("sCurveGamma")) { server.arg("sCurveGamma").getBytes(buf, 2); gammaCorrectionType = '1' == buf[0] ? USE_PHILIP_GAMMA_CORRECTION : DISABLE_GAMMA_CORRECTION; valid += 1; } if(server.hasArg("colorCorrection")) { server.arg("colorCorrection").getBytes(buf, 2); gammaCorrectionType = '1' == buf[0] ? ENABLE_FASTLED_CORRECTION : DISABLE_FASTLED_CORRECTION; valid += 1; } if(valid) handleGetCfg(); else server.send(400, "text/plain", "400: Invalid Request"); return; } void handleGetCfg() { String info = "cfg [quickGamma:"; uint8_t fastled = ENABLE_FASTLED_CORRECTION == useFastLedCorrection; uint8_t sCurve = USE_PHILIP_GAMMA_CORRECTION == gammaCorrectionType; uint8_t quick = USE_BG100_S_CURVE_GAMMA_CORRECTION == gammaCorrectionType; if(valid) server.send(200, "text/html", info + quick + " sCurveGamma:" + sCurve + " colorCorrection:" + fastled + ']' + '\n'); return; } void handleRGB() { // If a POST request is made to URI /login if( ! server.hasArg("color")) { server.send(400, "text/plain", "400: Invalid Request"); // The request is invalid, so send HTTP status 400 return; } // turn of animate animate = 1; // kinda lame but parsing is not super easy here server.arg("color").getBytes(hexStr, 7); setColor( /* red */ (nibbler(hexStr[0]) << 4) + nibbler(hexStr[1]), /* green */ (nibbler(hexStr[2]) << 4) + nibbler(hexStr[3]), /* blue */ (nibbler(hexStr[4]) << 4) + nibbler(hexStr[5]) ) String info = "degub r:"; server.send(200, "text/html", info + red + " g:" + green + " b:" + blue + '\n'); } void setColor(uint8_t r, uint8_t g, uint8_t b) { // store colors for API red = r; green = g; blue = b; /* [!] it looks like we always correct color, this is not the case * the correctColor() routine, and the chained gammaCorrection() routine * check configuration settings before setting the LED color. * * the fast-led correction tones down green significantly and blue a bit. * Both linearly, I assume this is not to adjust for human eye sensitivity * but just for the junction efficiency */ analogWrite(PIN_5, correctColor(red, FASTLED_RED_CORRECTION)); analogWrite(PIN_6, correctColor(green, FASTLED_GREEN_CORRECTION)); analogWrite(PIN_7, correctColor(blue, FASTLED_BLUE_CORRECTION)); } // converts ASCII utf8 chars to integer values, [0..F], otherwise zero uint8_t nibbler(uint8_t v) { switch (v) { case 'f': case 'F': return 15; case 'e': case 'E': return 14; case 'd': case 'D': return 13; case 'c': case 'C': return 12; case 'b': case 'B': return 11; case 'a': case 'A': return 10; case '9': return 9; case '8': return 8; case '7': return 7; case '6': return 6; case '5': return 5; case '4': return 4; case '3': return 3; case '2': return 2; case '1': return 1; default: return 0; } } uint8_t hexStrToInt(char buf[]) { return (int) strtol(buf, 0, 16); } void handleLogin() { // If a POST request is made to URI /login if( ! server.hasArg("username") || ! server.hasArg("password") || server.arg("username") == NULL || server.arg("password") == NULL) { // If the POST request doesn't have username and password data server.send(400, "text/plain", "400: Invalid Request"); // The request is invalid, so send HTTP status 400 return; } if(server.arg("username") == "John Doe" && server.arg("password") == "password123") { // If both the username and the password are correct server.send(200, "text/html", "

Welcome, " + server.arg("username") + "!

Login successful

"); } else { // Username and password don't match server.send(401, "text/plain", "401: Unauthorized"); } } void handleNotFound(){ server.send(404, "text/plain", "404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request }