/* 11/12/2021
 * NodeMCU configurato come Access Point e Web Server per gestione client Slider per lampada LED esterna Gropparello
 * Variazione della tensione di alimentazione della lampada impostata tramite potenziometro digitale MCP4132 collegato 
 * ad un dc/dc converter XL6009 (commenti e collegamenti vedi sketch <NodeMCU_MCP4132>
 */

#include <ESP8266WiFi.h> // Include the Wi-Fi library
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <SPI.h>   

#define MCP_PIN 15      // Chip Select MCP4132:  GPIO15 (NodeMCU D8)

int potVal = B00000001;  // valore iniziale Rmin = 80R


const char *ssid = "****************"; // The name of the Wi-Fi network that will be created
const char *password = "****************"; // The password required to connect to it, leave blank for an open network

const int output = 4;

String sliderValue = "0";

const char* PARAM_INPUT = "value";

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Outdoor Garage Lamp</title>
  <style>
    html {font-family: Arial; display: inline-block; text-align: center;}
    h2 {font-size: 2.3rem;}
    p {font-size: 1.9rem;}
    body {max-width: 400px; margin:0px auto; padding-bottom: 25px;}
    .slider { -webkit-appearance: none; margin: 14px; width: 360px; height: 25px; background: #FFD65C;
      outline: none; -webkit-transition: .2s; transition: opacity .2s;}
    .slider::-webkit-slider-thumb {-webkit-appearance: none; appearance: none; width: 35px; height: 35px; background: #003249; cursor: pointer;}
    .slider::-moz-range-thumb { width: 35px; height: 35px; background: #003249; cursor: pointer; } 
  </style>
</head>
<body>
  <h2>Outdoor Garage Lamp</h2>
  <p><span id="textSliderValue">%SLIDERVALUE%</span></p>
  <p><input type="range" onchange="updateSliderPWM(this)" id="pwmSlider" min="1" max="100" value="%SLIDERVALUE%" step="1" class="slider"></p>
<script>
function updateSliderPWM(element) {
  var sliderValue = document.getElementById("pwmSlider").value;
  document.getElementById("textSliderValue").innerHTML = sliderValue;
  console.log(sliderValue);
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "/slider?value="+sliderValue, true);
  xhr.send();
}
</script>
</body>
</html>
)rawliteral";

// Replaces placeholder with button section in your web page
String processor(const String& var){
  //Serial.println(var);
  if (var == "SLIDERVALUE"){
    return sliderValue;
  }
  return String();
}



void setup() {
    Serial.begin(115200);
    delay(10);
    Serial.println('\n');

    pinMode(MCP_PIN, OUTPUT);
    digitalWrite(MCP_PIN,HIGH);

    analogWrite(output, sliderValue.toInt());

   SPI.begin();   
   SPI.setFrequency(1000000); //fmax 4132 = 10MHz, 1MHz enough
   
   digitalWrite(MCP_PIN, LOW);
   SPI.transfer(B00000000); // Comando Write
   SPI.transfer(potVal); // 
   digitalWrite(MCP_PIN, HIGH);
   Serial.print ("potVal:    ");
   Serial.println (potVal);
   delay(50);
   
 

    WiFi.softAP(ssid, password); // Start the access point
    Serial.print("Access Point \"");
    Serial.print(ssid);
    Serial.println("\" started");
    Serial.print("IP address:\t");
    Serial.println(WiFi.softAPIP()); // Send the IP address of the ESP8266 to the computer


  // Route for root / web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", index_html, processor);
  });

  // Send a GET request to <ESP_IP>/slider?value=<inputMessage>
  server.on("/slider", HTTP_GET, [] (AsyncWebServerRequest *request) {
    String inputMessage;
    // GET input1 value on <ESP_IP>/slider?value=<inputMessage>
    if (request->hasParam(PARAM_INPUT)) {
      inputMessage = request->getParam(PARAM_INPUT)->value();
      sliderValue = inputMessage;
 
    // analogWrite(output, sliderValue.toInt());
    potVal = (sliderValue.toInt());
    digitalWrite(MCP_PIN, LOW);
    SPI.transfer(B00000000); // Comando Write
    SPI.transfer(potVal); // 
    digitalWrite(MCP_PIN, HIGH);
    Serial.print ("potVal:    ");
    Serial.println (potVal);
      
    
    }
    else {
      inputMessage = "No message sent";
    }
    Serial.println(inputMessage);
    request->send(200, "text/plain", "OK");
  });
  
  // Start server
  server.begin();
}

void loop() {
      
    
}
