ESP webserver with w5500 module

I need a help with fixing this code
here i connect esp32 with w5500 module
and also connected the esp32 with 3 leds
so i when i uploaded the code

this is the output on the serial monitor but when i ping on 192.168.1.28 it does not reply
14:10:34.379 -> Begin Ethernet

14:10:39.931 -> Local IP: 192.168.1.28

14:10:39.931 -> Subnet Mask: 255.255.255.0

14:10:39.931 -> Gateway IP: 192.168.1.1

14:10:39.931 -> DNS Server: 192.168.1.1

14:10:39.931 -> Ethernet Successfully Initialized

here is the code

#include <SPI.h>
#include <Ethernet.h>

// Set web server port number to 80
EthernetServer server(80);

// Variable to store the HTTP request
String header;

// Auxiliary variables to store the current output state
String output25State = "off";
String output26State = "off";
String output27State = "off";

// Assign output variables to GPIO pins
const int output25 = 25;
const int output26 = 26;
const int output27 = 27;

// Enter a MAC address for your controller below.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Set the static IP address to use if DHCP fails to assign
#define MYIPADDR 192, 168, 1, 28
#define MYIPMASK 255, 255, 255, 0
#define MYDNS 192, 168, 1, 1
#define MYGW 192, 168, 1, 1

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;

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

  // Initialize the output variables as outputs
  pinMode(output25, OUTPUT);
  pinMode(output26, OUTPUT);
  pinMode(output27, OUTPUT);

  // Set outputs to LOW
  digitalWrite(output25, LOW);
  digitalWrite(output26, LOW);
  digitalWrite(output27, LOW);

  delay(1000);
  Serial.println("Begin Ethernet");

  //Ethernet.init(10);  // Most Arduino shields
  Ethernet.init(5);  // MKR ETH Shield
                     //Ethernet.init(0);   // Teensy 2.0
                     //Ethernet.init(20);  // Teensy++ 2.0
                     //Ethernet.init(15);  // ESP8266 with Adafruit FeatherWing Ethernet
                     //Ethernet.init(33);  // ESP32 with Adafruit FeatherWing Ethernet

  // Initialize Ethernet shield with a static IP address
  IPAddress ip(MYIPADDR);
  IPAddress dns(MYDNS);
  IPAddress gw(MYGW);
  IPAddress sn(MYIPMASK);
  Ethernet.begin(mac, ip, dns, gw, sn);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found. Sorry, can't run without hardware.");
    while (true) {
      delay(1);  // Do nothing, no point running without Ethernet hardware
    }
  }

  // Check Ethernet link status
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

  delay(5000);

  // Print Ethernet connection details
  Serial.print("Local IP: ");
  Serial.println(Ethernet.localIP());
  Serial.print("Subnet Mask: ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("Gateway IP: ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("DNS Server: ");
  Serial.println(Ethernet.dnsServerIP());
  Serial.println("Ethernet Successfully Initialized");

  server.begin();
}

void loop() {
  EthernetClient client = server.available();  // Listen for incoming clients

  if (client) {  // If a new client connects,
    Serial.println("New Client.");
    String currentLine = "";  // Make a String to hold incoming data from the client
    unsigned long previousTime = millis();
    bool timeout = false;

    while (client.connected() && !timeout) {  // Loop while the client is connected
      if (client.available()) {               // If there's bytes to read from the client
        char c = client.read();               // Read a byte
        Serial.write(c);                      // Print it out the serial monitor
        header += c;
        if (c == '\n') {  // If the byte is a newline character
          if (currentLine.length() == 0) {
            // Send HTTP response
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();

            // Control the GPIOs
            if (header.indexOf("GET /25/on") >= 0) {
              Serial.println("GPIO 25 on");
              output25State = "on";
              digitalWrite(output25, HIGH);
            } else if (header.indexOf("GET /25/off") >= 0) {
              Serial.println("GPIO 25 off");
              output25State = "off";
              digitalWrite(output25, LOW);
            } else if (header.indexOf("GET /26/on") >= 0) {
              Serial.println("GPIO 26 on");
              output26State = "on";
              digitalWrite(output26, HIGH);
            } else if (header.indexOf("GET /26/off") >= 0) {
              Serial.println("GPIO 26 off");
              output26State = "off";
              digitalWrite(output26, LOW);
            } else if (header.indexOf("GET /27/on") >= 0) {
              Serial.println("GPIO 27 on");
              output27State = "on";
              digitalWrite(output27, HIGH);
            } else if (header.indexOf("GET /27/off") >= 0) {
              Serial.println("GPIO 27 off");
              output27State = "off";
              digitalWrite(output27, LOW);
            }

            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
            client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
            client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
            client.println(".button2 {background-color: #555555;}</style></head>");
            client.println("<body><h1>ESP32 Web Server</h1>");

            // GPIO 25
            client.println("<p>GPIO 25 - State " + output25State + "</p>");
            client.println(output25State == "off" ? "<p><a href=\"/25/on\"><button class=\"button\">ON</button></a></p>" : "<p><a href=\"/25/off\"><button class=\"button button2\">OFF</button></a></p>");

            // GPIO 26
            client.println("<p>GPIO 26 - State " + output26State + "</p>");
            client.println(output26State == "off" ? "<p><a href=\"/26/on\"><button class=\"button\">ON</button></a></p>" : "<p><a href=\"/26/off\"><button class=\"button button2\">OFF</button></a></p>");

            // GPIO 27
            client.println("<p>GPIO 27 - State " + output27State + "</p>");
            client.println(output27State == "off" ? "<p><a href=\"/27/on\"><button class=\"button\">ON</button></a></p>" : "<p><a href=\"/27/off\"><button class=\"button button2\">OFF</button></a></p>");

            client.println("</body></html>");

            // The HTTP response ends with another blank line
            client.println();
            break;
          } else {
            currentLine = "";
          }
        } else if (c != '\r') {
          currentLine += c;
        }
      }

      // Check for timeout
      if (millis() - previousTime > 2000) {
        timeout = true;
      }
    }

    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }
}

Welcome to the forum

You started a topic in the Uncategorised category of the forum when its description explicitly tells you not to

Your topic has been moved to a relevant category. Please be careful in future when deciding where to start new topics

1 Like

this is not how you do a WebServer on a ESP32

see this example on how to do it