Pages: [1]   Go Down
Author Topic: I find some bugs in the sample code of "IPAddress()" class document  (Read 921 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think I find some bugs in the sample code of IPAddress class reference page. Here is the url: http://arduino.cc/en/Reference/EthernetIPAddress

The original code is
Code:
#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.
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.
Logged

Pages: [1]   Go Up
Jump to: