Arduino Ethernet Shield used to Create Simple WebPage not Working

Hey I am learning about Arduinos using “Programming Arduino” by Simon Monk - I got to the part concerning ethernet shields and servers and such so I bought this http://www.ebay.com/itm/400356634583?_trksid=p2059210.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT shield which is based on the Wiznet W5100 chip so it should be fine. I am using it with an Arduino Uno R3.

I then uploaded this code as instructed and as instructed changed the IP to something that was not already used on my LAN

// sketch 10-01 Simple Server Example

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


// MAC address just has to be unique. This should work
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// The IP address will be dependent on your local network:
byte ip[] = { 192, 168, 1, 56 };

EthernetServer server(80);

void setup()
{
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.begin(9600);
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) 
  {
    while (client.connected()) 
    {
      // send a standard http response header
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println();
      
      // send the body
      client.println("<html><body>");
      client.println("<h1>Arduino Server</h1>");
      client.print("<p>A0="); 
      client.print(analogRead(0)); 
      client.println("</p>"); 
      client.print("<p>millis="); 
      client.print(millis()); 
      client.println("</p>"); 
      client.println("</body></html>");
      client.stop();
    }
    delay(1);
  }
}

I then connected the arduino via Ethernet cord to my wireless router and looked up the arduinos IP(192.168.1.56) – the browser either says “The connection has timed out–The server at 192.168.1.56 is taking too long to respond.” or “Firefox could not connect to the server” – I tried reconnected the arduino – reloading the code - even resetting the router - nothing worked - it also does not show up on the list of connected devices on my routers page.

Any help will be GREATLY appreciated - THANKS!

The Uno R3 (and also Mega R3) needs an Ethernet Shield version R3. The one you bought is a 2009 version.
The Uno R2 (and also Mega R2) needs an Ethernet Shield version R2.

I don’t know what did change, I think it has to do with the SPI bus.

That ethernet shield should work ok. It has the ICSP connector. I use a WiFi shield R3 on an Mega R2. There is a requirement to use a jumper from the 5v pin to the ioref pin on the shield.

Are you certain you are using an IP in the localnet range of the ethernet ports on the router? What make/model router do you have?

edit: You shouldn't need any jumpers or pin bending to use the pre-R3 ethernet shields with an R3 Uno or Mega as long as the shield has the ICSP socket on the bottom.

Have you tried using DHCP on the shield to get an IP?

void setup()
{
  if(!Ethernet.begin(mac)) Serial.println("DHCP failed");

  Serial.println(Ethernet.localIP());

  server.begin();
  Serial.begin(9600);
}

It is an Actiontec PK5000

I am sure - my computer and other family members computers have the same form - except for the last number of course - which can be anywhere between 0 and 255

So the code you have there will automatically assign a IP to the arduino? - and no I haven't tried that

THANKS!

Give that a try. If the dhcp startup fails, it may take a couple minutes to display the “DHCP failed” message. If it doesn’t display anything for a few minutes, that can be caused by a SD card in the shield’s slot. If you have a SD card in the shield’s slot , remove it for this test.

If it fails, I have another test you can try.

edit: This code tests the SPI connection to the w5100. If the serial monitor shows 192.168.2.2, then it is working ok. If it shows anything else, like 0.0.0.0 or 255.255.255.255, then you have a problem there.

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

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,2);

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

  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  // Start ethernet
  Serial.print(F("Starting ethernet..."));
  Ethernet.begin(mac, ip);
  Serial.println(Ethernet.localIP());

  delay(2000);
  Serial.println(F("Ready"));
}

void loop() {
}

One question - if I use DHCP I delete the byte IP[] = {ip address}; correct?

And then what do I look up in my browser to connect to the Arduino - my computer is not by my Router(wifi) so I have no way of viewing what's in the serial monitor

That code prints 192.168.2.2 in the serial monitor

Ok so I tried to upload this code:

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 2, 2);

void setup() {
   if(!Ethernet.begin(mac)) {
  Serial.println("DHCP Failed");
  }
  Serial.println(Ethernet.localIP());
  server.begin();
  Serial.begin(9600);
   
   pinMode(4, OUTPUT);
   digitalWrite(4, HIGH);
   
   Serial.print(F("Starting ethernet......"));
   Ethernet.begin(mac, ip);
   Serial.println(Ethernet.localIP());
   
   delay(2000);
   Serial.println(F("Ready"));
}

void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) 
  {
    while (client.connected()) 
    {
      // send a standard http response header
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println();
      
      // send the body
      client.println("<html><body>");
      client.println("<h1>Arduino Server</h1>");
      client.print("<p>A0="); 
      client.print(analogRead(0)); 
      client.println("</p>"); 
      client.print("<p>millis="); 
      client.print(millis()); 
      client.println("</p>"); 
      client.println("</body></html>");
      client.stop();
    }
    delay(1);
  }
}

But it returned ‘server’ was not declared in this scope

How do I fix this?

You need to find the correct network settings for your localnet. You must get your computer or laptop close enough to the router to view the dhcp info. Use this code to get your network settings. This should show you all the settings you need.

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

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

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

  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.print(F("Starting ethernet..."));
  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else {
      Serial.print(F("IP: "));
      Serial.println(Ethernet.localIP());
      Serial.print(F("Subnet: "));
      Serial.println(Ethernet.subnetMask());
      Serial.print(F("Gateway: "));
      Serial.println(Ethernet.gatewayIP());
      Serial.print(F("DNS server: "));
      Serial.println(Ethernet.dnsServerIP());
}

void loop() {
}

One question - if I use DHCP I delete the byte IP = {ip address}; correct?

If you are trying to make a server setup, then you probably should not use DHCP as you really need to use a fixed IP address. Below is some client test code you can try to see if you have some basic network/hardware issues. When you send an efrom the serial monitor, you should get a response from the server.

//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.1"); //download text
    client.println("Host: web.comporium.net");
    client.println("Connection: close");  //close 1.1 persistent connection  
    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

}

Both of your methods worked - thanks a ton! I have no idea what was wrong with the original code as it was directly from the instructional book but hey now it works

The only issue I have now is the fact that the Ethernet Shield is sooo power hungry only the computer will power it(when its plugged in via programming cable) - a 9v battery doesn't seem to have enough uuuumph - only the red LED's dimmly light up - the other green and yellow ones indication ethernet stuff don't - plus it doesn't show up in my browser then - that can be fixed by a bigger PS - so pretty simple

THANKS again!

The w5100 chips require a lot of power, and actually run noticeably warm. Probably quickly kill a small 9v battery.

Yeah I found that out haha - ill hook up a wall wart to it and it'll all be fine