// Load Wi-Fi library #include const char* ssid = "TOT"; const char* password = "0815610067"; WiFiServer server(80); // Set web server port number to 80 // Variable to store the HTTP request String header; // Auxiliar variables to store the current output state String output1State = "off"; String output2State = "off"; // Assign output variables to GPIO pins const int output1 = D1; const int output2 = D2; #define output1_OFF digitalWrite(output1, LOW) #define output1_ON digitalWrite(output1, HIGH) #define output2_OFF digitalWrite(output2, LOW) #define output2_ON digitalWrite(output2, HIGH) String button5 = "

"; String button4 = "

"; const String http_header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n Connection: close \r\n\r\n"; //----------------------------------------------------------- // Cheak url string void check_header(void){ // turns the GPIOs on and off if (header.indexOf("GET /5/on") >= 0) { Serial.println("GPIO 5 on"); output1State = "on"; output1_ON; button5 = "

"; } else if (header.indexOf("GET /5/off") >= 0) { Serial.println("GPIO 5 off"); output1State = "off"; output1_OFF; button5 = "

"; } else if (header.indexOf("GET /4/on") >= 0) { Serial.println("GPIO 4 (D2) on"); output2State = "on"; output2_ON; button4 = "

"; } else if (header.indexOf("GET /4/off") >= 0) { Serial.println("GPIO 4 (D2) off"); output2State = "off"; output2_OFF; button4 = "

"; } } //----------------------------------------------------------- // Prepare page String getPage(){ String page = ""; page += ""; //'\"' ให้ใส่เครื่องหมาย " page += ""; page += ""; page += "

ESP8266 Web Server

"; page += "

GPIO 5 [D1] - State "; page += output1State; page += "

"; page += button5; page += "

GPIO 4 [D2] - State "; page += output2State; page += "

"; page += button4; page += "\r\n"; return page; } //----------------------------------------------------------- void setup() { Serial.begin(115200); // Initialize the output variables as outputs pinMode(output1, OUTPUT); pinMode(output2, OUTPUT); output1_OFF; // Set Off all output2_OFF; // Set Off all // Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } // Print local IP address and start web server Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); server.begin(); } void loop(){ WiFiClient client = server.available(); // Listen for incoming clients if (client) { // If a new client connects, Serial.println("New Client."); // print a message out in the serial port String currentLine = ""; // make a String to hold incoming data from the client while (client.connected()) { // loop while the client's connected if (client.available()) { // if there's bytes to read from the client, char c = client.read(); // read a byte, then Serial.write(c); // print it out the serial monitor header += c; if (c == '\n') { // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) { // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println(http_header); check_header(); client.print(getPage()); // Display the HTML web page break; // Break out of the while loop } else { // if you got a newline, then clear currentLine currentLine = ""; } } else if (c != '\r') { // if you got anything else but a carriage return character, currentLine += c; // add it to the end of the currentLine } } } // Clear the header variable header = ""; // Close the connection client.stop(); Serial.println("Client disconnected."); Serial.println(""); } }