I think I find some bugs in the sample code of IPAddress class reference page. Here is the url: Ethernet - Arduino Reference
The original code is
#include <Ethernet.h>
// network configuration. gateway and subnet are optional.
// the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// the router's gateway address:
byte gateway[] = { 10, 0, 0, 1 };
// the subnet:
byte subnet[] = { 255, 255, 0, 0 };
EthernetServer server = EthernetServer(23);
//the IP address is dependent on your network
IPAddress ip(192,168,1,1);
void setup()
{
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
}
void loop()
{
//print out the IP address
Serial.println([color=red]myIPaddress[/color]);
}
This code is quite simple but includes some mistakes:
1 Variable "myIPaddress" is not defined.
2 "Serial" is not initialized in the "setup" function so you won't see any output.
3 "SPI.h" is not included so you will encounter compile failure.
I fix the code and this is my code.
#include <Ethernet.h>
#include <SPI.h>
// network configuration. gateway and subnet are optional.
// the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// the router's gateway address:
byte gateway[] = { 10, 0, 0, 1 };
// the subnet:
byte subnet[] = { 255, 255, 0, 0 };
EthernetServer server = EthernetServer(23);
//the IP address is dependent on your network
IPAddress ip(192,168,1,1);
void setup()
{
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600);
// start listening for clients
server.begin();
}
void loop()
{
//print out the IP address
Serial.println(Ethernet.localIP());
}
My code runs correctly in my MEGA2560 board. I hope the website administrators can fix the bugs and revise the code.