Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Load Wi-Fi library
- #include <ESP8266WiFi.h>
- #include <LiquidCrystal.h>
- #include <Adafruit_NeoPixel.h>
- #include <ESP8266WebServer.h>
- #include <WiFiClient.h>
- // Replace with your network credentials
- const char* ssid = "BaeschdeWEGE";
- const char* password = "wasistdasfuer1Muesli";
- // Set web server port number to 80
- ESP8266WebServer server(80);
- IPAddress staticIP(192, 168, 0, 171); //ESP static ip
- IPAddress gateway(192, 168, 0, 1); //IP Address of your WiFi Router (Gateway)
- IPAddress subnet(255, 255, 255, 0); //Subnet mask
- IPAddress dns(8,8,8,8); //DNS
- const char* deviceName = "LedBoiiii";
- // Variable to store the HTTP request
- String header;
- #define LED_PIN 5
- #define NUM_LEDS 300
- Adafruit_NeoPixel pixels(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
- //Define global input variables for LED-Strip
- String hue;
- String lightness;
- String statusConst;
- String saturation;
- String statusParty;
- uint16_t partyspeed;
- uint16_t partycolor;
- // Current time
- unsigned long currentTime = millis();
- // Previous time
- unsigned long previousTime = 0;
- // Define timeout time in milliseconds (example: 2000ms = 2s)
- const long timeoutTime = 2000;
- //Display
- const int rs = 4, en = 0, d4 = 12, d5 = 13, d6 = 15, d7 = 3;
- LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
- //Website ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- const char upper[] PROGMEM = R"=====(
- <!DOCTYPE html><html>
- <head><meta name= "viewport " content= "width=device-width, initial-scale=1">
- <link rel= "icon " href= "data:,">
- <style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}
- .button { background-color: #FCFCFC; width: 133px; border: none; color: black; padding: 16px 40px;
- text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}
- .button2 {background-color: #3B3B3B; width: 133px; border: none; color: white; padding: 16px 40px;
- text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}
- </style></head>;
- <body style= "background-color:black; "><h1><font color= "white">LED Control Terminal</font></h1>
- <p>
- <button class = 'button' ontouchend = "buttonClick()" onmouseup = "buttonClick()" id= "buttonConst"><font color= "black">CONST ON</font></button>
- <button class = 'button' ontouchend = "partyClick()" onmouseup = "partyClick()" id= "buttonParty"><font color= "black">PARTY ON</font></button>
- </p>
- <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(0, 100%, 0%), hsl(0, 100%, 50%), hsl(0, 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;
- }
- .sliderSaturation {
- -webkit-appearance: none;
- width: 100%;
- height: 25px;
- background: linear-gradient(to right, hsl(0, 0%, 50%), hsl(0, 50%, 50%), hsl(0, 100%, 50%));
- outline: none;
- opacity: 1;
- -webkit-transition: .2s;
- transition: opacity .2s;
- }
- .sliderSaturation:hover {
- opacity: 1;
- }
- .sliderSaturation::-webkit-slider-thumb {
- -webkit-appearance: none;
- appearance: none;
- width: 25px;
- height: 25px;
- background: hsl(180, 0%, 80%);
- cursor: pointer;
- opacity: 0.7;
- }
- .sliderSaturation::-moz-range-thumb {
- width: 25px;
- height: 25px;
- background: hsl(180, 0%, 80%);
- cursor: pointer;
- opacity: 0.7;
- }
- .sliderPartyspeed {
- -webkit-appearance: none;
- width: 100%;
- height: 25px;
- background: Grey;
- outline: none;
- opacity: 1;
- -webkit-transition: .2s;
- transition: opacity .2s;
- }
- .sliderPartyspeed:hover {
- opacity: 1;
- }
- .sliderPartyspeed::-webkit-slider-thumb {
- -webkit-appearance: none;
- appearance: none;
- width: 25px;
- height: 25px;
- background: hsl(180, 0%, 80%);
- cursor: pointer;
- opacity: 0.7;
- }
- .sliderPartyspeed::-moz-range-thumb {
- width: 25px;
- height: 25px;
- background: hsl(180, 0%, 80%);
- cursor: pointer;
- opacity: 0.7;
- }
- </style>
- <div class= "slidecontainer">"
- <input ontouchend = "sliderClick() " onmouseup = "sliderClick()" type= "range" min= "0" max= "359" class= "slider" id= "hue">
- </div><br>
- <div class= "slidecontainer">
- <input ontouchend = "sliderClick() " onmouseup = "sliderClick()" type= "range" min= "0" max= "100" class= "sliderLightness" id= "lightness">
- </div><br>
- <div class= "slidecontainer">
- <input ontouchend = "sliderClick() " onmouseup = "sliderClick()" type= "range" min= "0" max= "100" value = "100" class= "sliderSaturation" id= "saturation">
- </div><br>
- <div class= "slidecontainer">
- <input ontouchend = "sliderClick()" onmouseup = "sliderClick()" type= "range" min= "10" max= "300" class= "sliderPartyspeed" id= "partyspeed">
- </div>
- <script type= "text/javascript">
- (function (){
- const urlParams = new URLSearchParams(window.location.search);
- const x = document.getElementById("partyspeed");
- 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');
- document.getElementById( "saturation").value = urlParams.get('saturation');
- document.getElementById( "saturation").innerHTML = urlParams.get('saturation');
- document.getElementById( "partyspeed").value = urlParams.get('partyspeed');
- document.getElementById( "partyspeed").innerHTML = urlParams.get('partyspeed');
- if (urlParams.get('statusConst') == 1){
- document.getElementById( "buttonConst").className = "button";
- document.getElementById( "buttonConst").innerHTML = "Const ON";
- } else{
- document.getElementById( "buttonConst").className = "button2";
- document.getElementById( "buttonConst").innerHTML = "Const OFF";
- }
- if (urlParams.get('statusParty') == 1){
- document.getElementById( "buttonParty").className = "button";
- document.getElementById( "buttonParty").innerHTML = "Party ON";
- x.style.display = "block";
- } else{
- document.getElementById( "buttonParty").className = "button2";
- document.getElementById( "buttonParty").innerHTML = "Party OFF";
- x.style.display = "none";
- }
- })();
- function partyClick(){
- const x = document.getElementById("partyspeed");
- const buttontoggle = document.getElementById("buttonParty");
- const urlParams = new URLSearchParams(window.location.search);
- var pfad;
- if (urlParams.get('statusParty') == 1){
- urlParams.set( "statusConst", 0);
- urlParams.set( "statusParty", 0);
- urlParams.set( "hue", document.getElementById( "hue").value);
- urlParams.set( "lightness", document.getElementById( "lightness").value);
- urlParams.set( "saturation", document.getElementById( "saturation").value);
- pfad = "/PartyOff";
- buttontoggle.className = "button";
- buttontoggle.innerHTML = "Party OFF";
- } else{
- urlParams.set( "statusConst", 0);
- urlParams.set( "statusParty", 1);
- urlParams.set( "hue", document.getElementById( "hue").value);
- urlParams.set( "lightness", document.getElementById( "lightness").value);
- urlParams.set( "saturation", document.getElementById( "saturation").value);
- pfad = "/PartyOn";
- buttontoggle.className = "button2";
- buttontoggle.innerHTML = "Party ON";
- }
- window.location.href = pfad+'?'+urlParams.toString();
- }
- function buttonClick(){
- var pfad;
- const urlParams = new URLSearchParams(window.location.search);
- console.log('bierbierbier');
- //var buttontoggle = document.getElementById("buttonConst");
- const buttontoggle = document.getElementById("buttonConst");
- if (urlParams.get('statusConst') == 1){
- urlParams.set( "statusConst", 0);
- urlParams.set( "hue", document.getElementById( "hue").value);
- urlParams.set( "lightness", document.getElementById( "lightness").value);
- urlParams.set( "saturation", document.getElementById( "saturation").value);
- pfad ="/Off";
- buttontoggle.className = "button2";
- buttontoggle.innerHTML = "Const OFF";
- } else{
- urlParams.set( "statusParty", 0);
- urlParams.set( "statusConst", 1);
- urlParams.set( "hue", document.getElementById( "hue").value);
- urlParams.set( "lightness", document.getElementById( "lightness").value);
- urlParams.set( "saturation", document.getElementById( "saturation").value);
- pfad ="/ConstOn";
- buttontoggle.className = "button";
- buttontoggle.innerHTML = "Const ON";
- }
- window.location.href = pfad+'?'+urlParams.toString();
- console.log(window.location.href);
- }
- function sliderClick(){
- const urlParams = new URLSearchParams(window.location.search);
- urlParams.set( "hue", document.getElementById( "hue").value);
- urlParams.set( "lightness", document.getElementById( "lightness").value);
- urlParams.set( "saturation", document.getElementById( "saturation").value);
- if (typeof partyspeed != 'undefined') {
- urlParams.set( "partyspeed", document.getElementById( "partyspeed").value);
- var outputPartyspeed = document.getElementById( "partyspeed");
- }
- var outputHue = document.getElementById( "hue");
- var outputLightness = document.getElementById( "lightness");
- var outputSaturation = document.getElementById( "saturation");
- window.location.search =urlParams.toString();
- } </script>
- </body></html>
- )=====";
- //Handler Functions
- //////////////////////////////////////////////////////////////
- void handleRoot(){
- Serial.println("You called root page");
- //server.setContentLength(13898);
- server.send_P(200, "text/html", upper);
- }
- void handleConst(){
- Serial.println("You turned on a Constant Color");
- statusConst = "1";
- statusParty = "0";
- hue = server.arg("hue");
- saturation = server.arg("saturation");
- lightness = server.arg("lightness");
- uint16_t hueint = round(hue.toFloat() * (65536 / 360));
- uint8_t lightnessint = round(lightness.toFloat() * (2.55));
- uint8_t saturationint = round(saturation.toFloat() * (2.55));
- pixels.fill(pixels.ColorHSV(hueint, saturationint, lightnessint), 0, 300);
- pixels.show();
- }
- void handleOff(){
- Serial.println("You turned off a Constant Color");
- statusConst = "0";
- statusParty = "0";
- pixels.clear();
- pixels.show();
- }
- void handleParty(){
- Serial.println("You turned on the Party");
- lightness = server.arg("lightness");
- statusConst = "0";
- statusParty = "1";
- }
- /////////////////////////////
- // Setup
- /////////////////////////////
- void setup() {
- statusParty = "0";
- statusConst = "0";
- ESP.wdtEnable(10000);
- Serial.begin(9600);
- // Connect to Wi-Fi network with SSID and password
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.hostname(deviceName); // DHCP Hostname (useful for finding device for static lease)
- WiFi.config(staticIP, subnet, gateway, dns);
- WiFi.begin(ssid, password);
- WiFi.mode(WIFI_STA);
- 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: ");
- String timepassed = String(millis() / 1000);
- server.on("/", handleRoot); //Which routine to handle at root location. This is display page
- server.on("/PartyOn", handleParty);
- server.on("/ConstOn", handleConst);
- server.on("/Off", handleOff);
- Serial.println("Time to connect:" + timepassed + "s");
- Serial.println(WiFi.localIP());
- server.begin();
- lcd.begin(16, 2);
- lcd.print("Ip-Adresse:");
- lcd.setCursor(0, 1);
- lcd.print(WiFi.localIP());
- pixels.begin();
- partycolor = 0;
- }
- //Utility Functions
- // 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;
- }
- //function for the partymode := change color once per loop
- void Party() {
- if (statusParty.toInt() == 1) {
- if (partycolor >= 65530) {
- partycolor = 0;
- } else {
- partycolor = partycolor + partyspeed;
- }
- uint8_t lightnessint = round(lightness.toFloat() * (2.55));
- pixels.fill(pixels.ColorHSV(partycolor, 255, lightnessint), 0, 300);
- pixels.show();
- } else if (statusConst.toInt() == 0) {
- pixels.clear();
- pixels.show();
- }
- }
- void loop() {
- server.handleClient();
- Party();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement