Ethernet Help.

Ok, I am not very experienced with Arduino, but I really want to do an Ethernet WebSever. I have been trying for two days now without a single drop of progress. Please keep in mind I am a person with no real deep understanding of how IP addresses work. So here we go: I am using this tutorial: Arduino Ethernet Shield Web Server Tutorial I am still on step 2. The problem is, I think, the IP and MAC addresses. In step two, they say "Change the IP address in the sketch to match the IP address range of your network." The problem is, that there are a few different addresses and I do not know which one to use. I go to command prompt and type in ipconfig to get a list of the different addresses, but there are so many. So the first part of my question is: what IP address do I type in to this part of the sketch so I can type some IP into a browser to view my page: IPAddress ip(10, 0, 0, 20); // IP address, may need to change depending on network. OK, part two: So now the problem is the MAC address. I have a little sticker on the Ethernet shield box with a 12 digit code separated by a - per 2 numbers. Now in this part of the sketch: byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; there is an 0x and then two letters. So then lets say just for example my sticker says fd-90-rd-65-er-43, do I simply replace the letters after the 0x with my code to make it look like this: 0xfd, 0x90, 0xrd, 0x65, 0xer, 0x43 or do I take out the "0x" to make it look like this: fd, 90, rd, 65, er, 43.

That is the question, please feel free to add other things that could be causing this problem and how to fix them.

Thank you

Andreus7422:
\So the first part of my question is: what IP address do I type in to this part of the sketch so I can type some IP into a browser to view my page: IPAddress ip(10, 0, 0, 20);

Depends on your network. If you're having trouble, post a screenshot of your ipconfig screen and we can direct you towards a likely solution. Alternatively, you can read more about IP addresses and sub-netting here: IP Tutorial: Subnet Mask and Subnetting.

Ultimately, it's going to depend on your router's settings. It needs to be something within the subnet of your router's local network, but outside of it's DHCP scope (You don't want the router giving out the address to a DHCP client if your Arduino has already claimed the address.

Now in this part of the sketch: byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; there is an 0x and then two letters. So then lets say just for example my sticker says fd-90-rd-65-er-43, do I simply replace the letters after the 0x with my code to make it look like this: 0xfd, 0x90, 0xrd, 0x65, 0xer, 0x43

Yes. 0x denotes a HEX represented number. MAC addresses are almost always given out in HEX. More info on numbering systems: Atrevida Tutorial 1: Introduction to Binary and Hexadecimal

Web client test code you can try as is to see if you can make a connection.

//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

}

Yes, try z-k's code.

The REALLY BIGGGGGG problem with the Arduino Ethernet library is that, "only" the Ethernet.begin(mac) function returns a code that tells if it was successful or not in connecting to the router, whereas none of the other .begin() functions in the library do so.

The startingelectronics example, which is basically just a clone of the WebServer example in the Arduino IDE, as well as many other examples use Ethernet.begin(mac, ip), which does "not" return a success/failure code. Therefore, if this is what you use, you don't know if your ethernet shield ever actually made connection with the router. z-k's code does it the correct way.

For background, you can look up the concept of "Catch 22" ;-).

Also, it doesn't matter what mac number you use, if you only have 1 ethernet shield. You're unlikely to choose what your PC is using.

Test code for checking router assigned IP address.

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

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

void setup()
{
  Serial.begin(9600); 

  // disable SD SPI if memory card in the uSD slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.println("Starting w5100...");
  if(!Ethernet.begin(mac)) Serial.println("failed");
  else Serial.println(Ethernet.localIP());
}

void loop() {

}

The startingelectronics example, which is basically just a clone of the WebServer example in the Arduino IDE, as well as many other examples use Ethernet.begin(mac, ip), which does "not" return a success/failure code. Therefore, if this is what you use, you don't know if your ethernet shield ever actually made connection with the router. z-k's code does it the correct way.

For a server, this is not correct. Most require the same ip to be assigned each startup. Unless you set your router to issue a static dhcp assignment, you will lose contact with the server eventually when it is assigned a different ip.

Why do you feel you need to make a connection with the router when assigning a static ip?

edit: If you want to check the w5100 to see if it got the static ip assignment:

Ethernet.begin(mac,ip);
Serial.println(Ethernet.localIP());

I have a network with two client devices, one server, and a switch (no router). That means no dhcp server. How would your Arduino get an ip there?

SurferTim:

The startingelectronics example, which is basically just a clone of the WebServer example in the Arduino IDE, as well as many other examples use Ethernet.begin(mac, ip), which does "not" return a success/failure code. Therefore, if this is what you use, you don't know if your ethernet shield ever actually made connection with the router. z-k's code does it the correct way.

For a server, this is not correct. Most require the same ip to be assigned each startup. Unless you set your router to issue a static dhcp assignment, you will lose contact with the server eventually when it is assigned a different ip.

Why do you feel you need to make a connection with the router when assigning a static ip?

I completely disagree with this, as I brought up a couple of weeks ago on the thread about a "robust" Ethernet library. We're dealing with embedded systems here, so you HAVE TO HAVE proper handshakes and return codes on operations like connecting to foreign hardware. This is not a plug'n'play PC where all it's done for you.

IOW, all of the Ethernet.begin() functions need to have return values, or else have other ways to "directly" gauge the ethernet shield health and shield-to-router connection, if you really want to create a robust embedded system. And both of these features are missing from the current Ethernet library. My AMI home automation program is up to over 50KB of code now, and part of it is doing "continual" background health checks on the various hardware in the system, and I don't want it hanging for long periods if the router gets disconnected, etc.

Given the current Ethernet library, you are forced to do a workaround, as z-k described. Since my router has DHCP, what I "always" do on bootup is

(a) use Ethernet.begin(mac) first, since it returns a code indicating whether the router connection is valid.

(b) then, if (a) is valid, I use Ethernet.begin(mac,ip) to set a "known" IPaddress that the other nodes on my home network will always know is the place to go to access the AMI.

(c) if (a) is not valid, then I set a flag accordingly, and the system can still go on and do the other 98% of its work.

To me, this is the only close-to-robust way to connect to the router. If I didn't have DHCP, then I'd just be taking potshots in the dark.

I have a network with two client devices, one server, and a switch (no router). That means no dhcp server. How would your Arduino get an ip there?

What you've done here is illustrate the problems with the Ethernet.begin() function that I've been talking about.