Hardware Reference
In-Depth Information
#include <Ethernet.h>
#include <Dns.h>
#include <EthernetUdp.h>
#include <util.h>
Depending on your application, you may not need all these libraries. Some
projects might not use an Ethernet server or might not require DNS, but it is
best to start off with all the libraries and remove them later if required.
Starting Ethernet
Like many libraries, the Ethernet library is initialized with begin() . This func-
tion can be called in different ways, depending on your needs:
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);
In all cases, begin() requires a MAC address. The MAC address is either
supplied on a sticker attached to the Arduino or Ethernet shield, or you have
to invent your own.
WARNING Do not use the same MAC address for multiple devices. These
numbers are designed to be unique, and two identical MAC addresses on the same
network will result in both devices having connectivity problems. Switches have
an internal MAC table, and when it receives a packet, it updates the table. Packets
will then be forwarded to this host until the switch receives a packet from the other
device. On most switches, this will cause intermittent reachability, and on some
advanced switches, one device will be deactivated and cannot connect.
The MAC address is typically represented as an array of six hexadecimal bytes:
// The MAC address for this shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
For projects where multiple devices will be used or sold, consider placing the
MAC address in EEPROM. (EEPROM is presented in Chapter 6.)
If begin() is not supplied an IP address, it issues a DHCP request to coni gure
itself automatically. begin() returns an int ; 1 if the DHCP server was contacted
and DHCP information was received. Otherwise, it returns 0. All other uses of
begin() require an IP address, and do not return anything. To use this func-
tionality, you must import “Dhcp.h” and make sure your router can assign IP
addresses through DHCP.
 
Search WWH ::




Custom Search