#include // Replace with your network credentials const char* ssid = "TR-2"; const char* password = "AQW112"; WiFiServer server(80); const int led1 = 22; // the number of the LED pin const int led2 = 23; // the number of the LED pin // Set your Static IP address IPAddress local_IP(192, 168, 0, 109); // Set your Gateway IP address IPAddress gateway(192, 168, 0, 1); IPAddress subnet(255, 255, 255, 0); //IPAddress primaryDNS(7, 7, 7, 7); //optional //IPAddress secondaryDNS(7, 7, 2, 2); //optional // Client variables char linebuf[80]; int charcount = 0; void setup() { // initialize the LEDs pins as an output: pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); // Configures static IP address // if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) { // Serial.println("STA Failed to configure"); // } // Configures static IP address if (!WiFi.config(local_IP, gateway, subnet)) { Serial.println("STA Failed to configure"); } //Initialize serial and wait for port to open: Serial.begin(115200); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } // We start by connecting to a WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); // attempt to connect to Wifi network: while (WiFi.status() != WL_CONNECTED) { // Connect to WPA/WPA2 network. Change this line if using open or WEP network: delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); // Print ESP MAC Address Serial.println("MAC address: "); Serial.println(WiFi.macAddress()); server.begin(); } void loop() { // listen for incoming clients WiFiClient client = server.available(); if (client) { Serial.println("New client"); memset(linebuf, 0, sizeof(linebuf)); charcount = 0; // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); //read char by char HTTP request linebuf[charcount] = c; if (charcount < sizeof(linebuf) - 1) charcount++; // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply if (c == '\n' && currentLineIsBlank) { // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); // the connection will be closed after completion of the response client.println(); client.println(""); client.println(""); client.println("

ESP32 - Web Server

"); client.println("

LED #1  

"); client.println("

LED #2  

"); client.println(""); break; } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; if (strstr(linebuf, "GET /on1") > 0) { Serial.println("LED 1 ON"); digitalWrite(led1, HIGH); } else if (strstr(linebuf, "GET /off1") > 0) { Serial.println("LED 1 OFF"); digitalWrite(led1, LOW); } else if (strstr(linebuf, "GET /on2") > 0) { Serial.println("LED 2 ON"); digitalWrite(led2, HIGH); } else if (strstr(linebuf, "GET /off2") > 0) { Serial.println("LED 2 OFF"); digitalWrite(led2, LOW); } // you're starting a new line currentLineIsBlank = true; memset(linebuf, 0, sizeof(linebuf)); charcount = 0; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); Serial.println("client disconnected"); } }