/*
 * ESP8266 Servo Motor Control With Web Server 
 * https://circuits4you.com
 */
 #include <FS.h>   //Include File System Headers
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <Servo.h>
#include "index.h";

const char* htmlfile = "/index.html";

//WiFi Connection configuration
const char* ssid = "GC4BZW6";
const char* password = "maxanais44";

ESP8266WebServer server(80); //Server on port 80

const uint8_t servoPin = D4;

#define ServoPin D4   //D5 is GPIO14

Servo myservo;  // create servo object to control a servo

void Root() {
 String s = MAIN_page; //Read HTML contents
 server.send(200, "text/html", s); //Send web page
 server.sendHeader("Location", "/index.html",true);   //Redirect to our html web page
}
//================================================
void handleServo(){
  String POS = server.arg("servoPOS");
  int pos = POS.toInt();
  myservo.write(pos);   // tell servo to go to position
  delay(15);
  Serial.print("Servo Angle:");
  Serial.println(pos);
  server.send(200, "text/plane","");
}
void handleWebRequests(){
  if(loadFromSpiffs(server.uri())) return;
  String message = "File Not Detected\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " NAME:"+server.argName(i) + "\n VALUE:" + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
  Serial.println(message);
}
//================================================
//            Setup
//================================================
void setup() {
  Serial.begin(115200);
  Serial.println("");
  WiFi.mode(WIFI_AP_STA);  //Both hotspot and client are enabled
  WiFi.softAP(ssid, password);  //Start HOTspot removing password will disable security

  IPAddress myIP = WiFi.softAPIP(); //Get IP address
  Serial.print("HotSpt IP:");
  Serial.println(myIP);
  server.on("/", Root);      //Which routine to handle at root location
  server.begin();                  //Start server
  Serial.println("HTTP server started");
  //Initialize File System
  SPIFFS.begin();
  Serial.println("File System Initialized");
  myservo.attach(ServoPin); // attaches the servo on GIO2 to the servo object
  
 
  //Initialize Webserver
  server.on("/",Root);
  server.on("/page1",Root);
  server.on("/page2",Root);
  server.on("/setPOS",handleServo); //Sets servo position from Web request
}
//================================================
//     LOOP
//================================================
void loop() {
 server.handleClient();
}
bool loadFromSpiffs(String path){
  String dataType = "text/plain";
  if(path.endsWith("/")) path += "index.htm";

  if(path.endsWith(".src")) path = path.substring(0, path.lastIndexOf("."));
  else if(path.endsWith(".html")) dataType = "text/html";
  else if(path.endsWith(".htm")) dataType = "text/htm";
  else if(path.endsWith(".css")) dataType = "text/css";
  else if(path.endsWith(".js")) dataType = "application/javascript";
  else if(path.endsWith(".png")) dataType = "image/png";
  else if(path.endsWith(".gif")) dataType = "image/gif";
  else if(path.endsWith(".jpg")) dataType = "image/jpeg";
  else if(path.endsWith(".ico")) dataType = "image/x-icon";
  else if(path.endsWith(".xml")) dataType = "text/xml";
  else if(path.endsWith(".pdf")) dataType = "application/pdf";
  else if(path.endsWith(".zip")) dataType = "application/zip";
  File dataFile = SPIFFS.open(path.c_str(), "r");
  if (server.hasArg("download")) dataType = "application/octet-stream";
  if (server.streamFile(dataFile, dataType) != dataFile.size()) {
  }

  dataFile.close();
  return true;
}
//================================================
