Newbie basic MAC ADDRESS question

Greetings Arduino community: I am just getting into arduino with a good fund of knowledge of circuitry, but only basic knowledge of TCPIP and most internet functionality.

I am trying to learn using an ETHERNET shield on my arduino.

I used this basic code ( created 12 April 2011
modified 9 Apr 20 by Tom Igoe):

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x03
};

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // this check is only needed on the Leonardo:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for (;;)
      ;
  }
  // print your local IP address:
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();
  Serial.println(Ethernet.localIP());
}

void loop() {

}

to show up the IP address given to my shield; I loaded it several times and the same IP address returns. Here are my questions:

  1. My ethernet shield did not have a MAC address on it so I just used whatever address came in this sketch; but if ALL devices are married to MAC address, how can this work? I understood that an IP address can be assigned, but a MAC address is hardwired; am I wrong?

  2. I am assuming that the IP address, when the EthernetClient is run, is dynamically allocated by my system; why is the same IP address always coming up?

Thank you so much for your help.

DHCP is usually configured to assign available consecutive addresses. Often this will be the same address, if there are very few clients.

1) You can use any legitimate mac address. The catch is no duplicates on the same localnet.

2) Yes. The dhcp server (normally your router) will assign the same IP to the same mac address until the dhcp lease expires. The lease will not expire if the ethernet shield either requests an IP in setup, or Ethernet.maintain() is called to renew the lease during the lease time. That lease time is normally 2 to 3 days, depending on the router.

gvlazzareschi: 1. My ethernet shield did not have a MAC address on it so I just used whatever address came in this sketch; but if ALL devices are married to MAC address, how can this work? I understood that an IP address can be assigned, but a MAC address is hardwired; am I wrong?

You are the manufacturer of the hardware, you assign the MAC address. Typically hardcoded in the sketch. The MAC address must be unique to your LAN network at home. Never use duplicate MAC address in your LAN at the same time! If you use your Arduino for hobby, just use some random address. If you want to sell hardware as a manufacturer, you'd have to follow the rules and have to use "official" MAC adresses from a range that is assigned to your company and must be unique (in that case: unique worldwide) to every device sold.

gvlazzareschi: 2. I am assuming that the IP address, when the EthernetClient is run, is dynamically allocated by my system; why is the same IP address always coming up?

That's what your router does: Most routers remember the MAC address of devices which ask for an IP number by using DHCP, and if the same device again asks for an IP number later on, the router will offer the same IP address to the same device. In that case: If you assign a different MAC address to your Ethernet shield, the router will most likely assign a different IP address.

Hi all,

Yesterday I just got a new Ethernet shield and have downloaded the latest Arduino 1.6.4 on macOSX. I am also a newbie and have tried this example as well. But in my case, it was totally failed.

With the totally same sketch just modified the MAC addrs which is pasted on my Ethernet shield.

When the sketch was running, it was always stuck in Ethernet.begin(mac), seeming like didn't get any return value back...

Any advise will be appreciate!

elsalin: Any advise will be appreciate!

The Ethernet shield is designed to be uses in a LAN network. For that you typically have a "router" which is connected to the Internet ("WAN") on one side and offers connectors to your "LAN" at home on the other side.

Do you have a router? How many "LAN" jacks does your router have? Is your Ethernet shield connected to one of the "LAN" jacks on your router?

This line in the code:

if (Ethernet.begin(mac) == 0)

will try to fetch an IP address from your router using DHCP, so the "DHCP" feature in your router must be enabled.

Hi jurs,

The router has 4 LAN jack(LAN port) on it.
I tested on a LAN network, using the same Ethernet cable to connect to my computer, I can get an ip address by ipconfig.

Any other thought?

Now I’m trying to look deeper in the Ethernet library to see where exactly it is stuck in.
Thanks!

Updated I tried to look deeper in the Ethernet library.

EthernetClass::begin() in Ethernet.cpp will try to get my config info from a DHCP server by using _dhcp->beginWithDHCP(mac_address); However, in the function of DhcpClass::beginWithDHCP, I've failed to find a DHCP server and got my configuration info.

It hangs in this statement in Dhcp.cpp: if (_dhcpUdpSocket.begin(DHCP_CLIENT_PORT) == 0) when trying to start EthernetUDP socket, listening at local port. It keeps going to socket() in socket.cpp and gets a socket 0 in my case and execute the statement of W5100.writeSnPORT(s, port);

It goes into the line of W5100.execCmdSn(s, Sock_OPEN); in socket.cpp and seems like it successfully executes the send command to socket and does wait for command to complete. But it never go into the line of SPI.endTransaction(); in socket.cpp

Does anyone know why?

"Does anyone know why?"

If you're going to tell the Ethernet library to use DHCP, you better have a DHCP server on the network. Otherwise, there is nothing it can do.... Your other option is to give it a fixed IP address.

Regards, Ray L.

Just to check your hardware setup, you can try the below code to see if the server will send you an error message

//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted

#include <SPI.h>
#include <Ethernet.h>

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

char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;

//////////////////////

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.1"); //download text
    client.println("Host: web.comporium.net");
    client.println("Connection: close");  //close 1.1 persistent connection  
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

Hi zoomkat,

It sucked in the beginning of setup() at Ethernet.begin(mac) as I mentioned before.
Therefore I cannot get any other message from the sketch you provided.

I don’t think it is because the router I tested is not included DHCP server.
will check if the DHCP server is curtain disable in the router…

Btw, I gave a fixed ip for the sketch.

Below code is what I’ve modified to fix ip. always got this output: My IP address: 0.24.24.24.
Even I ping 192.168.1.200 I get request timeout like this:
PING 192.168.1.200 (192.168.1.200): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2
Request timeout for icmp_seq 3

void setup() {

  byte ip[] = {192, 168, 1, 200};
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // this check is only needed on the Leonardo:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Ethernet.begin(mac, ip);
  
  // print your local IP address:
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();
}

Sorry it seems dumb but any suggestion?

elsalin: Sorry it seems dumb but any suggestion?

Perhaps a mechanical problem stacking the shield to the UNO board?

Please check the connection from Arduino board to Ethernet-Shield when stacking, especially if UNO board and Ethernet shield are no-name and not original ones:

Not only the header rows on the side must match, but the main connection from board to shield is established with the 6-pin ISP-header.

Please check spacing of board and shield: 1. Is the 6-pin connector (ISP-header) stacked deep enough to establish the connection? 2. Is there enough spacing left between USB jack of the UNO board and the soldering side of the Ethernet shield?

If the 6-pin ISP connector cannot stablish connection, perhaps the pin-header rows at the side need shortening by one millimeter.

If the shield stacks too deep and the USB jack perhaps creates a shortening with the soldering side of the shield, use some isolating tape sticking on top of the USB jack.

Hi jurs,

This is what it looks like:

https://drive.google.com/file/d/0B-_qgG9diqL1bXA2R2RYQ2lmZndHWU9rSHI0SUJIbG8wQzhn/view?usp=sharing

https://drive.google.com/file/d/0B-_qgG9diqL1Wi1ueWFKd1BKdVRUZnZiOXZaWmlIUUYzVEtN/view?usp=sharing

Both Ethernet shield and UNO are from official, but the Ethernet shield looks a little bit differ from the images shared on Internet. Mine has 6 extra components (2 greens, 2 oranges, 2 whites, dont know what they are) right after the Ethernet jack... This is the web link of the Ethernet shield I got: http://www.marutsu.co.jp/pc/i/228209/

I can see the power is lighting, indicators like 100M is green, LINK and FDX is orange, ACT is blinking in green over the Ethernet shield.

Just found that this Ethernet shield I got is using W5500.... sorry for missing this check. Maybe I have to get the libraries which support it.

elsalin: Just found that this Ethernet shield I got is using W5500.... sorry for missing this check.

The Ethernet library provided with the Arduino IDE is for the Wiznet W5100 Ethernet chip ONLY!

The Wiznet W5100 is the chip that is used on the "original" Arduino Ethernet shield and on compatible Ethernet shields.

If you use different hardware ==> use different library for that!

*** Update the result ***

After I upgraded the Arduino 1.7.4. with build-in library (But failed in library )
The DhcpAddressPrinter example works.

I can get the return My IP address: 192.168.1.168.

But somehow it is not a very quick response to get this ip address…
I almost wait for 1 minute then get the response.

Is it normal?

elsalin: I almost wait for 1 minute then get the response.

Normally you should get a DHCP response within the fraction of a second. That's all I can say from using Arduino versions 1.0.1 up to 1.6.4 and W5100 Ethernet shields.

Arduino IDE 1.7.x versions are offered for download by the other Arduino company which is operating at newly created http://www.arduino.org/ instead of the well known Arduino website at http://www.arduino.cc/ that is existing for many years.

Do they have a forum meanwhile at the other website? If you are operating with their hardware and their IDE version, perhaps contact them and ask about your hardware that cannot operate with the Ethernet library provided with the IDE 1.6.4 version.

Hi jurs,

Arduino.org does not have a forum. But I found this: https://github.com/arduino-org/Arduino/issues/10

Seems like no one would reply if I posted my issue there.. So I filed an issue on Arduino.cc back.. https://github.com/arduino/Arduino/issues/3278

That looks like the new "Ethernet 2" shield from Arduino.org; it looks pretty nice, actually. The library seems to be included with their 1.7.x IDE download, or you can probably get it here: https://github.com/arduino-org/Arduino/tree/ide-org-1.6.1.x/libraries

Hi westfw,

I've downloaded Arduino IDE to 1.7.2 and tried to get IP address from DHCP with Ethernet 2 library. The shield can properly get an IP address responded from DHCP. But my issue is the response latency is too long.

It took me nearly 30 seconds to get the response.

Already filed this issue on both Arduino.cc and Arduino.org Arduino.cc - https://github.com/arduino/Arduino/issues/3278 Arduino.org - https://github.com/arduino-org/Arduino/issues/12