Ethernet Shield + UNO connection problem

Hello everybody,
Just got my new Ethernet Shield for arduino uno. At first I don`t correctly wrote my IP address in to the code so I could not connect, but after some time I found the right IP. And I tried to connect, the first time everything worked well(I became information to my web browser from arduino), but when I have reloaded browser, arduino do not send data any more. I have tried to reboot arduino, to upload a code, and and and.. But without success, arduino do not answer..
Maybe somebody can help me? And bring me on the right way?
Thank you very much!

I have used this code:

#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:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,1 };  

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
Server server(80);

void setup()
{
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  // listen for incoming clients
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(analogRead(analogChannel));
            client.println("
");
          }
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

The ethernet library webserver code did not work for me (and others), so I use this code. Change the network settings to yours.
For IDE v1.0 only.

/*
A simple web server using an Arduino Wiznet Ethernet shield. 
For Arduino IDE V1.0 only. Previous IDE versions require mods to this code.

Original code created 18 Dec 2009
 by David A. Mellis
 modified 4 Sep 2010
 by Tom Igoe
 modified 18 Jan 2012
 by Tim Dicus 
*/

#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:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip( 192,168,1,10 );
IPAddress gateway( 192,168,1,1 );
IPAddress subnet( 255,255,255,0 );
IPAddress dns( 192,168,1,1 );

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

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

  // set SPI SS pins on w5100 and SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  // start the SD interface here if you want.
  // Add the SD.h library above
  // SD.begin(4);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, dns, gateway, subnet);
  // disable w5100 SPI so SD SPI can work with it
  digitalWrite(10,HIGH);
  delay(2000);
  server.begin();

  Serial.println("setup finished");
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("Client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      while(client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          Serial.println("Sending response");
          // send a standard http response header
          client.println("HTTP/1.0 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<HTML><BODY>TEST OK!</BODY></HTML>");
          client.stop();
        }
        else if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    Serial.println("Disconnected");
  }
}

Open the serial monitor and you can watch the connections.

Are you sure that the code is good? My IDE v1.0 can`t compile it.. comes message ('IPAddress' does not name a type).
Here how looks the code with my ip:

#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:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip( 92,168,1,1 );
IPAddress gateway( 192,168,1,254 );
IPAddress subnet( 255,255,255,0 );
IPAddress dns( 212,59,1,1 );

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

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

  // set SPI SS pins on w5100 and SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  // start the SD interface here if you want.
  // Add the SD.h library above
  // SD.begin(4);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, dns, gateway, subnet);
  // disable w5100 SPI so SD SPI can work with it
  digitalWrite(10,HIGH);
  delay(2000);
  server.begin();

  Serial.println("setup finished");
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("Client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      while(client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          Serial.println("Sending response");
          // send a standard http response header
          client.println("HTTP/1.0 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<HTML><BODY>TEST OK!</BODY></HTML>");
          client.stop();
        }
        else if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    Serial.println("Disconnected");
  }
}

I copied, pasted and compiled that code just now. It compiles and runs fine.

Are you sure you are using V1.0? That would be an error thrown by v0022 if you tried that code. I just checked that too, and that is the error it throws.

If you installed the IDE from the Ubuntu repository (or most other Linux repositories), that is v0022. If you then downloaded and unpacked the IDE v1.0 from the Arduino site, you need to run that code from the local directory. In the "../arduino-v1.0/" directory there is a shell script named arduino. Run that. Otherwise, you will run V0022.

I use both versions using that technique.

Edit: Here is the v0022 code

/*
A simple web server using an Arduino Wiznet Ethernet shield. 
For Arduino IDE V0022 only. IDE v1.0 require mods to this code.

Original code created 18 Dec 2009
 by David A. Mellis
 modified 4 Sep 2010
 by Tom Igoe
 modified 21 Jan 2012
 by Tim Dicus 
*/

#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:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,10 };
byte gateway[] = { 192,168,1,1 };
byte subnet[] = { 255,255,255,0 };

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
Server server(80);

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

  // set SPI SS pins on w5100 and SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  // start the SD interface here if you want.
  // Add the SD.h library above
  // SD.begin(4);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, subnet, gateway);
  // disable w5100 SPI so SD SPI can work with it
  digitalWrite(10,HIGH);
  delay(2000);
  server.begin();

  Serial.println("setup finished");
}

void loop()
{
  // listen for incoming clients
  Client client = server.available();
  if (client) {
    Serial.println("Client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      while(client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          Serial.println("Sending response");
          // send a standard http response header
          client.println("HTTP/1.0 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<HTML><BODY>TEST OK!</BODY></HTML>");
          client.stop();
        }
        else if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    Serial.println("Disconnected");
  }
}

Edit: Changed typo in ip and gateway.

At first the code doesn't work because in my documents folder was old libraries (from v0023)... Now I can compile the code in both IDE versions. But the main problem is still here.. With your code, Serial Monitor writes only that "setup finished", nothing more. And web browser: "The connection has timed out, The server at 92.168.1.1 is taking too long to respond.".. What should I try at next?
Thank you for your answers!

The network settings may be incorrect. I see a typo in that code. This:
byte ip[] = { 92,168,1,1 };
should be this:
byte ip[] = { 192,168,1,1 };

...and this is the ip of your router?
byte gateway[] = { 192,168,1,254 };

Edit: My bad. That was probably my typo when I converted that code to v0022. Sorry!

SurferTim:
byte ip[] = { 92,168,1,1 };
should be this:
byte ip[] = { 192,168,1,1 };

...and this is the ip of your router?
byte gateway[] = { 192,168,1,254 };

Yes, I know and I have tried with 192. But still no success..
I have wrote ipconfig /all in cmd and this is what I get:

The OS is on Russian language.. so I don`t exactly know where is gateway, but I think 192,168,1,254 is right or not?
P.S. On the picture my IP is 192,168,1,2 and in the code I write 192,168,1,1 it is because I have two routers one in the flat and another for the whole house.

Both routers on the same subnet? Not a problem if all is set correctly.
One router is 192.168.1.254.
And the other router ip? It isn't 192.168.1.1, is it?

Try changing the ip on the shield to 192.168.1.4.

Edit: If you are unsure about the other devices on the localnet, try pinging the shield ip from a localnet computer before connecting the shield to the network. If you get a response, don't use that ip.

I don`t know how you came on this IP address 192.168.1.4... But it is working very very well! You are my Hero!
Thank you very much!

Good to hear it is working.

Many router dhcp servers do not issue the first few or last few ip addresses, so I take advantage of that. :slight_smile:

If you have not altered your router's dhcp server (192.168.1.254), you should. Insure the dhcp server will not issue 192.168.1.4, or you will have the same problem when it does.

Another little problem :slight_smile: When I connect to arduino from my computer I use this address http://192.168.1.4/ And it is working fine!
Bet when my friend try to connect from his computer(he lives in another city) he can`t connect to arduino. Maybe he need another address? Not the same that I use to connect(http://192.168.1.4/)?
One more time thank you for answers!

That ip is good only for computers on your localnet (connected to your router). That is a private ip subnet.

You can access that from a public network by putting that ip (192.168.1.4) in the "DMZ" (ip/port forwarding). Then your friend can access it, but by using your public ip, not 192.168.1.4.

Edit: This is also conditional. If your internet account is not commercial (a residential type account), your ISP may block port 80 requests from the internet to your router.

And which one is my public IP? :slight_smile:

From your computer:

SurferTim:
Edit: This is also conditional. If your internet account is not commercial (a residential type account), your ISP may block port 80 requests from the internet to your router.

That means? If it so what can I do to change it?

I just have tried but without success. I made DMZ like you told, and my friend has tried to connect via public IP, but no connection..
Thank you for your answers!

Can your friend ping your public ip?

There are ways around the port 80 block.

The most legal is to get a commercial account with a static ip from your provider. Sometimes this is not cheap. Mine went from $39 US for a residential account to $99 for a minimum commercial account. I now pay well over $300/month.

The second way is to use a port other than port 80. May I suggest port 8088. Set that port in your server() code in the arduino, then check it on localnet. Does this respond?
http://192.168.1.4:8088

Then your friend should be able to use
http://yourpublicip:8088

SurferTim really you are my HERO! Thanks you it is working fine! I`m so happy now!

Mastino:
SurferTim really you are my HERO! Thanks you it is working fine! I`m so happy now!

Thanks!

Ok, now that his problem has been fixed, I'm going to high-jack this thread.

I've been working on connecting my arduino UNO with the (official) ethernet sheild for about 8 hrs now with limitted success. (btw: using v 1.0 IDE)

Right now it does connect, but only if I manually delete the IP address from my routers' DHCP table and then it only connects once or twice before jamming-up again.

I used some code from a previous post to connect to an XAMPP local server and read a text file, and when it does connect everything works fine. But what could be causing this issue?

Hijack away! At least this way I know what you have seen. :slight_smile:

Please post your code and describe your network setup, like gateway and subnet mask. dhcp server? Other network devices? The ip/subnet/gateway assigned to the computer you are attempting the connection from.

Can you ping it after it "jams up"?