Hardware Reference
In-Depth Information
Note that you can also use other pages here, for example ht-
tp://www.example.com/hello .
The Ethernet shield will then automatically get the IP address of this website.
To get an IP address for the Ethernet shield itself, we'll use DHCP to automatically get
one from the router we are connected to. However, if DHCP fails, we need to assign a de-
fault address to the shield.
This is stored in an IPAddress variable. Note that you can put anything you want inside
this variable. As for this first project, we really need DHCP to work to get connected to
the Web. However, it is a good practice to specify an IP address in the same subnet as
your router, so the shield can at least connect to your local network. For example, the IP
address of my computer was 192.168.1.100, so I specified a similar IP address for the
shield:
IPAddress ip(192,168,1,50);
We can now create the instance for the Ethernet client with the following code:
EthernetClient client;
Now, in the setup() function of the sketch, we will try to get an IP address using
DHCP. If you're connected to a router, there is no reason it would fail. However, if it does
indeed fail, we will automatically set the default IP address for the shield:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using
DHCP");
Ethernet.begin(mac, ip);
}
Then, we will print out the IP address on the Serial port for debugging reasons:
Serial.begin(115200);
Serial.print("IP address: ");
Serial.println(Ethernet.localIP());
Search WWH ::




Custom Search