#include <SPI.h>
#include <ESP8266WiFi.h>
const char* ssid = "SSID";
const char* password = "Password";
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(8080);
void setup() {
Serial.begin(115200);
delay(10);
// prepare LED_BUILTIN pin
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 0);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
IPAddress ip(192,168,1,176);
IPAddress gateway(192,168,1,254);
IPAddress subnet(255,255,255,0);
WiFi.begin(ssid, password);
WiFi.config(ip,gateway,subnet);
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.println(WiFi.localIP());
}
void loop() {
//Declare reset function @ address 0
void(* resetFunc) (void) = 0;
// 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 req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
String IP = WiFi.localIP().toString();
// client.print("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now </html>\n");
client.print("<html>");
client.print("<FORM action=/gate"">");
client.print("<input type=""submit"" value=""Gate"" style=""height:300px;width:300px"">");
client.print("</form>");
client.print("</html>");
client.flush();
//client.stop();
// Match the request
int val;
if (req.indexOf("/gate")!=-1)
val=1;
else {
Serial.println("invalid request");
client.stop();
return;
}
// Set LED_BUILTIN On sleep 500 ms then LED_BUILTIN off
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
client.flush();
//Prepare the response
client.print("<html>\r\nGate Button Pressed! </html>\n");
delay(1);
Serial.println("Client disonnected");
delay(5000);
resetFunc(); //call reset
}