diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index 03f4ff6..e7af936 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -30,7 +30,7 @@ WebSocketsServer webSocket = WebSocketsServer(81); // *************************************************************************** #include #ifdef __AVR__ - #include +#include #endif // Parameter 1 = number of pixels in strip @@ -56,9 +56,9 @@ Ticker ticker; void tick() { - //toggle state - int state = digitalRead(BUILTIN_LED); // get the current state of GPIO1 pin - digitalWrite(BUILTIN_LED, !state); // set pin to the opposite state + //toggle state + int state = digitalRead(BUILTIN_LED); // get the current state of GPIO1 pin + digitalWrite(BUILTIN_LED, !state); // set pin to the opposite state } @@ -67,12 +67,12 @@ void tick() // *************************************************************************** //gets called when WiFiManager enters configuration mode void configModeCallback (WiFiManager *myWiFiManager) { - DBG_OUTPUT_PORT.println("Entered config mode"); - DBG_OUTPUT_PORT.println(WiFi.softAPIP()); - //if you used auto generated SSID, print it - DBG_OUTPUT_PORT.println(myWiFiManager->getConfigPortalSSID()); - //entered config mode, make led toggle faster - ticker.attach(0.2, tick); + DBG_OUTPUT_PORT.println("Entered config mode"); + DBG_OUTPUT_PORT.println(WiFi.softAPIP()); + //if you used auto generated SSID, print it + DBG_OUTPUT_PORT.println(myWiFiManager->getConfigPortalSSID()); + //entered config mode, make led toggle faster + ticker.attach(0.2, tick); } @@ -98,260 +98,275 @@ void configModeCallback (WiFiManager *myWiFiManager) { // MAIN // *************************************************************************** void setup() { - DBG_OUTPUT_PORT.begin(115200); - - // set builtin led pin as output - pinMode(BUILTIN_LED, OUTPUT); - // start ticker with 0.5 because we start in AP mode and try to connect - ticker.attach(0.6, tick); - - // *************************************************************************** - // Setup: WiFiManager - // *************************************************************************** - //Local intialization. Once its business is done, there is no need to keep it around - WiFiManager wifiManager; - //reset settings - for testing - //wifiManager.resetSettings(); - - //set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode - wifiManager.setAPCallback(configModeCallback); - - //fetches ssid and pass and tries to connect - //if it does not connect it starts an access point with the specified name - //here "AutoConnectAP" - //and goes into a blocking loop awaiting configuration - if (!wifiManager.autoConnect()) { - DBG_OUTPUT_PORT.println("failed to connect and hit timeout"); - //reset and try again, or maybe put it to deep sleep - ESP.reset(); - delay(1000); - } - - //if you get here you have connected to the WiFi - DBG_OUTPUT_PORT.println("connected...yeey :)"); - ticker.detach(); - //keep LED on - digitalWrite(BUILTIN_LED, LOW); - - - // *************************************************************************** - // Setup: Neopixel - // *************************************************************************** - strip.begin(); - strip.setBrightness(brightness); - strip.show(); // Initialize all pixels to 'off' - - - // *************************************************************************** - // Setup: MDNS responder - // *************************************************************************** - MDNS.begin(HOSTNAME); - DBG_OUTPUT_PORT.print("Open http://"); - DBG_OUTPUT_PORT.print(HOSTNAME); - DBG_OUTPUT_PORT.println(".local/edit to see the file browser"); - - - // *************************************************************************** - // Setup: WebSocket server - // *************************************************************************** - webSocket.begin(); - webSocket.onEvent(webSocketEvent); - - - // *************************************************************************** - // Setup: SPIFFS - // *************************************************************************** - SPIFFS.begin(); - { - Dir dir = SPIFFS.openDir("/"); - while (dir.next()) { - String fileName = dir.fileName(); - size_t fileSize = dir.fileSize(); - DBG_OUTPUT_PORT.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str()); - } - DBG_OUTPUT_PORT.printf("\n"); - } - - // *************************************************************************** - // Setup: SPIFFS Webserver handler - // *************************************************************************** - //list directory - server.on("/list", HTTP_GET, handleFileList); - //load editor - server.on("/edit", HTTP_GET, [](){ - if(!handleFileRead("/edit.htm")) server.send(404, "text/plain", "FileNotFound"); - }); - //create file - server.on("/edit", HTTP_PUT, handleFileCreate); - //delete file - server.on("/edit", HTTP_DELETE, handleFileDelete); - //first callback is called after the request has ended with all parsed arguments - //second callback handles file uploads at that location - server.on("/edit", HTTP_POST, [](){ server.send(200, "text/plain", ""); }, handleFileUpload); - //get heap status, analog input value and all GPIO statuses in one json call - server.on("/status", HTTP_GET, [](){ - String json = "{"; - json += "\"heap\":"+String(ESP.getFreeHeap()); - json += ", \"analog\":"+String(analogRead(A0)); - json += ", \"gpio\":"+String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16))); - json += "}"; - server.send(200, "text/json", json); - json = String(); - }); - - - //called when the url is not defined here - //use it to load content from SPIFFS - server.onNotFound([](){ - if(!handleFileRead(server.uri())) - handleNotFound(); - }); - - - server.on("/upload", handleMinimalUpload); - - // *************************************************************************** - // Setup: SPIFFS Webserver handler - // *************************************************************************** - server.on("/brightness", []() { + DBG_OUTPUT_PORT.begin(115200); + + // set builtin led pin as output + pinMode(BUILTIN_LED, OUTPUT); + // start ticker with 0.5 because we start in AP mode and try to connect + ticker.attach(0.6, tick); + + // *************************************************************************** + // Setup: WiFiManager + // *************************************************************************** + //Local intialization. Once its business is done, there is no need to keep it around + WiFiManager wifiManager; + //reset settings - for testing + //wifiManager.resetSettings(); + + //set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode + wifiManager.setAPCallback(configModeCallback); + + //fetches ssid and pass and tries to connect + //if it does not connect it starts an access point with the specified name + //here "AutoConnectAP" + //and goes into a blocking loop awaiting configuration + if (!wifiManager.autoConnect()) { + DBG_OUTPUT_PORT.println("failed to connect and hit timeout"); + //reset and try again, or maybe put it to deep sleep + ESP.reset(); + delay(1000); + } + + //if you get here you have connected to the WiFi + DBG_OUTPUT_PORT.println("connected...yeey :)"); + ticker.detach(); + //keep LED on + digitalWrite(BUILTIN_LED, LOW); + + + // *************************************************************************** + // Setup: Neopixel + // *************************************************************************** + strip.begin(); + strip.setBrightness(brightness); + strip.show(); // Initialize all pixels to 'off' + + + // *************************************************************************** + // Setup: MDNS responder + // *************************************************************************** + MDNS.begin(HOSTNAME); + DBG_OUTPUT_PORT.print("Open http://"); + DBG_OUTPUT_PORT.print(HOSTNAME); + DBG_OUTPUT_PORT.println(".local/edit to see the file browser"); + + + // *************************************************************************** + // Setup: WebSocket server + // *************************************************************************** + webSocket.begin(); + webSocket.onEvent(webSocketEvent); + + + // *************************************************************************** + // Setup: SPIFFS + // *************************************************************************** + SPIFFS.begin(); + { + Dir dir = SPIFFS.openDir("/"); + while (dir.next()) { + String fileName = dir.fileName(); + size_t fileSize = dir.fileSize(); + DBG_OUTPUT_PORT.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str()); + } + DBG_OUTPUT_PORT.printf("\n"); + } + + // *************************************************************************** + // Setup: SPIFFS Webserver handler + // *************************************************************************** + //list directory + server.on("/list", HTTP_GET, handleFileList); + //load editor + server.on("/edit", HTTP_GET, []() { + if (!handleFileRead("/edit.htm")) server.send(404, "text/plain", "FileNotFound"); + }); + //create file + server.on("/edit", HTTP_PUT, handleFileCreate); + //delete file + server.on("/edit", HTTP_DELETE, handleFileDelete); + //first callback is called after the request has ended with all parsed arguments + //second callback handles file uploads at that location + server.on("/edit", HTTP_POST, []() { + server.send(200, "text/plain", ""); + }, handleFileUpload); + //get heap status, analog input value and all GPIO statuses in one json call + server.on("/esp_status", HTTP_GET, []() { + String json = "{"; + json += "\"heap\":" + String(ESP.getFreeHeap()); + json += ", \"analog\":" + String(analogRead(A0)); + json += ", \"gpio\":" + String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16))); + json += "}"; + server.send(200, "text/json", json); + json = String(); + }); + + + //called when the url is not defined here + //use it to load content from SPIFFS + server.onNotFound([]() { + if (!handleFileRead(server.uri())) + handleNotFound(); + }); + + server.on("/upload", handleMinimalUpload); + + server.on("/restart", []() { + DBG_OUTPUT_PORT.printf("/restart:\n"); + server.send(200, "text/plain", "restarting..." ); + ESP.restart(); + }); + + + // *************************************************************************** + // Setup: SPIFFS Webserver handler + // *************************************************************************** + server.on("/set_brightness", []() { if (server.arg("c").toInt() > 0) { brightness = (int) server.arg("c").toInt() * 2.55; } else { brightness = server.arg("p").toInt(); } - if (brightness > 255) { - brightness = 255; - } - if (brightness < 0) { - brightness = 0; - } - strip.setBrightness(brightness); - - if (mode == HOLD) { - mode = ALL; - } - - getStatusJSON(); - }); + if (brightness > 255) { + brightness = 255; + } + if (brightness < 0) { + brightness = 0; + } + strip.setBrightness(brightness); + + if (mode == HOLD) { + mode = ALL; + } + + getStatusJSON(); + }); server.on("/get_brightness", []() { - server.send(200, "text/plain", String((int) (brightness / 2.55)) ); + String str_brightness = String((int) (brightness / 2.55)); + server.send(200, "text/plain", str_brightness ); + DBG_OUTPUT_PORT.print("/get_brightness: "); + DBG_OUTPUT_PORT.println(str_brightness); }); server.on("/get_switch", []() { server.send(200, "text/plain", (mode == OFF) ? "0" : "1" ); + DBG_OUTPUT_PORT.printf("/get_switch: %s\n", (mode == OFF) ? "0" : "1"); }); server.on("/get_color", []() { - server.send(200, "text/plain", String(main_color.red, HEX) + String(main_color.green, HEX) + String(main_color.blue, HEX) ); + String rgbcolor = String(main_color.red, HEX) + String(main_color.green, HEX) + String(main_color.blue, HEX); + server.send(200, "text/plain", rgbcolor ); + DBG_OUTPUT_PORT.print("/get_color: "); + DBG_OUTPUT_PORT.println(rgbcolor); }); - - server.on("/status", []() { - getStatusJSON(); - }); - - server.on("/off", []() { - exit_func = true; - mode = OFF; - getArgs(); - getStatusJSON(); - }); - - server.on("/all", []() { - exit_func = true; - mode = ALL; - getArgs(); - getStatusJSON(); - }); - - server.on("/wipe", []() { - exit_func = true; - mode = WIPE; - getArgs(); - getStatusJSON(); - }); - - server.on("/rainbow", []() { - exit_func = true; - mode = RAINBOW; - getArgs(); - getStatusJSON(); - }); - - server.on("/rainbowCycle", []() { - exit_func = true; - mode = RAINBOWCYCLE; - getArgs(); - getStatusJSON(); - }); - - server.on("/theaterchase", []() { - exit_func = true; - mode = THEATERCHASE; - getArgs(); - getStatusJSON(); - }); - - server.on("/theaterchaseRainbow", []() { - exit_func = true; - mode = THEATERCHASERAINBOW; - getArgs(); - getStatusJSON(); - }); - - server.on("/tv", []() { - exit_func = true; - mode = TV; - getArgs(); - getStatusJSON(); - }); - - server.begin(); + + server.on("/status", []() { + getStatusJSON(); + }); + + server.on("/off", []() { + exit_func = true; + mode = OFF; + getArgs(); + getStatusJSON(); + }); + + server.on("/all", []() { + exit_func = true; + mode = ALL; + getArgs(); + getStatusJSON(); + }); + + server.on("/wipe", []() { + exit_func = true; + mode = WIPE; + getArgs(); + getStatusJSON(); + }); + + server.on("/rainbow", []() { + exit_func = true; + mode = RAINBOW; + getArgs(); + getStatusJSON(); + }); + + server.on("/rainbowCycle", []() { + exit_func = true; + mode = RAINBOWCYCLE; + getArgs(); + getStatusJSON(); + }); + + server.on("/theaterchase", []() { + exit_func = true; + mode = THEATERCHASE; + getArgs(); + getStatusJSON(); + }); + + server.on("/theaterchaseRainbow", []() { + exit_func = true; + mode = THEATERCHASERAINBOW; + getArgs(); + getStatusJSON(); + }); + + server.on("/tv", []() { + exit_func = true; + mode = TV; + getArgs(); + getStatusJSON(); + }); + + server.begin(); } void loop() { - server.handleClient(); - webSocket.loop(); - - // Simple statemachine that handles the different modes - if (mode == OFF) { - //colorWipe(strip.Color(0, 0, 0), 50); + server.handleClient(); + webSocket.loop(); + + // Simple statemachine that handles the different modes + if (mode == OFF) { + //colorWipe(strip.Color(0, 0, 0), 50); uint16_t i; for (i = 0; i < strip.numPixels(); i++) { strip.setPixelColor(i, 0, 0, 0); } strip.show(); - mode = HOLD; - } - if (mode == ALL) { - uint16_t i; - for (i = 0; i < strip.numPixels(); i++) { - strip.setPixelColor(i, main_color.red, main_color.green, main_color.blue); - } - strip.show(); - //mode = HOLD; - } - if (mode == WIPE) { - colorWipe(strip.Color(main_color.red, main_color.green, main_color.blue), delay_ms); - } - if (mode == RAINBOW) { - rainbow(delay_ms); - } - if (mode == RAINBOWCYCLE) { - rainbowCycle(delay_ms); - } - if (mode == THEATERCHASE) { - theaterChase(strip.Color(main_color.red, main_color.green, main_color.blue), delay_ms); - } - if (mode == THEATERCHASERAINBOW) { - theaterChaseRainbow(delay_ms); - } - if (mode == HOLD) { - if (exit_func) { - exit_func = false; - } - } - if (mode == TV) { - tv(); - } + //mode = HOLD; + } + if (mode == ALL) { + uint16_t i; + for (i = 0; i < strip.numPixels(); i++) { + strip.setPixelColor(i, main_color.red, main_color.green, main_color.blue); + } + strip.show(); + //mode = HOLD; + } + if (mode == WIPE) { + colorWipe(strip.Color(main_color.red, main_color.green, main_color.blue), delay_ms); + } + if (mode == RAINBOW) { + rainbow(delay_ms); + } + if (mode == RAINBOWCYCLE) { + rainbowCycle(delay_ms); + } + if (mode == THEATERCHASE) { + theaterChase(strip.Color(main_color.red, main_color.green, main_color.blue), delay_ms); + } + if (mode == THEATERCHASERAINBOW) { + theaterChaseRainbow(delay_ms); + } + if (mode == HOLD) { + if (exit_func) { + exit_func = false; + } + } + if (mode == TV) { + tv(); + } } diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index e3bc731..70a2f4c 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -3,53 +3,53 @@ // *************************************************************************** void getArgs() { if (server.arg("rgb") != "") { - uint32_t rgb = (uint32_t) strtol(server.arg("rgb").c_str(), NULL, 16); - main_color.red = ((rgb >> 16) & 0xFF); - main_color.green = ((rgb >> 8) & 0xFF); - main_color.blue = ((rgb >> 0) & 0xFF); + uint32_t rgb = (uint32_t) strtol(server.arg("rgb").c_str(), NULL, 16); + main_color.red = ((rgb >> 16) & 0xFF); + main_color.green = ((rgb >> 8) & 0xFF); + main_color.blue = ((rgb >> 0) & 0xFF); } else { main_color.red = server.arg("r").toInt(); main_color.green = server.arg("g").toInt(); main_color.blue = server.arg("b").toInt(); } - delay_ms = server.arg("d").toInt(); - - if (main_color.red > 255) { - main_color.red = 255; - } - if (main_color.green > 255) { - main_color.green = 255; - } - if (main_color.blue > 255) { - main_color.blue = 255; - } - - if (main_color.red < 0) { - main_color.red = 0; - } - if (main_color.green < 0) { - main_color.green = 0; - } - if (main_color.blue < 0) { - main_color.blue = 0; - } - - if (server.arg("d") == "") { - delay_ms = 20; - } - - DBG_OUTPUT_PORT.print("Mode: "); - DBG_OUTPUT_PORT.print(mode); - DBG_OUTPUT_PORT.print(", Color: "); - DBG_OUTPUT_PORT.print(main_color.red); - DBG_OUTPUT_PORT.print(", "); - DBG_OUTPUT_PORT.print(main_color.green); - DBG_OUTPUT_PORT.print(", "); - DBG_OUTPUT_PORT.print(main_color.blue); - DBG_OUTPUT_PORT.print(", Delay:"); - DBG_OUTPUT_PORT.print(delay_ms); - DBG_OUTPUT_PORT.print(", Brightness:"); - DBG_OUTPUT_PORT.println(brightness); + delay_ms = server.arg("d").toInt(); + + if (main_color.red > 255) { + main_color.red = 255; + } + if (main_color.green > 255) { + main_color.green = 255; + } + if (main_color.blue > 255) { + main_color.blue = 255; + } + + if (main_color.red < 0) { + main_color.red = 0; + } + if (main_color.green < 0) { + main_color.green = 0; + } + if (main_color.blue < 0) { + main_color.blue = 0; + } + + if (server.arg("d") == "") { + delay_ms = 20; + } + + DBG_OUTPUT_PORT.print("Mode: "); + DBG_OUTPUT_PORT.print(mode); + DBG_OUTPUT_PORT.print(", Color: "); + DBG_OUTPUT_PORT.print(main_color.red); + DBG_OUTPUT_PORT.print(", "); + DBG_OUTPUT_PORT.print(main_color.green); + DBG_OUTPUT_PORT.print(", "); + DBG_OUTPUT_PORT.print(main_color.blue); + DBG_OUTPUT_PORT.print(", Delay:"); + DBG_OUTPUT_PORT.print(delay_ms); + DBG_OUTPUT_PORT.print(", Brightness:"); + DBG_OUTPUT_PORT.println(brightness); } void handleMinimalUpload() { @@ -59,7 +59,7 @@ void handleMinimalUpload() { int hr = min / 60; snprintf ( temp, 1500, - "\ + "\ \ \ ESP8266 Upload\ @@ -75,159 +75,159 @@ void handleMinimalUpload() { \ \ ", - hr, min % 60, sec % 60 + hr, min % 60, sec % 60 ); - server.send ( 200, "text/html", temp ); + server.send ( 200, "text/html", temp ); } void handleNotFound() { - String message = "File Not Found\n\n"; - message += "URI: "; - message += server.uri(); - message += "\nMethod: "; - message += ( server.method() == HTTP_GET ) ? "GET" : "POST"; - message += "\nArguments: "; - message += server.args(); - message += "\n"; - for ( uint8_t i = 0; i < server.args(); i++ ) { - message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n"; - } - server.send ( 404, "text/plain", message ); + String message = "File Not Found\n\n"; + message += "URI: "; + message += server.uri(); + message += "\nMethod: "; + message += ( server.method() == HTTP_GET ) ? "GET" : "POST"; + message += "\nArguments: "; + message += server.args(); + message += "\n"; + for ( uint8_t i = 0; i < server.args(); i++ ) { + message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n"; + } + server.send ( 404, "text/plain", message ); } void getStatusJSON() { - char json[255]; - snprintf(json, sizeof(json), "{\"mode\":%d, \"delay_ms\":%d, \"brightness\":%d, \"color\":[%d, %d, %d]}", mode, delay_ms, brightness, main_color.red, main_color.green, main_color.blue); - server.send ( 200, "application/json", json ); + char json[255]; + snprintf(json, sizeof(json), "{\"mode\":%d, \"delay_ms\":%d, \"brightness\":%d, \"color\":[%d, %d, %d]}", mode, delay_ms, brightness, main_color.red, main_color.green, main_color.blue); + server.send ( 200, "application/json", json ); } void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) { - switch(type) { - case WStype_DISCONNECTED: - DBG_OUTPUT_PORT.printf("WS: [%u] Disconnected!\n", num); - break; - - case WStype_CONNECTED: { - IPAddress ip = webSocket.remoteIP(num); - DBG_OUTPUT_PORT.printf("WS: [%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload); + switch (type) { + case WStype_DISCONNECTED: + DBG_OUTPUT_PORT.printf("WS: [%u] Disconnected!\n", num); + break; - // send message to client - webSocket.sendTXT(num, "Connected"); + case WStype_CONNECTED: { + IPAddress ip = webSocket.remoteIP(num); + DBG_OUTPUT_PORT.printf("WS: [%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload); + + // send message to client + webSocket.sendTXT(num, "Connected"); + } + break; + + case WStype_TEXT: + DBG_OUTPUT_PORT.printf("WS: [%u] get Text: %s\n", num, payload); + + // # ==> Set main color + if (payload[0] == '#') { + // decode rgb data + uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16); + main_color.red = ((rgb >> 16) & 0xFF); + main_color.green = ((rgb >> 8) & 0xFF); + main_color.blue = ((rgb >> 0) & 0xFF); + DBG_OUTPUT_PORT.printf("Set main color to: [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue); + webSocket.sendTXT(num, "OK"); + } + + // # ==> Set delay + if (payload[0] == '?') { + // decode delay data + uint8_t d = (uint8_t) strtol((const char *) &payload[1], NULL, 10); + delay_ms = ((d >> 0) & 0xFF); + DBG_OUTPUT_PORT.printf("WS: Set delay to: [%u]\n", delay_ms); + webSocket.sendTXT(num, "OK"); + } + + // # ==> Set brightness + if (payload[0] == '%') { + uint8_t b = (uint8_t) strtol((const char *) &payload[1], NULL, 10); + brightness = ((b >> 0) & 0xFF); + DBG_OUTPUT_PORT.printf("WS: Set brightness to: [%u]\n", brightness); + strip.setBrightness(brightness); + webSocket.sendTXT(num, "OK"); + } + + + // * ==> Set main color and light all LEDs (Shortcut) + if (payload[0] == '*') { + // decode rgb data + uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16); + + main_color.red = ((rgb >> 16) & 0xFF); + main_color.green = ((rgb >> 8) & 0xFF); + main_color.blue = ((rgb >> 0) & 0xFF); + + for (int i = 0; i < strip.numPixels(); i++) { + strip.setPixelColor(i, main_color.red, main_color.green, main_color.blue); } - break; - - case WStype_TEXT: - DBG_OUTPUT_PORT.printf("WS: [%u] get Text: %s\n", num, payload); + strip.show(); + DBG_OUTPUT_PORT.printf("WS: Set all leds to main color: [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue); + mode = HOLD; + webSocket.sendTXT(num, "OK"); + } - // # ==> Set main color - if(payload[0] == '#') { - // decode rgb data - uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16); - main_color.red = ((rgb >> 16) & 0xFF); - main_color.green = ((rgb >> 8) & 0xFF); - main_color.blue = ((rgb >> 0) & 0xFF); - DBG_OUTPUT_PORT.printf("Set main color to: [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue); - webSocket.sendTXT(num, "OK"); - } + // ! ==> Set single LED in given color + if (payload[0] == '!') { + // decode led index + uint64_t rgb = (uint64_t) strtol((const char *) &payload[1], NULL, 16); - // # ==> Set delay - if(payload[0] == '?') { - // decode delay data - uint8_t d = (uint8_t) strtol((const char *) &payload[1], NULL, 10); - delay_ms = ((d >> 0) & 0xFF); - DBG_OUTPUT_PORT.printf("WS: Set delay to: [%u]\n", delay_ms); - webSocket.sendTXT(num, "OK"); - } + uint8_t led = ((rgb >> 24) & 0xFF); + if (led < strip.numPixels()) { + ledstates[led].red = ((rgb >> 16) & 0xFF); + ledstates[led].green = ((rgb >> 8) & 0xFF); + ledstates[led].blue = ((rgb >> 0) & 0xFF); + DBG_OUTPUT_PORT.printf("WS: Set single led [%u] to [%u] [%u] [%u]!\n", led, ledstates[led].red, ledstates[led].green, ledstates[led].blue); - // # ==> Set brightness - if(payload[0] == '%') { - uint8_t b = (uint8_t) strtol((const char *) &payload[1], NULL, 10); - brightness = ((b >> 0) & 0xFF); - DBG_OUTPUT_PORT.printf("WS: Set brightness to: [%u]\n", brightness); - strip.setBrightness(brightness); - webSocket.sendTXT(num, "OK"); - } + for (uint8_t i = 0; i < strip.numPixels(); i++) { + strip.setPixelColor(i, ledstates[i].red, ledstates[i].green, ledstates[i].blue); + //DBG_OUTPUT_PORT.printf("[%u]--[%u] [%u] [%u] [%u] LED index!\n", rgb, i, ledstates[i].red, ledstates[i].green, ledstates[i].blue); + } + strip.show(); + } + mode = HOLD; + webSocket.sendTXT(num, "OK"); + } + // ! ==> Activate mode + if (payload[0] == '=') { + // we get mode data + String str_mode = String((char *) &payload[0]); - // * ==> Set main color and light all LEDs (Shortcut) - if(payload[0] == '*') { - // decode rgb data - uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16); + exit_func = true; + if (str_mode.startsWith("=off")) { + mode = OFF; + } + if (str_mode.startsWith("=all")) { + mode = ALL; + } + if (str_mode.startsWith("=wipe")) { + mode = WIPE; + } + if (str_mode.startsWith("=rainbow")) { + mode = RAINBOW; + } + if (str_mode.startsWith("=rainbowCycle")) { + mode = RAINBOWCYCLE; + } + if (str_mode.startsWith("=theaterchase")) { + mode = THEATERCHASE; + } + if (str_mode.startsWith("=theaterchaseRainbow")) { + mode = THEATERCHASERAINBOW; + } + if (str_mode.startsWith("=tv")) { + mode = TV; + } - main_color.red = ((rgb >> 16) & 0xFF); - main_color.green = ((rgb >> 8) & 0xFF); - main_color.blue = ((rgb >> 0) & 0xFF); - - for (int i = 0; i < strip.numPixels(); i++) { - strip.setPixelColor(i, main_color.red, main_color.green, main_color.blue); - } - strip.show(); - DBG_OUTPUT_PORT.printf("WS: Set all leds to main color: [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue); - mode = HOLD; - webSocket.sendTXT(num, "OK"); - } - - // ! ==> Set single LED in given color - if(payload[0] == '!') { - // decode led index - uint64_t rgb = (uint64_t) strtol((const char *) &payload[1], NULL, 16); - - uint8_t led = ((rgb >> 24) & 0xFF); - if (led < strip.numPixels()) { - ledstates[led].red = ((rgb >> 16) & 0xFF); - ledstates[led].green = ((rgb >> 8) & 0xFF); - ledstates[led].blue = ((rgb >> 0) & 0xFF); - DBG_OUTPUT_PORT.printf("WS: Set single led [%u] to [%u] [%u] [%u]!\n", led, ledstates[led].red, ledstates[led].green, ledstates[led].blue); - - for (uint8_t i = 0; i < strip.numPixels(); i++) { - strip.setPixelColor(i, ledstates[i].red, ledstates[i].green, ledstates[i].blue); - //DBG_OUTPUT_PORT.printf("[%u]--[%u] [%u] [%u] [%u] LED index!\n", rgb, i, ledstates[i].red, ledstates[i].green, ledstates[i].blue); - } - strip.show(); - } - mode = HOLD; - webSocket.sendTXT(num, "OK"); - } - - // ! ==> Activate mode - if(payload[0] == '=') { - // we get mode data - String str_mode = String((char *) &payload[0]); - - exit_func = true; - if (str_mode.startsWith("=off")) { - mode = OFF; - } - if (str_mode.startsWith("=all")) { - mode = ALL; - } - if (str_mode.startsWith("=wipe")) { - mode = WIPE; - } - if (str_mode.startsWith("=rainbow")) { - mode = RAINBOW; - } - if (str_mode.startsWith("=rainbowCycle")) { - mode = RAINBOWCYCLE; - } - if (str_mode.startsWith("=theaterchase")) { - mode = THEATERCHASE; - } - if (str_mode.startsWith("=theaterchaseRainbow")) { - mode = THEATERCHASERAINBOW; - } - if (str_mode.startsWith("=tv")) { - mode = TV; - } - - DBG_OUTPUT_PORT.printf("Activated mode [%u]!\n", mode); - webSocket.sendTXT(num, "OK"); - } - break; - } + DBG_OUTPUT_PORT.printf("Activated mode [%u]!\n", mode); + webSocket.sendTXT(num, "OK"); + } + break; + } } void checkForRequests() { - webSocket.loop(); - server.handleClient(); + webSocket.loop(); + server.handleClient(); }