EtherCard library + Arduino Mega2560 + ENC28J60 board

I am having some trouble getting an ENC28J60 board to work consistently.

I bought a two-pack on Ebay; the silk-screen on the back of the board reads “LC STUDIO” and “http://www.lcsoft.net”. I downloaded the EtherCard library from …github.com/jcw/ethercard and installed it in my Arduino “libraries” folder. I’m using an Arduino Mega2560 board, so I changed the #define statements for SPI to use pins 50-53. I started with the example sketch “getDHCPandDNS”.

When I power-on the Arduino (and ENC28J60), I can watch the Ethernet traffic on Wireshark. I see a successful DHCP negotiation and the sketch starts sending HTTP requests to my desktop computer’s (simple Python) webserver. Sometimes I’ll see multiple HTTP requests and responses, sometimes only one.

Problem: after the HTTP requests stop working, I can’t get the Arduino to communicate with the ENC28J60 until they’re powered-off for at least 2 minutes. If I use the Arduino reset button or power-off the Arduino briefly, the setup() routine in the sketch never gets a good response from the ether.begin() call.

Any help will be appreciated.

// This demo does web requests via DHCP and DNS lookup.
// 2011-07-05 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php

#include <EtherCard.h>

#define REQUEST_RATE 5000 // milliseconds

// ethernet interface mac address
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
// remote website name
char website[] PROGMEM = "192.168.57.63:8008";

// byte Ethernet::buffer[700];
byte Ethernet::buffer[2000];
static unsigned long timer;

// called when the client request is complete
static void my_result_cb (byte status, word off, word len) {
  Serial.print("<<< reply ");
  Serial.print(millis() - timer);
  Serial.println(" ms");
  Serial.println((const char*) Ethernet::buffer + off);
}

void dumpSPI(String msg) {
  Serial.println("\n"+msg); 
  
  Serial.print("SPCR=0x"); Serial.println(SPCR,HEX);
  Serial.print("SPSR=0x"); Serial.println(SPSR,HEX);
  // Serial.print("SPDR=0x"); Serial.println(SPDR,HEX);
  delay(2*1000);
}

void setup () {
  Serial.begin(57600);
  Serial.println("\n[getDHCPandDNS]");
  Serial.println(".wait 2 seconds and continue"); 
  delay(2*1000); 
  
  byte x; 
  
  // dumpSPI("before doBIST()"); 
  
  // x = ether.doBIST(53);
  // Serial.print("results of doBIST()=0x"); Serial.println(x,HEX);
  // Serial.println( ".wait 2 seconds and continue"); delay(2*1000); 

  while (true) {
    
    // x = ether.begin(sizeof Ethernet::buffer, mymac);
    // dumpSPI("before begin()"); 
    x = ether.begin(sizeof Ethernet::buffer, mymac, 53);
    if (x == 0) {

      Serial.println( "Failed to access Ethernet controller, wait 2 seconds and check again");
      delay(2*1000); 
      continue; 

    } else if (x >= 10) {

      Serial.println( "Invalid value from ether.begin(), wait 2 seconds and check again");
      delay(2*1000); 
      
      continue; 
    }

    Serial.print("breaking, ether.begin()=0x"); Serial.println(x,HEX);
    break; 

  }


  // dumpSPI("before dhcpSetup()"); 

  while (true) {
    Serial.println("calling dhcpSetup()"); 
    if (!ether.dhcpSetup()) {
      Serial.println("DHCP failed");
    } else 
      break;
  }

  ether.printIp("My IP: ", ether.myip);
  // ether.printIp("Netmask: ", ether.mymask);
  ether.printIp("GW IP: ", ether.gwip);
  ether.printIp("DNS IP: ", ether.dnsip);

  if (false) {
    if (!ether.dnsLookup(website))
      Serial.println("DNS failed");
    ether.printIp("Server: ", ether.hisip);
  } else { 
  
    ether.hisip[0] = 192; 
    ether.hisip[1] = 168; 
    ether.hisip[2] = 57; 
    ether.hisip[3] = 63;
    
    ether.hisport = 8008; 

    ether.printIp("Server (kludge): ", ether.hisip);
    
  }
  
  timer = - REQUEST_RATE; // start timing out right away
}

void loop () {
    
  ether.packetLoop(ether.packetReceive());
  
  if (millis() > timer + REQUEST_RATE) {
    timer = millis();
    Serial.println("\n>>> REQ");
    ether.browseUrl(PSTR("/offline.html"), " ", website, my_result_cb);
  }
}

Are you connect your local network pc successful?
I have a asp server 192.168.1.104
But is not response…

#include <EtherCard.h>

static byte mymac[] = {0xDD,0xDD,0xDD,0x00,0x00,0x01};
char website[] PROGMEM = "192.168.1.104:80";

byte Ethernet::buffer[700];
static uint32_t timer;

static void response_callback (byte status, word off, word len) {
  
  Serial.print((const char*) Ethernet::buffer + off );
} 
 
void setup () {
 
  Serial.begin(57600);
  Serial.println("Client Demo");
  Serial.println();
 
  if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
    Serial.println( "Failed to access Ethernet controller");
 else
   Serial.println("Ethernet controller initialized");
 Serial.println();
 
  if (!ether.dhcpSetup())
    Serial.println("Failed to get configuration from DHCP");
  else
    Serial.println("DHCP configuration done");
 
  ether.printIp("IP Address:\t", ether.myip);
  ether.printIp("Netmask:\t", ether.mymask);
  ether.printIp("Gateway:\t", ether.gwip);
  Serial.println();
  
  ether.hisip[0] = 192; 
  ether.hisip[1] = 168; 
  ether.hisip[2] = 1; 
  ether.hisip[3] = 104;
  ether.hisport = 80; 
  ether.printIp("SRV IP:\t", ether.hisip);
  Serial.println();
}
 
void loop() {
 
  ether.packetLoop(ether.packetReceive());
  
  if (millis() > timer) {
    timer = millis() + 5000;
    ether.browseUrl(PSTR("/"), "?tem=12&humi=65", website, response_callback);
  }
}

Can you give me some advice? Thank you so much…

What do you see in Arduino's "Serial Monitor" window ? Is a DHCP IP address being assigned to the Ethernet board ? I have my wireless router locked-down, so I had to add more addresses to the DHCP server before I could make progress.

Have you checked your PC's firewall log / settings ? Are you sure your web server is on port 80 ? Sorry if any of these questions seem stupid but ...

Like I said in my original post, using Wireshark (http://sourceforge.net/projects/wireshark/) to watch the Ethernet traffic was very useful for monitoring progress and errors.

FWIW, I added code to loop() that reads & prints a number of registers from the ethernet board every 1/2 second and flags errors. (E.g. wrong EREVID value, compares the MAC address to the value the Arduino initialized it with). After the initial power-up, the Ethernet board returns the correct values for a while (26 seconds, 47 seconds), returns a bad value or two, goes back to giving the correct values for a short while, then starts returning invalid values continuously.

I read the datasheet and noticed that the ENC28J60's MISO pin is not "5v tolerant".

I found posts elsewhere that the ENC28J60 chip might be overheating. It didn't feel particularly warm to my fingertip but I did what it suggested - put a quarter coin on the chip to act as a heatsink; after that, the Arduino ran my code for 12 continuous hours without error.