Arduino Ethernet shield questions:

I've just recently started experimenting with the official Arduino Ethernet shield. I have some questions and hope some experts can help me:

  1. The library reference says that the begin method needs to use the MAC address printed on the sticker, but older shields can use anything. So is the new shield somehow configured to only use the printed MAC address?

  2. I tried to use a different MAC on my shield and DHCP but DHCP fails to work. Is this because somehow the shield knows its own MAC address? But if yes, then why do I have to provide this MAC address anyway?

  3. Is there a way to programmatically extract the MAC address so it can be used in begin without human intervention? I'm trying to make a batch of units and don't want to compile the code a dozen different times with each address. I want to update the hex code in the field where my units will be and the field operators don't necessarily have the knowledge to compile code, although they could use XLoader to load HEX. Wifi shield can report its own MAC address so why not Ethernet shield?

  4. The begin method apparently returns a value so why is it not listed in the reference (even the most current doesn't have it but sample code uses Ethernet.begin(mac)==0 condition. Why hiding this return value from reference?

Thank you!

You can use the standard “DEAD BEEF FEED” mac address. It should work as long as you do not have a duplicate on your localnet.

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

The most common reason for the dhcp fail is a SD card in the shield’s slot without disabling it.

The return value is in the reference.

The DHCP version of this function, Ethernet.begin(mac), returns an int: 1 on a successful DHCP connection, 0 on failure. The other versions don’t return anything.

http://arduino.cc/en/Reference/EthernetBegin

Tim,

Thanks. I never have to disable wifi shield's sd card when beginning wifi. How do you disable it, by pulling the SS pin high? I thought the SdFat library does that automatically when a library call is done. BTW, my test code doesn't have SdFat code but I do have an SD card in the slot. Is that the source of the problem?

but I do have an SD card in the slot. Is that the source of the problem?

Could be. Take the SD card out, eat some salted peanuts, and then see if the code works.

zoomkat:

but I do have an SD card in the slot. Is that the source of the problem?

Could be. Take the SD card out, eat some salted peanuts, and then see if the code works.

Too late tonight. I will try tomorrow with salted pistachios.

Here is the code so you can leave the SD card in. No salted peanuts required. I prefer sprinkling a little magic pixie dust on it, but zoomkat already knows that. :slight_smile:

void setup() {
   pinMode(4,OUTPUT);
   digitalWrite(4,HIGH);

   // rest of your setup code

OK so one more question:

How do I know that Ethernet shield has connection to DHCP server? I know I can call begin if I start the sketch, what about while running? Do I just call begin another time to check? What if there’s cached data on the shield when I call begin? So many questions? Thanks.

After getting an IP, you must renew the lease on a schedule specified by the dhcp server. I use this in the loop function. You can call it every iteration of loop if you wish. Ethernet.maintain() returns 0 if no action was taken, so nothing is printed for a zero return value.

     byte rtnVal = Ethernet.maintain();
      switch(rtnVal) {
        case 1: Serial.println(F("\r\nDHCP renew fail"));        
                break;
        case 2: Serial.println(F("\r\nDHCP renew ok"));        
                break;
        case 3: Serial.println(F("\r\nDHCP rebind fail"));        
                break;
        case 4: Serial.println(F("\r\nDHCP rebind ok"));        
                break;
      }

Is this what you mean?

Tim,

I'll add it to my code. I just hate to migrate to a different IDE version in the middle of a project. Too much uncertainty. I'm using 1.0. I've tested my setup for 2.5 days without the maintain() since 1.0 IDE doesn't have it. The IP address didn't change. Does the shield renew lease automatically when it connects to remote server or does it have use maintain()? The Ethernet shield existed before Arduino 1.0 so what were people doing back then, maintaining a connection for weeks, months?

2.5 days would be just short of my router's lease of 3 days. I am not aware of any automatic feature that renews the lease in the library, and there appears to be no renew feature in the firmware like the wifi shield. I'm not recommending using the wifi shield. It has its own problems with the firmware.

Normally on a ip lease renewal, the ip does not change. It will be issued the same ip as long as the lease has not expired yet.

My router models check for the existence of an ip address before issuing it to any device by pinging the ip it is about to issue, and if it gets a response, it won't issue that ip and gets another available ip from the pool.

I don't use any IDE version before v1.0.2 due to an ethernet library bug that affects my Mega 2560.

Most users in the past (prior to v1.0) were using a static ip assignment outside the range of the dhcp server ip pool.

Tim,

Thanks for the info. Can you give me a brief of the bugs that affect your MEGA 2560 when you use IDE 1.0.2 or earlier? I am using MEGA 2560 and Arduino 1.0

I am not to excited to upgrade since they always have something wrong with their "latest" versions.

Here is my report. It shows as fixed now.
https://code.google.com/p/arduino/issues/detail?id=605
It affects the "while(client.available())" return value. It would return an incorrect value, causing an endless loop of garbage from client.read().

Search the forum for "605 bug" to see all the posts about this bug.

Tim,

Thank you for pointing this out. I made the change, not the IDE upgrade :wink:

So I just looked at my server replies: 248 bytes. So it always fits the 1 byte and the problem may stay dormant for a while before spewing garbage to me, maybe?