Beginner Problem with Arduino Ethernet Shield - Can't Connect

Hi all,

iam currently at beginners level with that whole stuff. I ordered an Arduino Uno SMD Edition together with an Ethernet Shield.
I stacked them together, attached some DHT11 Sensors and these are working quite well. But at the moment i have a problem
getting the ethernet to work.

I tried the code that you see below. I want to connect to a webpage but that connect already fails so iam not able to make
subsequent http calls. I tried serveral websites (1. the webinterface of my router in the local network, another webserver on my
local network, and my webpage on the internet). All of them are reachable via my pc but my adruino won't connect i always
get the connection failed message.

I get an ip via dhcp which is also pingable from my pc so that does not seem to be a problem.

Code:

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

byte mac[] = {  .... };
IPAddress ip(192,168,0, 50);
IPAddress gateway(192,168,0, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress server = (192,168,0, 1);
int incommingdata = 0;
EthernetClient client;

void setup()
{
  Serial.begin(115200);
  pinMode(13, OUTPUT); 
  Serial.println("Initialising Ethernet...");
  Ethernet.begin(mac);//, ip, gateway, subnet);  
  delay(1000);
  Serial.println(Ethernet.localIP());
  Serial.println("connecting...");
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{

....
...

}

Any suggestions here ? Iam really stuck at the moment.

Thanks in advance

ok, so replay on myself ;). I actually got it to connect to my routers webinterface which is 192.168.0.1, local ip. But it does not work with an internet ip. which is quite strange.
Is there some function to return also subnet and gateway config that the interface received from dhcp ? Probably its something wrong there.

Thanks

Normally, the gateway ip for your localnet is 192.168.0.1. You should check the router setup to insure that is the correct gateway.

Insure you can connect to that server's public ip with a web browser on a localnet computer.

Hi Tim,

yes my gateway is indeed 192.168.0.1. I double checked the connectivity to the ip i want to connect to and its also working from the pc.
Even if i set the whole subnet, ip, gateway configuration manually it won't work. So i guess there is no function for reading the ethernet configuration
after it has been set ?

Thanks

CaptainChemnitz:
Hi Tim,

yes my gateway is indeed 192.168.0.1. I double checked the connectivity to the ip i want to connect to and its also working from the pc.
Even if i set the whole subnet, ip, gateway configuration manually it won't work. So i guess there is no function for reading the ethernet configuration
after it has been set ?

Thanks

Did you find a function to do what you want in the Ethernet library header file?

The network settings should be the same as a localnet computer.

Are you attempting the connection using the ip address with your web browser? You are not using a URL, are you? There can be a difference in the way the URL is requested.

Do you know that the IDE v1.0 and later have a different format for Ethernet.begin()? Note the addition of a dns server ip.

This is by dhcp:
Ethernet.begin(mac);

This is manually set:
Ethernet.begin(mac,ip);
Ethernet.begin(mac,ip,dns);
Ethernet.begin(mac,ip,dns,gateway);
Ethernet.begin(mac,ip,dns,gateway,subnet);

  pinMode(13, OUTPUT);

You seem unaware of which pins the Ethernet shield uses. Hint: 10, 11, 12, and 13. Uh, oh.

void loop()
{

....
...

}

Does that even compile?

Some simple client test code you can try.

edit: Made a slight tweek to the code changing ip address from 192, 168, 1, 1 to 192, 168, 0, 1 to match your router addressing.

//zoomkat 12-08-11
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 0, 102 }; // ip in lan assigned to arduino
//byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
//byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte myserver[] = { 208, 104, 2, 86 }; // zoomkat web page server IP address
EthernetClient client;
//////////////////////

void setup(){

  Ethernet.begin(mac, ip);
  //Ethernet.begin(mac, ip, subnet, gateway);
  Serial.begin(9600); 
  Serial.println("Better client test 12/01/11"); // 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(myserver, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    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

}

Hi,

thanks for the replys so far:

@DHunt: Really good hint. I just took a look there and there are indeed coressponding functions. They are
IPAddress localIP();
IPAddress subnetMask();
IPAddress gatewayIP();
IPAddress dnsServerIP();
I will try them this afternoon.

@SurferTim: As i stated earlier, iam able to connect to the desired IP from a webbrowser and iam indeed using an ip and port, no URL. Additionally, the request isn't the problem at the moment because the earlier step (the connect) is whats not working right now. Regarding the Ethernet.begin, i wasn't aware that there also is a DNS parameter i will give that a try later today when iam back home. Seems like the documentation on the arduino website isn't even close to be up to date, isn't it ? ^^

@PaulS: You are right, pin 13 reference might have to be removed but as my problem atm is that only connection to internet ip's won't work, i dont think that this matters here. Connection to internal IPs work properly and also ping is fine. The void loop part of the code was omitted here in my posting as it has nothing to do with the problem iam facing. I just intended to make the post easier to read and keep it focused to the actual problem.

@zoomkat: I will give your sample code a try too as soon as iam back home today.

i will keep you updated.

Hi all,

i finally solved my problem. The connection is working properly now. Actually zoomkat's example code did the trick for me. After i tried you example and found that its working, i compared your code with my code and the following line was the problem

IPAddress server = (192,168,0, 1);
changed to
byte server[] = { 192, 168, 0, 1 };

When the variable with the server ip is defined as IPAdress and not as byte[] the connect won't work. I changed that and everything is fine now.
I still don't know why it worked with the IPAdress datatype for local ip's in the first place. Probably a bug in the ethernet library.

Thanks for the help.

to intialize IPAddress object you need to do

IPAddress server = IPAddress(192,168,0,1);

you did not get a compile error because the = operator is overloaded in IPAddress class as
IPAddress& operator=(const uint8_t *address);
IPAddress& operator=(uint32_t address);

I have been trying to connect an Ethernet Shield 2 for days without success but today I managed to get it working. This is how I did it.

1/ Because I am using the Ethernet Shield 2, it actually requires library <Ethernet2.h> and not <Ethernet.h>. Be aware that this is true at least for the Ethernet Shield 2 with the Wiznet5500 chip.
So first, check the chip on the Ethernet Shield2. This chip might look as if it has no writing, but in fact if you take a magnifying glass you can see that it has very fine print on it. Since I did not have a magnifying glass, I took a photo of it with my iPhone and then magnified the photo on the phone. That allowed me to clearly see the mark and type of the chip, ie Wiznet W5500.

2/ So your code should not start with :

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

but with:

#include <SPI.h>
#include <Ethernet2.h>

In my case this library was not present and the code did not compile correctly, so I had to download it. I found it here :

I downloaded the latest version, in my case it was Ethernet2-1.0.4.zip. You can download it to any folder.

3/ Then go back to the sketch where you changed <Ethernet.h> to <Ethernet2.h>. Go to Sketch > Use library > add .ZIP library (or something along those lines, since my IDE was not in English.)

4/ Add the new library you just downloaded. In my case it went quite smoothly and it was added without any further action required.

5/ This makes the code compile correctly and the Ethernet Shield 2 connect without a problem.

Hope this helps.

You save me with this Los 86. Kudos for you.

Thanks for sharing your findings. This saved my day !!

Hope this info finds its way into the Arduino Ethernet Shield page