Pages: [1]   Go Down
Author Topic: HELP: Web Client connection failed!  (Read 1930 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi folks

I'm a Arduino (also programming) beginner  and I have problem connecting Arduino Uno + Enternet Shield with the standard WEB CLIENT Sketch

The program doesn't enter in the "If" condition [client.connect()]but I really don't understand why.

At the same time the WebServer sketch works perfectly....(so I suppose that IP & MAC address are fine)

I'm working inside a private network (so Arduino IP isn't public): could this be a problem on client connection? (even if the server works?)

Anyone could help me?

following the (well known) 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[] = { 10,172,85,203 };

byte server[] = { 209,85,148,106 }; // Google


// 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):
Client client(server, 80);

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

  delay(1000);

  Serial.println("connecting...");

  if (client.connect()) { //Here where I can't enter in !!
    Serial.println("connected");
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;smiley-wink
      ;
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.selfseo.com/find_ip_address_of_a_website.php
Use this link to find the IP address that a URL points to. Try a couple of different IP addresses for URL that you can access from the same network that the Arduino is connected to.

If your PC can access the site by IP address, the Arduino should be able to.
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Some simple client test code you can try. Use the tool bar # code tag for posting your code.

Code:
//zoomkat 12-01-11
//simple client test
//for use with IDE 0021
//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
byte ip[] = { 192, 168, 1, 102 }; // ip in lan assigned to arduino
//byte gateway[] = { 192, 168, 1, 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
Client client(myserver, 80);  // connect to web server using port 80

//////////////////////

void setup(){

  Ethernet.begin(mac, ip);
  //Ethernet.begin(mac, ip, gateway, subnet);
  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()) {  //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

}
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@zoomkat

I tried your code but no way: I arrive till "sendGET()" and then I receive a "connection failed" after waiting a bunch of second on " if (client.connect())"

Could it be a sort of Ms Xp Bugs?
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Could it be a sort of Ms Xp Bugs?

No, works fine on my xp machine. You may need to read and study more postings on client/lan/internet connections. You will need to consider the network you are connecting to (lan, router, and such) to see why the connecion is failing. If you are behind a router, make sure the ip address you assign to the arduino is compatable with the router and not in use by another device.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

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

I have the same problem: CONNECTION FAILED. I try to connect the Intel Galileo at the server of Google.
Here is my code:

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


byte mac[] = { 0x98, 0x4F, 0xEE, 0x00, 0x55, 0xCD }; //Intel Galileo MAC Adresse
byte ip[] = { 160, 78, 27, 210 }; //Intel Galileo IP
byte server[] = { 173, 194, 33, 104 }; // Google

EthernetClient client;

void setup()
{
 // system("ifconfig eth0 160.78.27.210 netmask 255.255.255.128 up");
  Ethernet.begin(mac, ip);
  Serial.begin(9600);

  client.connect(server,80);
  delay(1000);
  
  Serial.println("connecting...");

  if (!client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}
Where is the problem?
« Last Edit: March 07, 2014, 08:44:53 am by Ilthebest » Logged

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

I had the same problem. Did you remove your SD-card? Therefore the WebServer example works perfect. It initializes the SD. If you don't do that, it produces failures...

This helped for me.
Logged

Pages: [1]   Go Up
Jump to: