Little Bugfixes added REST-API endpoints.

This commit is contained in:
Tobias Blum 2016-05-22 00:28:30 +02:00
parent 0364a5316f
commit fe6f191deb
2 changed files with 435 additions and 420 deletions

View file

@ -30,7 +30,7 @@ WebSocketsServer webSocket = WebSocketsServer(81);
// *************************************************************************** // ***************************************************************************
#include <Adafruit_NeoPixel.h> #include <Adafruit_NeoPixel.h>
#ifdef __AVR__ #ifdef __AVR__
#include <avr/power.h> #include <avr/power.h>
#endif #endif
// Parameter 1 = number of pixels in strip // Parameter 1 = number of pixels in strip
@ -56,9 +56,9 @@ Ticker ticker;
void tick() void tick()
{ {
//toggle state //toggle state
int state = digitalRead(BUILTIN_LED); // get the current state of GPIO1 pin int state = digitalRead(BUILTIN_LED); // get the current state of GPIO1 pin
digitalWrite(BUILTIN_LED, !state); // set pin to the opposite state digitalWrite(BUILTIN_LED, !state); // set pin to the opposite state
} }
@ -67,12 +67,12 @@ void tick()
// *************************************************************************** // ***************************************************************************
//gets called when WiFiManager enters configuration mode //gets called when WiFiManager enters configuration mode
void configModeCallback (WiFiManager *myWiFiManager) { void configModeCallback (WiFiManager *myWiFiManager) {
DBG_OUTPUT_PORT.println("Entered config mode"); DBG_OUTPUT_PORT.println("Entered config mode");
DBG_OUTPUT_PORT.println(WiFi.softAPIP()); DBG_OUTPUT_PORT.println(WiFi.softAPIP());
//if you used auto generated SSID, print it //if you used auto generated SSID, print it
DBG_OUTPUT_PORT.println(myWiFiManager->getConfigPortalSSID()); DBG_OUTPUT_PORT.println(myWiFiManager->getConfigPortalSSID());
//entered config mode, make led toggle faster //entered config mode, make led toggle faster
ticker.attach(0.2, tick); ticker.attach(0.2, tick);
} }
@ -98,260 +98,275 @@ void configModeCallback (WiFiManager *myWiFiManager) {
// MAIN // MAIN
// *************************************************************************** // ***************************************************************************
void setup() { void setup() {
DBG_OUTPUT_PORT.begin(115200); DBG_OUTPUT_PORT.begin(115200);
// set builtin led pin as output // set builtin led pin as output
pinMode(BUILTIN_LED, OUTPUT); pinMode(BUILTIN_LED, OUTPUT);
// start ticker with 0.5 because we start in AP mode and try to connect // start ticker with 0.5 because we start in AP mode and try to connect
ticker.attach(0.6, tick); ticker.attach(0.6, tick);
// *************************************************************************** // ***************************************************************************
// Setup: WiFiManager // Setup: WiFiManager
// *************************************************************************** // ***************************************************************************
//Local intialization. Once its business is done, there is no need to keep it around //Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager; WiFiManager wifiManager;
//reset settings - for testing //reset settings - for testing
//wifiManager.resetSettings(); //wifiManager.resetSettings();
//set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode //set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
wifiManager.setAPCallback(configModeCallback); wifiManager.setAPCallback(configModeCallback);
//fetches ssid and pass and tries to connect //fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name //if it does not connect it starts an access point with the specified name
//here "AutoConnectAP" //here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration //and goes into a blocking loop awaiting configuration
if (!wifiManager.autoConnect()) { if (!wifiManager.autoConnect()) {
DBG_OUTPUT_PORT.println("failed to connect and hit timeout"); DBG_OUTPUT_PORT.println("failed to connect and hit timeout");
//reset and try again, or maybe put it to deep sleep //reset and try again, or maybe put it to deep sleep
ESP.reset(); ESP.reset();
delay(1000); delay(1000);
} }
//if you get here you have connected to the WiFi //if you get here you have connected to the WiFi
DBG_OUTPUT_PORT.println("connected...yeey :)"); DBG_OUTPUT_PORT.println("connected...yeey :)");
ticker.detach(); ticker.detach();
//keep LED on //keep LED on
digitalWrite(BUILTIN_LED, LOW); digitalWrite(BUILTIN_LED, LOW);
// *************************************************************************** // ***************************************************************************
// Setup: Neopixel // Setup: Neopixel
// *************************************************************************** // ***************************************************************************
strip.begin(); strip.begin();
strip.setBrightness(brightness); strip.setBrightness(brightness);
strip.show(); // Initialize all pixels to 'off' strip.show(); // Initialize all pixels to 'off'
// *************************************************************************** // ***************************************************************************
// Setup: MDNS responder // Setup: MDNS responder
// *************************************************************************** // ***************************************************************************
MDNS.begin(HOSTNAME); MDNS.begin(HOSTNAME);
DBG_OUTPUT_PORT.print("Open http://"); DBG_OUTPUT_PORT.print("Open http://");
DBG_OUTPUT_PORT.print(HOSTNAME); DBG_OUTPUT_PORT.print(HOSTNAME);
DBG_OUTPUT_PORT.println(".local/edit to see the file browser"); DBG_OUTPUT_PORT.println(".local/edit to see the file browser");
// *************************************************************************** // ***************************************************************************
// Setup: WebSocket server // Setup: WebSocket server
// *************************************************************************** // ***************************************************************************
webSocket.begin(); webSocket.begin();
webSocket.onEvent(webSocketEvent); webSocket.onEvent(webSocketEvent);
// *************************************************************************** // ***************************************************************************
// Setup: SPIFFS // Setup: SPIFFS
// *************************************************************************** // ***************************************************************************
SPIFFS.begin(); SPIFFS.begin();
{ {
Dir dir = SPIFFS.openDir("/"); Dir dir = SPIFFS.openDir("/");
while (dir.next()) { while (dir.next()) {
String fileName = dir.fileName(); String fileName = dir.fileName();
size_t fileSize = dir.fileSize(); 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("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str());
} }
DBG_OUTPUT_PORT.printf("\n"); DBG_OUTPUT_PORT.printf("\n");
} }
// *************************************************************************** // ***************************************************************************
// Setup: SPIFFS Webserver handler // Setup: SPIFFS Webserver handler
// *************************************************************************** // ***************************************************************************
//list directory //list directory
server.on("/list", HTTP_GET, handleFileList); server.on("/list", HTTP_GET, handleFileList);
//load editor //load editor
server.on("/edit", HTTP_GET, [](){ server.on("/edit", HTTP_GET, []() {
if(!handleFileRead("/edit.htm")) server.send(404, "text/plain", "FileNotFound"); if (!handleFileRead("/edit.htm")) server.send(404, "text/plain", "FileNotFound");
}); });
//create file //create file
server.on("/edit", HTTP_PUT, handleFileCreate); server.on("/edit", HTTP_PUT, handleFileCreate);
//delete file //delete file
server.on("/edit", HTTP_DELETE, handleFileDelete); server.on("/edit", HTTP_DELETE, handleFileDelete);
//first callback is called after the request has ended with all parsed arguments //first callback is called after the request has ended with all parsed arguments
//second callback handles file uploads at that location //second callback handles file uploads at that location
server.on("/edit", HTTP_POST, [](){ server.send(200, "text/plain", ""); }, handleFileUpload); server.on("/edit", HTTP_POST, []() {
//get heap status, analog input value and all GPIO statuses in one json call server.send(200, "text/plain", "");
server.on("/status", HTTP_GET, [](){ }, handleFileUpload);
String json = "{"; //get heap status, analog input value and all GPIO statuses in one json call
json += "\"heap\":"+String(ESP.getFreeHeap()); server.on("/esp_status", HTTP_GET, []() {
json += ", \"analog\":"+String(analogRead(A0)); String json = "{";
json += ", \"gpio\":"+String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16))); json += "\"heap\":" + String(ESP.getFreeHeap());
json += "}"; json += ", \"analog\":" + String(analogRead(A0));
server.send(200, "text/json", json); json += ", \"gpio\":" + String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16)));
json = String(); 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([](){ //called when the url is not defined here
if(!handleFileRead(server.uri())) //use it to load content from SPIFFS
handleNotFound(); server.onNotFound([]() {
}); if (!handleFileRead(server.uri()))
handleNotFound();
});
server.on("/upload", handleMinimalUpload);
server.on("/upload", handleMinimalUpload);
// ***************************************************************************
// Setup: SPIFFS Webserver handler server.on("/restart", []() {
// *************************************************************************** DBG_OUTPUT_PORT.printf("/restart:\n");
server.on("/brightness", []() { server.send(200, "text/plain", "restarting..." );
ESP.restart();
});
// ***************************************************************************
// Setup: SPIFFS Webserver handler
// ***************************************************************************
server.on("/set_brightness", []() {
if (server.arg("c").toInt() > 0) { if (server.arg("c").toInt() > 0) {
brightness = (int) server.arg("c").toInt() * 2.55; brightness = (int) server.arg("c").toInt() * 2.55;
} else { } else {
brightness = server.arg("p").toInt(); brightness = server.arg("p").toInt();
} }
if (brightness > 255) { if (brightness > 255) {
brightness = 255; brightness = 255;
} }
if (brightness < 0) { if (brightness < 0) {
brightness = 0; brightness = 0;
} }
strip.setBrightness(brightness); strip.setBrightness(brightness);
if (mode == HOLD) { if (mode == HOLD) {
mode = ALL; mode = ALL;
} }
getStatusJSON(); getStatusJSON();
}); });
server.on("/get_brightness", []() { 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.on("/get_switch", []() {
server.send(200, "text/plain", (mode == OFF) ? "0" : "1" ); 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.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", []() { server.on("/status", []() {
getStatusJSON(); getStatusJSON();
}); });
server.on("/off", []() { server.on("/off", []() {
exit_func = true; exit_func = true;
mode = OFF; mode = OFF;
getArgs(); getArgs();
getStatusJSON(); getStatusJSON();
}); });
server.on("/all", []() { server.on("/all", []() {
exit_func = true; exit_func = true;
mode = ALL; mode = ALL;
getArgs(); getArgs();
getStatusJSON(); getStatusJSON();
}); });
server.on("/wipe", []() { server.on("/wipe", []() {
exit_func = true; exit_func = true;
mode = WIPE; mode = WIPE;
getArgs(); getArgs();
getStatusJSON(); getStatusJSON();
}); });
server.on("/rainbow", []() { server.on("/rainbow", []() {
exit_func = true; exit_func = true;
mode = RAINBOW; mode = RAINBOW;
getArgs(); getArgs();
getStatusJSON(); getStatusJSON();
}); });
server.on("/rainbowCycle", []() { server.on("/rainbowCycle", []() {
exit_func = true; exit_func = true;
mode = RAINBOWCYCLE; mode = RAINBOWCYCLE;
getArgs(); getArgs();
getStatusJSON(); getStatusJSON();
}); });
server.on("/theaterchase", []() { server.on("/theaterchase", []() {
exit_func = true; exit_func = true;
mode = THEATERCHASE; mode = THEATERCHASE;
getArgs(); getArgs();
getStatusJSON(); getStatusJSON();
}); });
server.on("/theaterchaseRainbow", []() { server.on("/theaterchaseRainbow", []() {
exit_func = true; exit_func = true;
mode = THEATERCHASERAINBOW; mode = THEATERCHASERAINBOW;
getArgs(); getArgs();
getStatusJSON(); getStatusJSON();
}); });
server.on("/tv", []() { server.on("/tv", []() {
exit_func = true; exit_func = true;
mode = TV; mode = TV;
getArgs(); getArgs();
getStatusJSON(); getStatusJSON();
}); });
server.begin(); server.begin();
} }
void loop() { void loop() {
server.handleClient(); server.handleClient();
webSocket.loop(); webSocket.loop();
// Simple statemachine that handles the different modes // Simple statemachine that handles the different modes
if (mode == OFF) { if (mode == OFF) {
//colorWipe(strip.Color(0, 0, 0), 50); //colorWipe(strip.Color(0, 0, 0), 50);
uint16_t i; uint16_t i;
for (i = 0; i < strip.numPixels(); i++) { for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 0, 0, 0); strip.setPixelColor(i, 0, 0, 0);
} }
strip.show(); strip.show();
mode = HOLD; //mode = HOLD;
} }
if (mode == ALL) { if (mode == ALL) {
uint16_t i; uint16_t i;
for (i = 0; i < strip.numPixels(); i++) { for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, main_color.red, main_color.green, main_color.blue); strip.setPixelColor(i, main_color.red, main_color.green, main_color.blue);
} }
strip.show(); strip.show();
//mode = HOLD; //mode = HOLD;
} }
if (mode == WIPE) { if (mode == WIPE) {
colorWipe(strip.Color(main_color.red, main_color.green, main_color.blue), delay_ms); colorWipe(strip.Color(main_color.red, main_color.green, main_color.blue), delay_ms);
} }
if (mode == RAINBOW) { if (mode == RAINBOW) {
rainbow(delay_ms); rainbow(delay_ms);
} }
if (mode == RAINBOWCYCLE) { if (mode == RAINBOWCYCLE) {
rainbowCycle(delay_ms); rainbowCycle(delay_ms);
} }
if (mode == THEATERCHASE) { if (mode == THEATERCHASE) {
theaterChase(strip.Color(main_color.red, main_color.green, main_color.blue), delay_ms); theaterChase(strip.Color(main_color.red, main_color.green, main_color.blue), delay_ms);
} }
if (mode == THEATERCHASERAINBOW) { if (mode == THEATERCHASERAINBOW) {
theaterChaseRainbow(delay_ms); theaterChaseRainbow(delay_ms);
} }
if (mode == HOLD) { if (mode == HOLD) {
if (exit_func) { if (exit_func) {
exit_func = false; exit_func = false;
} }
} }
if (mode == TV) { if (mode == TV) {
tv(); tv();
} }
} }

View file

@ -3,53 +3,53 @@
// *************************************************************************** // ***************************************************************************
void getArgs() { void getArgs() {
if (server.arg("rgb") != "") { if (server.arg("rgb") != "") {
uint32_t rgb = (uint32_t) strtol(server.arg("rgb").c_str(), NULL, 16); uint32_t rgb = (uint32_t) strtol(server.arg("rgb").c_str(), NULL, 16);
main_color.red = ((rgb >> 16) & 0xFF); main_color.red = ((rgb >> 16) & 0xFF);
main_color.green = ((rgb >> 8) & 0xFF); main_color.green = ((rgb >> 8) & 0xFF);
main_color.blue = ((rgb >> 0) & 0xFF); main_color.blue = ((rgb >> 0) & 0xFF);
} else { } else {
main_color.red = server.arg("r").toInt(); main_color.red = server.arg("r").toInt();
main_color.green = server.arg("g").toInt(); main_color.green = server.arg("g").toInt();
main_color.blue = server.arg("b").toInt(); main_color.blue = server.arg("b").toInt();
} }
delay_ms = server.arg("d").toInt(); delay_ms = server.arg("d").toInt();
if (main_color.red > 255) { if (main_color.red > 255) {
main_color.red = 255; main_color.red = 255;
} }
if (main_color.green > 255) { if (main_color.green > 255) {
main_color.green = 255; main_color.green = 255;
} }
if (main_color.blue > 255) { if (main_color.blue > 255) {
main_color.blue = 255; main_color.blue = 255;
} }
if (main_color.red < 0) { if (main_color.red < 0) {
main_color.red = 0; main_color.red = 0;
} }
if (main_color.green < 0) { if (main_color.green < 0) {
main_color.green = 0; main_color.green = 0;
} }
if (main_color.blue < 0) { if (main_color.blue < 0) {
main_color.blue = 0; main_color.blue = 0;
} }
if (server.arg("d") == "") { if (server.arg("d") == "") {
delay_ms = 20; delay_ms = 20;
} }
DBG_OUTPUT_PORT.print("Mode: "); DBG_OUTPUT_PORT.print("Mode: ");
DBG_OUTPUT_PORT.print(mode); DBG_OUTPUT_PORT.print(mode);
DBG_OUTPUT_PORT.print(", Color: "); DBG_OUTPUT_PORT.print(", Color: ");
DBG_OUTPUT_PORT.print(main_color.red); DBG_OUTPUT_PORT.print(main_color.red);
DBG_OUTPUT_PORT.print(", "); DBG_OUTPUT_PORT.print(", ");
DBG_OUTPUT_PORT.print(main_color.green); DBG_OUTPUT_PORT.print(main_color.green);
DBG_OUTPUT_PORT.print(", "); DBG_OUTPUT_PORT.print(", ");
DBG_OUTPUT_PORT.print(main_color.blue); DBG_OUTPUT_PORT.print(main_color.blue);
DBG_OUTPUT_PORT.print(", Delay:"); DBG_OUTPUT_PORT.print(", Delay:");
DBG_OUTPUT_PORT.print(delay_ms); DBG_OUTPUT_PORT.print(delay_ms);
DBG_OUTPUT_PORT.print(", Brightness:"); DBG_OUTPUT_PORT.print(", Brightness:");
DBG_OUTPUT_PORT.println(brightness); DBG_OUTPUT_PORT.println(brightness);
} }
void handleMinimalUpload() { void handleMinimalUpload() {
@ -59,7 +59,7 @@ void handleMinimalUpload() {
int hr = min / 60; int hr = min / 60;
snprintf ( temp, 1500, snprintf ( temp, 1500,
"<!DOCTYPE html>\ "<!DOCTYPE html>\
<html>\ <html>\
<head>\ <head>\
<title>ESP8266 Upload</title>\ <title>ESP8266 Upload</title>\
@ -75,159 +75,159 @@ void handleMinimalUpload() {
</form>\ </form>\
</body>\ </body>\
</html>", </html>",
hr, min % 60, sec % 60 hr, min % 60, sec % 60
); );
server.send ( 200, "text/html", temp ); server.send ( 200, "text/html", temp );
} }
void handleNotFound() { void handleNotFound() {
String message = "File Not Found\n\n"; String message = "File Not Found\n\n";
message += "URI: "; message += "URI: ";
message += server.uri(); message += server.uri();
message += "\nMethod: "; message += "\nMethod: ";
message += ( server.method() == HTTP_GET ) ? "GET" : "POST"; message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
message += "\nArguments: "; message += "\nArguments: ";
message += server.args(); message += server.args();
message += "\n"; message += "\n";
for ( uint8_t i = 0; i < server.args(); i++ ) { for ( uint8_t i = 0; i < server.args(); i++ ) {
message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n"; message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
} }
server.send ( 404, "text/plain", message ); server.send ( 404, "text/plain", message );
} }
void getStatusJSON() { void getStatusJSON() {
char json[255]; 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); 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 ); server.send ( 200, "application/json", json );
} }
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) { void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
switch(type) { switch (type) {
case WStype_DISCONNECTED: case WStype_DISCONNECTED:
DBG_OUTPUT_PORT.printf("WS: [%u] Disconnected!\n", num); DBG_OUTPUT_PORT.printf("WS: [%u] Disconnected!\n", num);
break; 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);
// send message to client case WStype_CONNECTED: {
webSocket.sendTXT(num, "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; 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);
case WStype_TEXT: mode = HOLD;
DBG_OUTPUT_PORT.printf("WS: [%u] get Text: %s\n", num, payload); webSocket.sendTXT(num, "OK");
}
// # ==> Set main color // ! ==> Set single LED in given color
if(payload[0] == '#') { if (payload[0] == '!') {
// decode rgb data // decode led index
uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16); uint64_t rgb = (uint64_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 uint8_t led = ((rgb >> 24) & 0xFF);
if(payload[0] == '?') { if (led < strip.numPixels()) {
// decode delay data ledstates[led].red = ((rgb >> 16) & 0xFF);
uint8_t d = (uint8_t) strtol((const char *) &payload[1], NULL, 10); ledstates[led].green = ((rgb >> 8) & 0xFF);
delay_ms = ((d >> 0) & 0xFF); ledstates[led].blue = ((rgb >> 0) & 0xFF);
DBG_OUTPUT_PORT.printf("WS: Set delay to: [%u]\n", delay_ms); DBG_OUTPUT_PORT.printf("WS: Set single led [%u] to [%u] [%u] [%u]!\n", led, ledstates[led].red, ledstates[led].green, ledstates[led].blue);
webSocket.sendTXT(num, "OK");
}
// # ==> Set brightness for (uint8_t i = 0; i < strip.numPixels(); i++) {
if(payload[0] == '%') { strip.setPixelColor(i, ledstates[i].red, ledstates[i].green, ledstates[i].blue);
uint8_t b = (uint8_t) strtol((const char *) &payload[1], NULL, 10); //DBG_OUTPUT_PORT.printf("[%u]--[%u] [%u] [%u] [%u] LED index!\n", rgb, i, ledstates[i].red, ledstates[i].green, ledstates[i].blue);
brightness = ((b >> 0) & 0xFF); }
DBG_OUTPUT_PORT.printf("WS: Set brightness to: [%u]\n", brightness); strip.show();
strip.setBrightness(brightness); }
webSocket.sendTXT(num, "OK"); 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) exit_func = true;
if(payload[0] == '*') { if (str_mode.startsWith("=off")) {
// decode rgb data mode = OFF;
uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16); }
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); DBG_OUTPUT_PORT.printf("Activated mode [%u]!\n", mode);
main_color.green = ((rgb >> 8) & 0xFF); webSocket.sendTXT(num, "OK");
main_color.blue = ((rgb >> 0) & 0xFF); }
break;
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;
}
} }
void checkForRequests() { void checkForRequests() {
webSocket.loop(); webSocket.loop();
server.handleClient(); server.handleClient();
} }