Go Down

Topic: I find some bugs in the sample code of "IPAddress()" class document (Read 1 time) previous topic - next topic

Refyzd

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: [Select]

#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: [Select]

#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.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy