Pages: [1]   Go Down
Author Topic: Ethernet can't connect at first time and problem with a domain without public IP  (Read 356 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all. Ethernet shield can't connect at first time. At second try everything works good. At first time I must wait for a while, next try is really fast (normal). What causes this problem?

I'm using this code:

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

#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

}

And the result is:

Code:
Better client test 9/22/12
Send an e in serial monitor to test
connection failed


disconnecting.
==================

[b]//second send an "e" and:[/b]

connected
HTTP/1.1 200 OK
Date: Mon, 02 Sep 2013 21:10:27 GMT
Server: Apache
Last-Modified: Sat, 13 Nov 2010 16:31:40 GMT
Accept-Ranges: bytes
Content-Length: 51
Connection: close
Content-Type: text/plain; charset=UTF-8

Woohoo! Your arduino ethernet client works!
zoomkat
disconnecting.
==================

Why that? In local network it works fine from the beginning. And why I can't connect with a domain without public IP? It just connects with IP, which is shared. How improve that? Please help.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5974
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is usually dns resolution that causes the delay. That would be up to the local ISP and dns servers. The initial resolution request may time out before the Arduino gets a response, so without the destination ip, the connection will fail.

The alternative is resolve the domain beforehand, and use that ip address. Like zoomkat's code above, use this. I used nslookup to resolve that domain name.
Code:
//Replace this
//char serverName[] = "web.comporium.net"; // zoomkat's test web page server
// with this
IPAddress serverName(208,104,2,86);
Does that do any better first time through?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Unfortunately there is no difference. Still I must wait ~25 seconds and I see "connection failed"  smiley-eek Second try works great. Any ideas?
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5974
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I can't see a reason in zoomkat's code. It should connect fine first time through. The only thing I could think of is a fussy router, but that is rare. The code does not make any unusual type requests that would cause a router to hesitate or fail.

Have you tried waiting several seconds before sending the request?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Still no difference, I've tp-link tl-wr740n router, with a new firmware. I even changed ethernet cable. A few hours of trying and searching web - without results.

And off course thanks for your feedback smiley
Logged

Pages: [1]   Go Up
Jump to: