/* Rupak Poddar
		 * HackUMass IX
		 *
		 * This program lets you exchange data between a web browser and ESP8266
		   board connected to a local Wi-Fi network using point-to-point
		   protocol(PPP). With this example, you can control the built-in LED of
		   the ESP8266 board by connecting to the local IP address with any web
		   browser on the same network.
		 */

		#include <ESP8266WiFi.h>

		const char* ssid = "CHANGE_THIS";      // Your SSID
		const char* password = "CHANGE_THIS"; // Your password

		int ledPin = 16;  // GPIO pin D0 of ESP8266 NodeMCU
		WiFiServer server(80);  // Service Port for http

		void setup() {
		  Serial.begin(115200);
		  delay(1000);

		  pinMode(ledPin, OUTPUT);
		  digitalWrite(ledPin, LOW);

		  //Connect to WiFi network
		  Serial.println();
		  Serial.println();
		  Serial.print("Connecting to ");
		  Serial.println(ssid);

		  WiFi.begin(ssid, password);

		  while (WiFi.status() != WL_CONNECTED) {
		    delay(500);
		    Serial.print("-");
		  }
		  Serial.println("");
		  Serial.println("WiFi connected");

		  // Start the server
		  server.begin();
		  Serial.println("Server started");

		  // Print the IP address
		  Serial.print("Use this URL to connect: ");
		  Serial.print("http://");
		  Serial.print(WiFi.localIP());
		  Serial.println("/");
		  
		  digitalWrite(ledPin, HIGH);
		}

		void loop() {
		  // Check if a client has connected
		  WiFiClient client = server.available();
		  if (!client) {
		    return;
		  }

		  // Wait until the client sends some data
		  Serial.println("new client");
		  while(!client.available()){
		    delay(1);
		  }

		  // Read the first line of the request
		  String request = client.readStringUntil('\r');
		  Serial.println(request);
		  client.flush();

		  //Match the request. Set ledPin according to the request.
		  int value = LOW;
		  if (request.indexOf("/LED=ON") != -1) {
		    digitalWrite(ledPin, HIGH);
		    value = HIGH;
		  }
		  if (request.indexOf("/LED=OFF") != -1){
		    digitalWrite(ledPin, LOW);
		    value = LOW;
		  }

		  // Return the response
		  client.println("HTTP/1.1 200 OK");
		  client.println("Content-Type: text/html");
		  client.println(""); //  do not forget this one
		  client.println("<!DOCTYPE HTML>");
		  client.println("<html>");

		  client.print("Led pin is now: ");

		  if(value == HIGH) {
		    client.print("On");
		  } else {
		    client.print("Off");
		  }
		  client.println("<br><br>");
		  client.println("Click <a href=\"/LED=ON\">here</a> turn the LED on pin 2 ON<br>");
		  client.println("Click <a href=\"/LED=OFF\">here</a> turn the LED on pin 2 OFF<br>");
		  client.println("</html>");

		  delay(1);
		  Serial.println("Client disconnected");
		  Serial.println("");
		}