Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Load Wi-Fi library
- #include <ESP8266WiFi.h>
- #include <FastLED.h>
- // Replace with your network credentials
- const char* ssid = "BaeschdeWEGE";
- const char* password = "wasistdasfuer1Muesli";
- // Set web server port number to 80
- WiFiServer server(80);
- // Variable to store the HTTP request
- String header;
- #define LED_PIN 7
- #define NUM_LEDS 20
- CRGB leds[NUM_LEDS];
- // Current time
- unsigned long currentTime = millis();
- // Previous time
- unsigned long previousTime = 0;
- // Define timeout time in milliseconds (example: 2000ms = 2s)
- const long timeoutTime = 2000;
- // Function to check if string is a number
- boolean isValidNumber(String str){
- for(byte i=0;i<str.length();i++)
- {
- if(!isDigit(str.charAt(i))) return false;
- }
- return true;
- }
- void setup() {
- FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
- Serial.begin(115200);
- // Connect to Wi-Fi network with SSID and password
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- // Print local IP address and start web server
- Serial.println("");
- Serial.println("WiFi connected.");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- server.begin();
- }
- void loop(){
- WiFiClient client = server.available(); // Listen for incoming clients
- if (client) { // If a new client connects,
- Serial.println("New Client."); // print a message out in the serial port
- String currentLine = ""; // make a String to hold incoming data from the client
- currentTime = millis();
- previousTime = currentTime;
- while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
- currentTime = millis();
- if (client.available()) { // if there's bytes to read from the client,
- char c = client.read(); // read a byte, then
- Serial.write(c); // print it out the serial monitor
- header += c;
- if (c == '\n') { // if the byte is a newline character
- // if the current line is blank, you got two newline characters in a row.
- // that's the end of the client HTTP request, so send a response:
- if (currentLine.length() == 0) {
- // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
- // and a content-type so the client knows what's coming, then a blank line:
- client.println("HTTP/1.1 200 OK");
- client.println("Content-type:text/html");
- client.println("Connection: close");
- client.println();
- //Gets the Inputs from the URL
- String hue = header.substring(header.indexOf("hue=")+String("hue=").length(), header.indexOf("hue=")+String("hue=").length()+5);
- while (!isValidNumber(hue)){
- hue = hue.substring(0, hue.length()-1);
- }
- String lightness = header.substring(header.indexOf("lightness=")+String("lightness=").length(), header.indexOf("lightness=")+String("lightness=").length()+3);
- while (!isValidNumber(lightness)){
- lightness = lightness.substring(0, lightness.length()-1);
- }
- String statusConst = header.substring(header.indexOf("statusConst=")+String("statusConst=").length(), header.indexOf("statusConst=")+String("statusConst=").length()+1);
- // Display the HTML web page
- client.println("<!DOCTYPE html><html>");
- client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
- client.println("<link rel=\"icon\" href=\"data:,\">");
- // CSS to style the on/off buttons
- // Feel free to change the background-color and font-size attributes to fit your preferences
- client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
- client.println(".button { background-color: #FCFCFC; width: 133px; border: none; color: white; padding: 16px 40px;"
- "text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
- client.println(".button2 {background-color: #3B3B3B; width: 133px; border: none; color: black; padding: 16px 40px;"
- "text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}</style></head>;"
- );
- // Web Page Heading
- client.println("<body style=\"background-color:black;\"><h1><font color=\"white\">LED Control Terminal</font></h1>");
- // If the output5State is off, it displays the ON button
- if(statusConst.toInt() == 1){
- client.println("<p><button class = 'button' ontouchend = \"buttonClick()\" onmouseup = \"buttonClick()\" id=\"button\"><font color=\"black\">ON</font></button></p>");
- } else{
- client.println("<p><button class = 'button2' ontouchend = \"buttonClick()\" onmouseup = \"buttonClick()\" id=\"button\"><font color=\"white\">OFF</font></button></p>");
- }
- client.println("<p>"+hue+"</p>");
- client.println("<p>"+lightness+"</p>");
- //css for sliders
- client.println("<style>"
- ".slider {"
- "-webkit-appearance: none;"
- "width: 100%;"
- "height: 25px;"
- "background: linear-gradient(to right, red 0%, #ff0 17%, lime 33%, cyan 50%, blue 66%, #f0f 83%, red 100%);"
- "outline: none;"
- "opacity: 1;"
- "-webkit-transition: .2s;"
- "transition: opacity .2s;"
- "}"
- ".slider:hover {"
- "opacity: 1;"
- "}"
- ".slider::-webkit-slider-thumb {"
- "-webkit-appearance: none;"
- "appearance: none;"
- "width: 25px;"
- "height: 25px;"
- "background: hsl(180, 0%, 80%);"
- "cursor: pointer;"
- "opacity: 0.7;"
- "}"
- ".slider::-moz-range-thumb {"
- "width: 25px;"
- "height: 25px;"
- "background: hsl(180, 0%, 80%);"
- "cursor: pointer;"
- "opacity: 0.7;"
- "}"
- ".sliderLightness {"
- "-webkit-appearance: none;"
- "width: 100%;"
- "height: 25px;"
- "background: linear-gradient(to right, hsl("+hue+", 100%, 0%), hsl("+hue+", 100%, 50%), hsl("+hue+", 100%, 100%));"
- "outline: none;"
- "opacity: 1;"
- "-webkit-transition: .2s;"
- "transition: opacity .2s;"
- "}"
- ".sliderLightness:hover {"
- "opacity: 1;"
- "}"
- ".sliderLightness::-webkit-slider-thumb {"
- "-webkit-appearance: none;"
- "appearance: none;"
- "width: 25px;"
- "height: 25px;"
- "background: hsl(180, 0%, 80%);"
- "cursor: pointer;"
- "opacity: 0.7;"
- "}"
- ".sliderLightness::-moz-range-thumb {"
- "width: 25px;"
- "height: 25px;"
- "background: hsl(180, 0%, 80%);"
- "cursor: pointer;"
- "opacity: 0.7;"
- "}"
- "</style>");
- // create hue slider
- client.println("<div class=\"slidecontainer\">"
- "<input ontouchend = \"sliderClick()\" onmouseup = \"sliderClick()\" type=\"range\" min=\"0\" max=\"359\" class=\"slider\" id=\"hue\">"
- "</div>");
- //create lightness slider
- client.println("<div class=\"slidecontainer\">"
- "<input ontouchend = \"sliderClick()\" onmouseup = \"sliderClick()\" type=\"range\" min=\"0\" max=\"100\" class=\"sliderLightness\" id=\"lightness\">"
- "</div>");
- client.println("<p id=\"hue\"></p>"
- "<p id=\"lightness\"></p>"
- );
- client.println( "<script type=\"text/javascript\"> "
- "(function (){"
- "const urlParams = new URLSearchParams(window.location.search);"
- "document.getElementById(\"hue\").value = urlParams.get('hue');"
- "document.getElementById(\"hue\").innerHTML = urlParams.get('hue');"
- "document.getElementById(\"lightness\").value = urlParams.get('lightness');"
- "document.getElementById(\"lightness\").innerHTML = urlParams.get('lightness');"
- "})();"
- "function buttonClick(){"
- "const urlParams = new URLSearchParams(window.location.search);"
- "console.log('bierbierbier');"
- "if (urlParams.get('statusConst') == 1){"
- // "document.getElementById(\"button\").className = 'button2'; "
- // "document.getElementById(\"button\").innerHTML = 'OFF';"
- "urlParams.set(\"statusConst\", 0);"
- "urlParams.set(\"hue\", document.getElementById(\"hue\").value);"
- "urlParams.set(\"lightness\", document.getElementById(\"lightness\").value);"
- "} else{"
- // "document.getElementById(\"button\").className = 'button'; "
- // "document.getElementById(\"button\").innerHTML = 'ON';"
- "urlParams.set(\"statusConst\", 1);"
- "urlParams.set(\"hue\", document.getElementById(\"hue\").value);"
- "urlParams.set(\"lightness\", document.getElementById(\"lightness\").value);"
- "}"
- "window.location.search =urlParams.toString();"
- "}"
- "function sliderClick(){"
- "const urlParams = new URLSearchParams(window.location.search);"
- "urlParams.set(\"hue\", document.getElementById(\"hue\").value);"
- "urlParams.set(\"lightness\", document.getElementById(\"lightness\").value);"
- "var outputHue = document.getElementById(\"hue\");"
- "var outputLightness = document.getElementById(\"lightness\");"
- "window.location.search =urlParams.toString();"
- ""
- "} </script>"
- );
- client.println("</body></html>");
- // The HTTP response ends with another blank line
- client.println();
- // Break out of the while loop
- break;
- } else { // if you got a newline, then clear currentLine
- currentLine = "";
- }
- } else if (c != '\r') { // if you got anything else but a carriage return character,
- currentLine += c; // add it to the end of the currentLine
- }
- }
- }
- // Clear the header variable
- header = "";
- // Close the connection
- client.stop();
- Serial.println("Client disconnected.");
- Serial.println("");
- }
- // LED control
- // Hue adjustment
- hue = round(hue.toInt()*255/360);
- lightness = round(lightness.toInt()*255/100);
- if (statusConst.toInt()==1){
- for (int i = 0; i <= 19; i++) {
- leds[i] = CHSV ( hue,255, lightness);
- FastLED.show();
- delay(40);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement