#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <Adafruit_NeoPixel.h>

//SSID and Password of your WiFi router
const char* ssid = "CrazyScience";
const char* password = "Crazy@12345";

ESP8266WebServer server(80);

// NeoPixel LED strip configuration
#define NUM_PIXELS 35
#define NEOPIXEL_PIN 15 // D8 GPIO15

Adafruit_NeoPixel strip(NUM_PIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);

String setcolor = "#ff00ff"; // Set color for HTML

const char MAIN_page[] PROGMEM = R"=====(
<!DOCTYPE html>
<html>
<head>
<title>Mood Lamp (RGB LED Color) Controller</title>
</head>
<body style="background:@@color@@;">
<center>
    Mood Lamp (RGB LED Color) Controller<br><br><br><br><br>
  <form method="post" action="/form">
      Color:
      <input type="color" name="color" value="@@color@@">
      <button type="submit" name="state" value="stop">SET</button>
  </form>
<br><br><br>
<a href="https://letsmakeprojects.com">letsmakeprojects.com</a><br><br><br><br>
   <p><br><marquee> FOR MORE AMAZING PROJECTS DO VISIT LETSMAKEPROJECTS.COM </marquee></p>
</center>
</body>
</html>
)=====";

//=======================================================================
// Handles the main page
//=======================================================================
void handleRoot() {
  String p = MAIN_page;
  p.replace("@@color@@", setcolor); // Set page background color and selected color
  server.send(200, "text/html", p);
}

//=======================================================================
// Handle Set Color
//=======================================================================
void handleForm() {
  String color = server.arg("color");
  setcolor = color; // Store actual color set for updating in HTML
  Serial.println(color);

  strip.fill(strip.Color(
    strtol(&color[1], NULL, 16) >> 16 & 0xFF, // Red
    strtol(&color[1], NULL, 16) >> 8 & 0xFF, // Green
    strtol(&color[1], NULL, 16) & 0xFF // Blue
  ));
  strip.show();

  server.sendHeader("Location", "/");
  server.send(302, "text/plain", "Updated-- Press Back Button");

  delay(500);
}

//=======================================================================
// SETUP
//=======================================================================
void setup() {
  Serial.begin(115200); // Start serial connection

  strip.begin();
  strip.show(); // Initialize LED strip

  WiFi.begin(ssid, password); // Connect to your WiFi router
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  // If connection successful, show IP address in the serial monitor
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP()); // IP address assigned to your ESP

  server.on("/", handleRoot); // Associate handler function to path
  server.on("/form", handleForm);

  server.begin(); // Start server
  Serial.println("HTTP server started");
}

//=======================================================================
// LOOP
//=======================================================================
void loop() {
  server.handleClient();
}
