[SOLVED] ENC28J60 with Arduino Mega

Hello everyone,

I'm currently using the ENC28J60 with Arduino Mega as a test. I'm trying to use its libraries on Github, like the UIPEthernet.

To test it, I decided to run a WebServer, code found in UIPEthernet_webserver_FUNCIONAL by gdarkog, and connect it with my Internet modem with the Ethernet Cable in a LAN port. But it doesn't look like it is working... At least nothing shows up when I access the IP 192.168.1.199.

Arduino Serial Monitor:

14:10:57.273 -> ENC28J60::init DEBUG:csPin = 53
14:10:57.306 -> ENC28J60::init DEBUG:miso = 50
14:10:57.340 -> ENC28J60::init DEBUG:mosi = 51
14:10:57.376 -> ENC28J60::init DEBUG:sck = 52
14:10:57.414 -> ENC28J60::init DEBUG:Use SPI lib SPI.begin()
14:10:57.450 -> ENC28J60::init INFO: Chip erevid=6 initialization completed.
14:10:57.521 -> IP Address: 192.168.1.199

And every 2-3 minutes i get something like this.

14:12:48.222 -> Enc28J60Network::receivePacket(void) DEBUG:receivePacket [6-42], next: 46, stat: C0, Packet count: 1 -> OK
14:12:48.222 -> Enc28J60Network::receivePacket(void) DEBUG: rxstat OK. receivePkt.size=60
14:12:48.222 -> UIPEthernetClass::tick() DEBUG:receivePacket: 255
14:12:48.222 -> Enc28J60Network::readPacket(memhandle handle, memaddress position, uint8_t* buffer, uint16_t len) DEBUG_V2: Read bytes:60 save to block(255) [0]: **HEX BLOCK HERE**
14:12:48.222 -> UIPEthernetClass::tick() DEBUG:readPacket type IP, uip_len: 60
14:12:48.222 -> UIPEthernetClass::tick() DEBUG:freeing packet: 255
14:12:48.222 -> Enc28J60Network::setERXRDPT(void) DEBUG:Set actnextPacketPtr:45

#include <UIPEthernet.h> // Used for Ethernet

// **** ETHERNET SETTING ****
byte mac[] = { 0x54, 0x34, 0xaa, 0x36, 0xcc, 0x31 };                                      
IPAddress ip(192, 168, 1, 199);                        
EthernetServer server(80);

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

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();

  Serial.print("IP Address: ");
  Serial.println(Ethernet.localIP());
}

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

  if (client)
  {  
    Serial.println("-> New Connection");

    // an http request ends with a blank line
    boolean currentLineIsBlank = true;

    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();

        // 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 == '.' && currentLineIsBlank)
        {
          client.println("<html><title>Hello World!</title><body>hOLIS!..</body>");
          break;
        }

        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        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(1000);

    // close the connection:
    client.stop();
    Serial.println("   Disconnected\n");
  }
}

try

if (!Ethernet.begin(mac)) {
   Serial.println("Failed to configure Ethernet using DHCP");
   while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
   }
 }

instead of
Ethernet.begin(mac, ip);

Juraj:
try

if (!Ethernet.begin(mac)) {

Serial.println("Failed to configure Ethernet using DHCP");
  while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
  }
}



instead of
Ethernet.begin(mac, ip);

Hello Juraj, Thank you for your reply.
I've tried your code and it didn't print anything, meaning that it didn't fail.

One point that I found yesterday is that my Router only allows IPs in the range 102.168.25.2 to 192.168.25.200, so I was using the wrong address.

However, I'm facing another problem. I did manage to see the Serial Monitor print that the client connected when I write the IP in the browser, but I don't receive the Html response and I wished.

Serial Monitor:

08:28:39.084 -> MemoryPool::freeBlock(memhandle handle) WARNING: Don't free NOBLOCK handle
08:28:39.155 -> MemoryPool::freeBlock(memhandle handle) WARNING: Don't free NOBLOCK handle
08:28:39.229 -> MemoryPool::freeBlock(memhandle handle) WARNING: Don't free NOBLOCK handle
08:28:39.332 -> MemoryPool::freeBlock(memhandle handle) WARNING: Don't free NOBLOCK handle
08:28:39.399 -> MemoryPool::freeBlock(memhandle handle) WARNING: Don't free NOBLOCK handle
08:28:39.471 -> Client NENC28J60::init INFO: Chip erevid=6 initialization completed.
08:28:42.865 -> MemoryPool::freeBlock(memhandle handle) WARNING: Don't free NOBLOCK handle
08:28:43.107 -> MemoryPool::freeBlock(memhandle handle) WARNING: Don't free NOBLOCK handle
08:28:43.175 -> MemoryPool::freeBlock(memhandle handle) WARNING: Don't free NOBLOCK handle
08:28:43.243 -> MemoryPool::freeBlock(memhandle handle) WARNING: Don't free NOBLOCK handle
08:28:43.315 -> MemoryPool::freeBlock(memhandle handle) WARNING: Don't free NOBLOCK handle
08:28:43.414 -> IP Address: 192.168.25.9
08:28:45.884 -> -> New Connection - 1
08:28:45.918 -> GET / HTTP/1.1
08:28:45.918 -> Host: 192.168.25.9
08:28:45.951 -> Upgrade-Insecure-Requests: 1
08:28:45.985 -> Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
08:28:46.054 -> User-Agent: Mozilla/5.0 [... other browser information here ...]
08:28:46.189 -> Accept-Language: en-us
08:28:46.223 -> Accept-Encoding: gzip, deflate
08:28:46.256 -> Connection: keep-alive
08:28:46.290 -> 
08:28:46.290 -> Client Not Available
08:28:46.290 -> Client Not Available

Full code below.

#include <UIPEthernet.h> // Used for Ethernet

// **** ETHERNET SETTING ****
byte mac[] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};                                      
IPAddress ip(192, 168, 25, 9);                        
EthernetServer server(80);

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

  // start the Ethernet connection and the server:
  if (!Ethernet.begin(mac)) {
    Serial.println("Failed to configure Ethernet using DHCP");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  server.begin();

  Serial.print("IP Address: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client)
  {  
    Serial.print("-> New Connection - ");
    Serial.println(client);

    // an http request ends with a blank line
    boolean currentLineIsBlank = true;

    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        Serial.write(c);

        // 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 == '.' && currentLineIsBlank)
        {
          client.println("<html><title>Hello World!</title><body>hOLIS!..</body>");
          Serial.write("Message sent to browser");
          break;
        }

        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r')
        {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }else{
        Serial.println("Client Not Available");
      }
    }

    // give the web browser time to receive the data
    delay(1000);

    // close the connection:
    client.stop();
    Serial.println("   Disconnected\n");
  }
}

I've tried with Postman but didn't work either.

08:33:31.377 -> -> New Connection - 1
08:33:31.413 -> GET / HTTP/1.1
08:33:31.413 -> User-Agent: PostmanRuntime/7.20.1
08:33:31.451 -> Accept: */*
08:33:31.485 -> Cache-Control: no-cache
08:33:31.485 -> Postman-Token: [...token here...]
08:33:31.556 -> Host: 192.168.25.9
08:33:31.556 -> Accept-Encoding: gzip, deflate
08:33:31.590 -> Connection: keep-alive
08:33:31.626 -> 
08:33:31.626 -> Client Not Available
[...Same message over and over again...]
08:33:32.776 -> Client Not Available
08:33:32.813 -> MemoryPool::freeBlock(memhandle handle) WARNING: Don't free NOBLOCK handle
08:33:32.885 -> MemoryPool::freeBlock(memhandle handle) WARNING: Don't free NOBLOCK handle
08:33:32.953 -> MemoryPool::freeBlock(memhandle handle) WARNING: Don't free NOBLOCK handle
08:33:33.026 -> MemoryPool::freeBlock(memhandle handle) WARNING: Don't free NOBLOCK handle
08:33:33.098 -> MemoryPool::freeBlock(memhandle handle) WARNING: Don't free NOBLOCK handle
08:33:33.202 -> Client Not Available
08:33:34.159 ->    Disconnected

what is if (c == '.' && currentLineIsBlank)?
it should be
if (c == '\n' && currentLineIsBlank)

Thank you, it did work. I don't know why UIPEthernet_webserver_FUNCIONAL by gdarkog used it that way, but I should have tried with "\n".

Final code.

#include <UIPEthernet.h> // Used for Ethernet

// **** ETHERNET SETTING ****
byte mac[] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};                                      
IPAddress ip(192, 168, 25, 199);                        
EthernetServer server(80);

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

  // start the Ethernet connection and the server:
  if (!Ethernet.begin(mac)) {
    Serial.println("Failed to configure Ethernet using DHCP");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  Ethernet.init(4);
  server.begin();

  Serial.print("IP Address: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client)
  {  
    Serial.print("-> New Connection - ");
    Serial.println(client);

    // an http request ends with a blank line
    boolean currentLineIsBlank = true;

    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        Serial.write(c);

        // 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("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<h1>Hello World</h1><p>It did work!!</p>");
          client.println("</html>");
          Serial.write("Message sent to browser");
          break;
        }

        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r')
        {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }else{
        Serial.println("Client Not Available");
      }
    }

    // give the web browser time to receive the data
    delay(1000);

    // close the connection:
    client.stop();
    Serial.println("   Disconnected\n");
  }
}

here I have a SDWebServer example with more practical HTTP request response handling

and my projects contain a WebServer for simple monitoring over web page