BLOG · 13/4/2023
this post is consists of report of common task from 6 to 9
c\n // Load Wi-Fi library\n#include \n\n// Replace with your network credentials\nconst char* ssid = \vvvvvvxxxxxx\";\nconst char* password = vvvvvvxxxxxx\";\n\n// Set web server port number to 80\nWiFiServer server(80);\n\n// Variable to store the HTTP request\nString header;\n\n// Auxiliar variables to store the current output state\nString output26State = \"off\";\nString output27State = \"off\";\n\n// Assign output variables to GPIO pins\nconst int output26 = 26;\nconst int output27 = 27;\n\n// Current time\nunsigned long currentTime = millis();\n// Previous time\nunsigned long previousTime = 0; \n// Define timeout time in milliseconds (example: 2000ms = 2s)\nconst long timeoutTime = 2000;\n\nvoid setup() {\n Serial.begin(115200);\n // Initialize the output variables as outputs\n pinMode(output26, OUTPUT);\n pinMode(output27, OUTPUT);\n // Set outputs to LOW\n digitalWrite(output26, LOW);\n digitalWrite(output27, LOW);\n\n // Connect to Wi-Fi network with SSID and password\n Serial.print(\"Connecting to \");\n Serial.println(ssid);\n WiFi.begin(ssid, password);\n while (WiFi.status() != WL_CONNECTED) {\n delay(500);\n Serial.print(\".\");\n }\n // Print local IP address and start web server\n Serial.println(\"\");\n Serial.println(\"WiFi connected.\");\n Serial.println(\"IP address: \");\n Serial.println(WiFi.localIP());\n server.begin();\n}\n\nvoid loop(){\n WiFiClient client = server.available(); // Listen for incoming clients\n\n if (client) { // If a new client connect \n currentTime = millis();\n previousTime = currentTime;\n Serial.println(\"New Client.\"); // print a message out in the serial port\n String currentLine = \"\"; // make a String to hold incoming data from the client\n while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client\'s connected\n currentTime = millis();\n if (client.available()) { // if there\'s bytes to read from the clien \n char c = client.read(); // read a byte, then\n Serial.write(c); // print it out the serial monitor\n header += c;\n if (c == \'\\n\') { // if the byte is a newline character\n // if the current line is blank, you got two newline characters in a row.\n // that\'s the end of the client HTTP request, so send a response:\n if (currentLine.length() == 0) {\n // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)\n // and a content-type so the client knows what\'s coming, then a blank line:\n client.println(\"HTTP/1.1 200 OK\");\n client.println(\"Content-type:text/html\");\n client.println(\"Connection: close\");\n client.println();\n \n // turns the GPIOs on and off\n if (header.indexOf(\"GET /26/on\") >= 0) {\n Serial.println(\"GPIO 26 on\");\n output26State = \"on\";\n digitalWrite(output26, HIGH);\n } else if (header.indexOf(\"GET /26/off\") >= 0) {\n Serial.println(\"GPIO 26 off\");\n output26State = \"off\";\n digitalWrite(output26, LOW);\n } else if (header.indexOf(\"GET /27/on\") >= 0) {\n Serial.println(\"GPIO 27 on\");\n output27State = \"on\";\n digitalWrite(output27, HIGH);\n } else if (header.indexOf(\"GET /27/off\") >= 0) {\n Serial.println(\"GPIO 27 off\");\n output27State = \"off\";\n digitalWrite(output27, LOW);\n }\n \n // Display the HTML web page\n client.println(\"\");\n client.println(\"\");\n client.println(\"\");\n // CSS to style the on/off buttons \n // Feel free to change the background-color and font-size attributes to fit your preferences\n client.println(\"\");\n \n // Web Page Heading\n client.println(\"ESP32 Web Server\");\n \n // Display current state, and ON/OFF buttons for GPIO 26 \n client.println(\"GPIO 26 - State \" + output26State + \"\");\n // If the output26State is off, it displays the ON button \n if (output26State==\"off\") {\n client.println(\"ON\");\n } else {\n client.println(\"OFF\");\n } \n \n // Display current state, and ON/OFF buttons for GPIO 27 \n client.println(\"GPIO 27 - State \" + output27State + \"\");\n // If the output27State is off, it displays the ON button \n if (output27State==\"off\") {\n client.println(\"ON\");\n } else {\n client.println(\"OFF\");\n }\n client.println(\"\");\n \n // The HTTP response ends with another blank line\n client.println();\n // Break out of the while loop\n break;\n } else { // if you got a newline, then clear currentLine\n currentLine = \"\";\n }\n } else if (c != \'\\r\') { // if you got anything else but a carriage return characte \n currentLine += c; // add it to the end of the currentLine\n }\n }\n }\n // Clear the header variable\n header = \"\";\n // Close the connection\n client.stop();\n Serial.println(\"Client disconnected.\");\n Serial.println(\"\");\n }\n}\n
\n