Problems with Arduino Ethernet Shield :)

Hej, guys :slight_smile: I have a Arduino Ethernet Shield, and I read the tutorial on Arduino.cc, but still get some problem :slight_smile: Most of all I wanna know how to get the MAC address & IP address of that shield? Thanks in advance, have a great day! :wink:

/*
 Chat  Server
 
 A simple server that distributes any incoming messages to all
 connected clients.  To use telnet to  your device's IP address and type.
 You can see the client's input in the serial monitor as well.
 Using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)
 
 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 
 */

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1, 177);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);


// telnet defaults to port 23
EthernetServer server(23);
boolean alreadyConnected = false; // whether or not the client was connected previously

void setup() {
  // initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);
  // start listening for clients
  server.begin();
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Chat server address:");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // wait for a new client:
  EthernetClient client = server.available();

  // when the client sends the first byte, say hello:
  if (client) {
    if (!alreadyConnected) {
      // clead out the input buffer:
      client.flush();    
      Serial.println("We have a new client");
      client.println("Hello, client!"); 
      alreadyConnected = true;
    } 

    if (client.available() > 0) {
      // read the bytes incoming from the client:
      char thisChar = client.read();
      // echo the bytes back to the client:
      server.write(thisChar);
      // echo the bytes to the server as well:
      Serial.write(thisChar);
    }
  }
}

it's part of arduino's IDE (in the examples) you don't "get the mac" you "set the mac"

Mac address you can use what ever you want but not the same on two or more devices.

IPAddress ip(192,168,1, 177);  // This must be on the rage of you're modem so if for example you're modem you call it eg. 192.168.2.1 then the line must be IPAddress ip(192,168,2, 240); the first 3 (192,168,2) it is stay the same and you choose a free number above 1 and less than 255. with this number you will call arduino from the explorer if the port on arduino it 80 if not for example it is 90 then for to call you type 192.168.2.177:90

IPAddress gateway(192,168,1, 1);  // This number is the number you call the modem

If you don't know what is the number of the modem you can go on PC --> Star --> Run --> type cmd and when the dos windows open type ipconfig /all
The number you will find next of the "Default Gateway" it is the Modem adress IP, this number you will write on the IPAddress gateway ....

Good luck.

Basic client test code to check your hardware/network setup.

//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;

//////////////////////

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    client.println("Host: web.comporium.net");
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

tinker1992:
Most of all I wanna know how to get the MAC address & IP address of that shield?

First of all, your ethernet shield has NOT assigned a fixed IP address! MAC address are printed on bottom side of the shield or you may use your imagination and compose ANY mac address you want (mac must be unique in your network's subnet). If you do not know if your subnet have DHCP Server, you can try it. I use subroutine "checkdhcp()" :

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 
  0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC }; // <---- read MAC from bottom of original shield or use random HEX numbers

bool checkdhcp()
{
  Serial.println(F("Waiting for DHCP lease ..."));
  if (Ethernet.begin(mac) == 0){ 
    Serial.println(F("Failed to configure IPv4 for using Dynamic Host Configuration Protocol!"));
    return false;
  } 
  else { 
    Serial.println(F("Dynamic Host Configuration Protocol passed"));
    Serial.print(F("Board IP address = "));
    Serial.println(Ethernet.localIP());
    Serial.print(F("Network subnet mask = "));
    Serial.println(Ethernet.subnetMask());
    Serial.print(F("Network gateway IP address = "));
    Serial.println(Ethernet.gatewayIP());
    Serial.print(F("DNS IP address = "));
    Serial.println(Ethernet.dnsServerIP());
    Serial.print(F("MAC = "));
    for (byte thisByte = 0; thisByte < 6; thisByte++) {
      if (mac[thisByte] < 16){
        Serial.print(F("0"));
      };
      Serial.print(mac[thisByte], HEX);
      if (thisByte < 5) {
        Serial.print(F(":"));
      };
    };
    Serial.println();
    return true;
  };
};

void setup()
{
  if (!checkdhcp()){
    /* nothing to do */
  };
};

void loop()
{
  // some code here
};

The main "magic" is in the Ethernet.begin(mac) formulation - it causes, that your ethernet shield will ask DHCP server and if success then gets IP Address from this one.

Thank you my friends :wink: Sadly I went to school two weeks ago, without my electronic stuffs(( + it's so hard to survive from this kind of education full of Tons of exams, ++ it's more sensible to read those interesting English technology books rather than those most boring Chinese textbooks...

You can use the Terminal or CMD window to get your network details (ifconfig for Mac, and ipconfig/all for Windows.
Also you can see your IP from Arduino's Serial Monitor if you run the DHCPAddressPrinter example. For the MAC address I use frequently an Android app called Fing that reports all the devices connected in your local network together with IPs and MAC addresses.