ENC28J60 + Arduino = FAIL!!!

Hey all,

I have been searching for the last 3 days for a way to get my ENC28J60 3.3V 25MHz Crystal Ethernet Network Board Module to link up with my Arduino Mega1280...i have tried the 10 - 13 pins and the 50 - 53 pins...i have tried the ethernet, ethershield, ethercard and enc28j60 libraries with no success...most of them fail on DHCP...What have i done wrong...here is a link to the module i have

One possibility might be to change the SPI definitions in enc28j60.c to fit your unique wiring. I would also suggest reinstalling the libraries and/or switch to IDE 1.0 . My enc28j60 doesn't seem to play well with IDE 1.0.1 or rather the libraries don't. I spent 3 days uploading and quadruple checking connections before I decided to start fresh with the older version that I know worked with it a few months back. works for me now...

Which library did u end up using???

trollmaker.com/article11/arduino-1-0-with-enc28j60-ethernet-shield-v1-1
I installed both libraries and it seems like only the examples from ETHER_28J60 library seem to work... :~

I got it to work BUT...now i am having a code issue while modifying the light sketch for it...here is what i have:

// A simple web server that turn an LED on or off"

#include "etherShield.h"
#include "ETHER_28J60.h"


int outputPin = 6;
int LED[10] = {13,12,10,9,8,7,6,5,4,3};
static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};   // this just needs to be unique for your network, 
                                                           
static uint8_t ip[4] = {192, 168, 0, 15}; // IP address for the webserver

static uint16_t port = 80; // Use port 80 - the standard for HTTP

ETHER_28J60 e;

void setup()
{ 
  Serial.begin(9600);
  Serial.println("Start Setup");
  e.setup(mac, ip, port);
  pinMode(outputPin, OUTPUT);
  Serial.println("END Setup");
 
}

void loop()
{
  char* params;
  if (params = e.serviceRequest())
  {
    Serial.println("Request");
    e.print("<h1>Arduino Web Remote</h1>\n");
    for(int x = 0; x < 10; x++)
    {
//Here is where the issues start
      if (strcmp(params, "?led=" + x) == 0)
      {
        Serial.print("LED HIT!!!");
        Serial.println(x);
        if(digitalRead(LED[x]) == HIGH)
          digitalWrite(outputPin, LOW);
        else
          digitalWrite(outputPin, HIGH);
      }
//Here is where the issues end

      if(digitalRead(LED[x]) == HIGH)
      {
        e.print("<a href='?");
        e.print(x);
        e.print("'><button style='border: 1px solid #ff0000; border-left: 10px solid #ff0000' type='button'>LED");
        e.print(x);
        e.print(" IS ON</button></a>\n");
      }
      else
      {
        e.print("<a href='?");
        e.print(x);
        e.print("'><button style='border: 1px solid #000; border-left: 10px solid #000' type='button'>LED");
        e.print(x);
        e.print(" IS OFF</button></a>\n");
      }
    }
    e.respond();
  }
}

now i am having a code issue while modifying the light sketch for it

Posting the code is only half of what you need to do. The other half is to tell us what the problem is.

sorry bout that...posted it on the way out the door

it is supposed to display a page with 10 buttons for turning on and off LEDs...it displays everything correctly...it just isn't reading the params right

Showing us what appears in the address bar of the browser after the submit button is pressed, and what the serial output of the Arduino is would be the next step. Now, all we know is that you have a problem and what the problem is, but still no details. Good thing I'm patient. Sometimes. (A little foot tapping sound in the background).

i think the issue is in the if statement...when it iterate...ex:// x = 3...and the param to the page is ?3 and that is exactly what it needs, then it should run thru the if...but it doesn't

i think the issue is in the if statement.

I can't recall a single example of an if statement failing to be executed the way it was written. What was executed may not have been what the programmer expected, but it wasn't a failure in the way the if statement was evaluated.

If you don't want to provide evidence to back up your assertions, all we can do is offer sympathy.

What is this line intended to do?

      if (strcmp(params, "?led=" + x) == 0)

Whatever it is, I doubt it is doing it.

I think Nick is on to something. for my web remote I used a series of if. . .else if statements for each button. so when you do string compare on params to check for an exact match like this:

 if (strcmp(params, "?led=" + x) == 0)

you might try doing something like this:

 if (params = e.serviceRequest())
  {
       
    e.print("<h1><a href='/?relay1=off'>Arduino Web Remote</a></h1>");                     // **take note of: <a href='/?relay1=off'> that is the important bit that the arduino looks at and decides what do **
    e.print("<button type='button' onclick=location.href='?relay1=off'>RELAY1</button>");              // a button for relay1, notice: href='?relay1=off'
    e.print("<button type='button' onclick=location.href='?fan=off'>FAN</button>");                 // copy for more buttons to control more pins...
    ...
    ...
    ...
    if (strcmp(params, "?relay1=on") == 0)             // this statement looks at what comes directly after this bit from above:  <a href='/... then you setup else if's to control pins
    {
     digitalWrite(relay1, HIGH);              // set your relay high
      
      e.print("<a href='?relay1=off'><button style='border: 1px solid #ff0000; border-left: 10px solid #ff0000' type='button'>relay1 IS ON</button></a>");
        e.print("
");
    }
    else if (strcmp(params, "?relay1=off") == 0)
    {
      digitalWrite(relay1, LOW);
      e.print("<a href='?relay1=on'><button style='border: 1px solid #000; border-left: 10px solid #000' type='button'>relay1 IS OFF</button></a>");
    }
    else if (strcmp(params, "?fan=on") == 0)
    {
      digitalWrite(fanPin, HIGH);
      e.print("<a href='?fan=off'><button style='border: 1px solid #ff0000; border-left: 10px solid #ff0000' type='button'>FAN IS ON</button></a>");
    }
    else if (strcmp(params, "?fan=off") == 0)
    {
      digitalWrite(fanPin, LOW);
      e.print("<a href='?fan=on'><button style='border: 1px solid #000; border-left: 10px solid #000' type='button'>FAN IS OFF</button></a>");
    }
    
    /* copy and paste else if's (2 for each relay- one to turn it on, the other 'else if' turns it off) so for 5 relays that should be one if statement followed 
    by 9 else if's */
    e.respond();
  }

if you have a way to do the same thing with a for loop, I would like to see it for my own learning

That is what I was trying...it just freaks out at the "+x" part...but I was thinking...make q function to return a char...something like

void loop()
{
  for(into x = 0; x < 10; x+)
  {
    if(strcmp(params, getpar(x)) == true)
    {
      //do stuff
    }
  }
}

char getpar(int num)
{
  switch (num)
  {
    case 0:
      return "?0";
      break;
  }
  return "";
}

Just an idea...wont be able to test it til tomorrow

char getpar(int num)
{
  switch (num)
  {
    case 0:
      return "?0";

"?0" is a string, consisting of three chars - '?', '0', and NULL - not a single char.

Perhaps a (re)view of the sprintf() function is in your future. Or, look at the strncmp() function. If you know that the first n characters match, it is trivial to extract the n+1th character and convert it to a number (subtract '0' from the character).

i was coding that from my phone last night...so it wasn't 100% correct...but i just tested it out...this version works...YAYAYAYAY

#include "etherShield.h"
#include "ETHER_28J60.h"
int outputPin = 6;
int LED[8] = {13,12,10,9,8,7,6,5};
static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};   // this just needs to be unique for your network, 
static uint8_t ip[4] = {192, 168, 0, 15}; // IP address for the webserver
static uint16_t port = 80; // Use port 80 - the standard for HTTP
ETHER_28J60 e;
void setup()
{ 
  Serial.begin(9600);
  Serial.println("Start Setup");
  e.setup(mac, ip, port);
  pinMode(outputPin, OUTPUT);
  for(int x=0;x<8;x++)
  {
    pinMode(LED[x],OUTPUT);
    digitalWrite(LED[x],HIGH);
    delay(200);
  }
  Serial.println("END Setup");
 
}

void loop()
{
  char* params;
  if (params = e.serviceRequest())
  {
    Serial.println("Request");
    e.print("<h1>Arduino Web Remote</h1>\n");
    for(int x = 0; x < 10; x++)
    {
      if (strcmp(params, getpar(x)) == 0)
      {
        if(digitalRead(LED[x]) == HIGH)
          digitalWrite(LED[x], LOW);
        else
          digitalWrite(LED[x], HIGH);
      }
      if(digitalRead(LED[x]) == HIGH)
      {
        e.print("<a href='?");
        e.print(x);
        e.print("'><button style='border: 1px solid #ff0000; border-left: 10px solid #ff0000' type='button'>LED");
        e.print(x);
        e.print(" IS ON</button></a>\n");
      }
      else
      {
        e.print("<a href='?");
        e.print(x);
        e.print("'><button style='border: 1px solid #000; border-left: 10px solid #000' type='button'>LED");
        e.print(x);
        e.print(" IS OFF</button></a>\n");
      }
    }
    e.respond();
  }
}

char* getpar(int num)
{
  switch (num)
  {
    case 0:
      return "?0";
      break;
    case 1:
      return "?1";
      break;
    case 2:
      return "?2";
      break;
    case 3:
      return "?3";
      break;
    case 4:
      return "?4";
      break;
    case 5:
      return "?5";
      break;
    case 6:
      return "?6";
      break;
    case 7:
      return "?7";
      break;
    case 8:
      return "?8";
      break;
    case 9:
      return "?9";
      break;
  }
  return "";
}

I'm glad that it now works. You do have a lot more code than you need, though.

char* getpar(int num)
{
  switch (num)
  {
    case 0:
      return "?0";
      break;
    case 1:
      return "?1";
      break;
    case 2:
      return "?2";
      break;
    case 3:
      return "?3";
      break;
    case 4:
      return "?4";
      break;
    case 5:
      return "?5";
      break;
    case 6:
      return "?6";
      break;
    case 7:
      return "?7";
      break;
    case 8:
      return "?8";
      break;
    case 9:
      return "?9";
      break;
  }
  return "";
}

This code, and the call to it could be replaced with:

char buff[8];
sprintf(buff, "?%d", num);

Then, strcmp(params, buff) will evaluate to -1, 0 or 1, depending on the value of num and what's in params.

Hi, I tried your code and it doesn't work for me - I think it even crashes the arduino.
I think you are using a mega? I tried it on a uno, I changed the output pins, as the enc module is on pins 10 to 13.

The startup goes ok - but as soon as I try to access the server, things get all weird. Lots of garbage on the serial port, and the page never completes loading. What url do you use to access it?

What url do you use to access it?

The one that you assigned to it:

static uint8_t ip[4] = {192, 168, 0, 15}; // IP address for the webserver

What numbers did YOU use?

I try to go to the url - but I figured I needed parameters to get this working. Some examples I found needed parameters to work.
I'm new to ethernet, just trying out different librarys and programs, seeing what will work best for me. So far this library has been the only one that did anything at all.

When I open the serial window, I get the start setup-end setup messages, but when I try to access the webserver things go all weird.

SStart Setup
END Setup
ReqWÉсSetup
END Setup
Req
WÉсSetup
END Setup
Re¹WÉсSetup
END kE?ÉсSetup
ENDkE?ÉсSetup
ENDkE?ÉсSetup
END Setup
Re¹
WÉсSetup

I included my modified code:

#include "etherShield.h"
#include "ETHER_28J60.h"
int LED[8] = {2,3,4,5,6,7,8,9};
static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};   // this just needs to be unique for your network, 
static uint8_t ip[4] = {192, 168, 1, 15}; // IP address for the webserver
static uint16_t port = 80; // Use port 80 - the standard for HTTP
ETHER_28J60 e;

void setup()
{ 
                                                                        Serial.begin(9600);
                                                                        Serial.println("Start Setup");
  e.setup(mac, ip, port);
  for(int x=0;x<8;x++)
  {
    pinMode(LED[x],OUTPUT);
    digitalWrite(LED[x],LOW);
    delay(200);
  }
                                                                        Serial.println("END Setup");
 
}


void loop()
{
  char* params;
  char buff[8];
  if (params = e.serviceRequest())
  {
                                                                        Serial.println("Request");
    e.print("<h1>Arduino Web Remote</h1>\n");
    for(int x = 0; x < 10; x++)
    {
      sprintf(buff, "?%d", x);
                                                                        Serial.println(buff);
      if (strcmp(params, buff) == 0)
      {
        if(digitalRead(LED[x]) == HIGH)
          digitalWrite(LED[x], LOW);
        else
          digitalWrite(LED[x], HIGH);
      }
      if(digitalRead(LED[x]) == HIGH)
      {
        e.print("<a href='?");
        e.print(x);
        e.print("'><button style='border: 1px solid #ff0000; border-left: 10px solid #ff0000' type='button'>LED");
        e.print(x);
        e.print(" IS ON</button></a>\n");
      }
      else
      {
        e.print("<a href='?");
        e.print(x);
        e.print("'><button style='border: 1px solid #000; border-left: 10px solid #000' type='button'>LED");
        e.print(x);
        e.print(" IS OFF</button></a>\n");
      }
    }
    e.respond();
  }
}

When I open the serial window, I get the start setup-end setup messages, but when I try to access the webserver things go all weird.

The code you posted doesn't have start setup or end setup messages, so where are those coming from?