operation of ethernet.begin

Hello everybody,

I the function gets mac and ip arguments, will it use the ip or try to acquire it from a dhcp?

Would it be correct to say the giving mac AND ip is more reliable as Ethernet will be initialized with given ip in case of no response from dhcp? (assuming one knows how to pick a good ip)

Many thanks

That depends on how dependent on the network settings you want to be. A static IP is easier to deal with when using server code, but not as flexible as a client using dhcp.

Normally if there is no response from the dhcp server, that indicates the router is offline.

Pick a static IP within the localnet range, but outside the dhcp range. You'll need to login to your router to find that info.

Thank Tim,
I should have numbered my questions

  1. if both mac and ip args are given would éthernet.begin even try dhcp?
  2. if answer to 1) "yes it will try dhcp even if ip is supplied" then, will it fallback to the given ip should it get no reply from dhcp?
    thanks again
    Guy
  1. No.
  2. N.A.

So, what's the point in the (mac,ip) combination listed here? What would have changed from mere 'ip'

Syntax

Ethernet.begin(mac);
Ethernet.begin(mac, ip);
Ethernet.begin(mac, ip, dns);
Ethernet.begin(mac, ip, dns, gateway);
Ethernet.begin(mac, ip, dns, gateway, subnet);

All the static IP Ethernet.begin functions call the next function in line. So if you call the begin function like this

byte mac[] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xED);
IPAddress ip(192,168,0,2);

Ethernet.begin(mac,ip);

// then it creates this variable
IPAddress dns_server(192,168,0,1);
// and calls the next function like this

Ethernet.begin(mac,ip,dns_server);

// then it creates this variable
IPAddress gateway(192,168,0,1);
// and calls the next function like this

Ethernet.begin(mac,ip,dns_server,gateway);

// then it creates this variable
IPAddress subnet(255,255,255,0);
// and calls the next function like this

Ethernet.begin(mac,ip,dns_server,gateway,subnet);

Here are the functions from Ethernet.cpp

void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip)
{
  // Assume the DNS server will be the machine on the same network as the local IP
  // but with last octet being '1'
  IPAddress dns_server = local_ip;
  dns_server[3] = 1;
  begin(mac_address, local_ip, dns_server);
}

void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server)
{
  // Assume the gateway will be the machine on the same network as the local IP
  // but with last octet being '1'
  IPAddress gateway = local_ip;
  gateway[3] = 1;
  begin(mac_address, local_ip, dns_server, gateway);
}

void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
{
  IPAddress subnet(255, 255, 255, 0);
  begin(mac_address, local_ip, dns_server, gateway, subnet);
}

void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
{
  W5100.init();
  SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
  W5100.setMACAddress(mac);
  W5100.setIPAddress(local_ip.raw_address());
  W5100.setGatewayIp(gateway.raw_address());
  W5100.setSubnetMask(subnet.raw_address());
  SPI.endTransaction();
  _dnsServerAddress = dns_server;
}

Thank you Tim

I realize my mistake: Giving mac parameter not only tells the function to go and get an ip but more simply set the mac of the shield (something uncommon in PC NICs with 'etched' mac). My conclusion is that if one knows how to assign the ip he'd have a more reliable and faster code

Guy