Connecting arduino mega to computer via ethernet module

I want to connect and test the Arduino Mega to the PC via the ENC28J60 Ethernet module, but I need to do this without connecting to any router or network. That means the module's Ethernet port will be connected directly to the PC via an Ethernet cable. However, when I run the required code, I can't seem to open the IP address in the code in a browser. I would be very happy if someone knowledgeable could help. The code I used is attached:

#include <UIPEthernet.h> // ENC28J60 modülü için kütüphane
EthernetServer server(80); // HTTP sunucusu için port numarası

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

  uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC adresi
  IPAddress ip(192, 168, 1, 177); // ENC28J60 modülünün IP adresi
  
  Ethernet.begin(mac, ip); // Ethernet bağlantısını başlat
  server.begin(); // Sunucuyu başlat
  
  // Arduino'nun IP adresini seri monitöre yazdır
  Serial.print("Sunucu IP adresi: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  EthernetClient client = server.available(); // Müşteri bağlantısını kontrol et
  if (client) {
    Serial.println("Yeni müşteri!");
    // Müşteriden gelen isteği oku (bu örnekte, istek göz ardı edilir)
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // HTTP isteğinin sonunu bul
        if (c == '\n' && currentLineIsBlank) {
          // HTTP header'ı gönder
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println();
          // Web sayfasını gönder
          client.println("<!DOCTYPE html>");
          client.println("<html>");
          client.println("<head><title>ENC28J60 Test Sayfasi</title></head>");
          client.println("<body><h1>Bu bir test sayfasidir</h1>");
          client.println("<p>ENC28J60 Ethernet modülü üzerinden bağlantı başarılı.</p>");
          client.println("</body></html>");
          break;
        }
        if (c == '\n') {
          // Yeni bir satır başlangıcı
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // Satırda karakter var
          currentLineIsBlank = false;
        }
      }
    }
    // İstemci bağlantısını kapat
    client.stop();
    Serial.println("Müşteri bağlantısı kesildi.");
  }
}

You may need to use a crossover cable when not connected to a switch or router.

1 Like

I moved your topic to an appropriate forum category @bugrahan_k.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Thank you for your response. I will try your suggestion as soon as possible.

Have you given the ethernet adapter on the PC a static IP address in the same subnet as the one on ethernet module?

1 Like

I assigned the PC an IP address in the same subnet mask as the Arduino's IP address.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.