-1

I'm using an Arduino Uno and Ethernet shield with the WIZnet chip W5100. The WebServer code example is working. DHCP is working, the Ethernet shield is receiving an IP address from the Linksys modem "router" and everything is fine.

But I cannot connect to a web site using the WebClient code. I found out that client.connect(server, 80); is returning -1 "Time_OUT" and it is supposed to return 1 according to the Arduino Ethernet library.

I tried connecting to different websites. I tried assigning an IP address manually, not using DHCP. I tried making pin 4 of the SD memory card SPI to output pinMode(4, OUTPUT); digitalWrite(4, HIGH); But it doesn't work. I get on my Serial line connection failed or it returns "connected" then "Disconnecting..." immediately after because client.connect has returned -1.

The example code I'm using:

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x28, 0xAD, 0xBE, 0xEF, 0xB9, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "www.google.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:

  Serial.println("connecting...");
  delay(2000);
  Serial.println(Ethernet.localIP());

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.print(client.connect(server,80));
    Serial.println();
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

This is my output:

enter image description here

Using a local server IP address like my router's IP address, the connection works. But the issue is mainly in the WAN. Any help would be appreciated.

migrated from electronics.stackexchange.com Dec 16 '16 at 16:38

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • Have you tried using numeric server IP address (i.e. server(74,125,232,128))? – Dmitry Grigoryev Dec 16 '16 at 13:53
  • Also, Serial.print(client.connect(server,80)); seems to establish a second connection without using the first one. Did you want to print a string on the terminal instead of actually running the function? – Dmitry Grigoryev Dec 16 '16 at 13:57
  • Before adding this line it was the same issue this line is just returning the result for me. Yes i used numeric ip so i wont use DNS and doesn't work. Im afraid its a modem issue because the same setup with the same shield and same arudino with the same code worked on another network. – Kegham K. Dec 16 '16 at 14:37
  • @DmitryGrigoryev By the way my shield came without a mac address printed on it and i'm using a random mac address. Could this be an issue? – Kegham K. Dec 16 '16 at 15:11
  • Don't cross post – gre_gor Dec 16 '16 at 18:53
1

This doesn't look right:

if (client.connect(server, 80)) {
    Serial.print(client.connect(server,80));
    ...

If the first call to client.connect() succeeds, you call it again immediately, and the error message you're seeing comes from that second call. This really is not surprising — I can't imagine a scenario in which that would actually work.

What you really want is something more like:

if (result = client.connect(server, 80)) {
    Serial.print(result);
    ...
  • This is not an issue Dave your way will work and my way is working. Im just retrieving the result and it is working and writing -1 on the serial line. – Kegham K. Dec 16 '16 at 14:35
  • That's strange ... I could've sworn that you said it wasn't working. My bad. In that case, we'll send this over to Arduino.SE where they might be able to help you with the details of that particular library. – Dave Tweed Dec 16 '16 at 16:38
  • Thanks Dave. I tried also using the ip address i get no result at all from the client.connect() so connection fails. – Kegham K. Dec 16 '16 at 16:42
  • Have you tried getting the result code like he suggested? Using the IPAddress and not the string, if it still doesn't connect and nothing is printed, then it means connect() returns 0. Try opening the EthernetClient source and adding a few print()s in connect() to find out the exact condition that causes the 0 return code. Though this looks like a connection issue since getHostByName() is failing just like connect(). You sure your modem is connected to the net? – TisteAndii Dec 17 '16 at 1:51
  • @TisteAndii thanks for your help. The issue was in the mac address. Because my shield has no mac address and i was using a well known manufacturer mac address so it wasn't completing the connection with the google server and it was dropping it. – Kegham K. Dec 17 '16 at 20:29
0

I resolved my problem:

The problem was in the mac address itself. When i used the mac address given by the example itself the connection didn't fail and client.connect() returned 1.

I used this mac address instead of the one in the code above:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.