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);
}
}