Hardware Reference
In-Depth Information
DNS and DHCP (cont'd)
The following example shows how to request an address using DHCP; it thenreports back the address obtained:
/* DHCP
Context: Arduino
*/
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01};
IPAddress ip(192,168,1,20); // an address to use if DHCP fails
void setup() {
// start the serial library:
Serial.begin(9600);
// start the Ethernet connection:
Serial.println("Asking for an IP address using DHCP...");
if (!Ethernet.begin(mac)) {
// if DHCP fails, set your own address:
Ethernet.begin(mac, ip);
}
// print the bytes of the IP address, separated by dots:
Serial.print("I got an IP address. It's ");
Serial.println(Ethernet.localIP());
}
void loop() {
}
The begin() technique is useful in any Ethernet sketch because it uses DHCP if available, and sets an IP address manually if
not. If you know you're going to set the address manually and want to save program memory, just comment out the if() line
and the corresponding bracket, and leave the command inside it, like so:
//if (!Ethernet.begin(mac)) {
// if DHCP fails, set your own address:
Ethernet.begin(mac, ip);
// }
Search WWH ::




Custom Search