Problem With Ethernet Client

I recently installed an Ethernet Shield W5100. It works great as a server but I'll be darned if I can't make any examples of a client work. They seem to get hung in never never land on the client.connect(server, 80) or they fail. I am using mac and ip configuration coded. I am using the example from the Ethernet library to connect to Google.

Additional information using Web Client example.

If I add the following lines in Setup() I connect but the data I get back is -1.

void setup() {
// disable Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);

I change "c" from char to int o I could see the value of the character coming in.

void loop()
{

// Serial.println("Loop");
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
int c = client.read();
Serial.println(c);
}

Let me know if I should post all the code.

Let me know if I should post all the code.

Yes, you should.

Here is the entire listing - Copied and modified from the Ethernet Examples

/*
Web client

This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.

Circuit:

  • Ethernet shield attached to pins 10, 11, 12, 13

created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe, based on work by Adrian McEwen

*/

#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[] = { 0x00, 0x00, 0x01, 0x03, 0x05, 0x02 };
IPAddress ip(192, 168, 0, 177); // IP address, may need to change depending on network
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[] = { 255, 255, 255, 0 };

//EthernetServer server(80); // create a server at port 80
// 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

// 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(80);

void setup() {
// disable Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);

Serial.begin(9600); // for debugging
Serial.println("Begin Ethernet");
Ethernet.begin(mac, ip); // initialize Ethernet device
Serial.println("Ethernet Began - Now Client begin");

// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");

// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
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()
{

// Serial.println("Loop");
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
int c = client.read();
Serial.println(c);
}

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

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

Simple client test code you can try to see if you get a reply from the server.

//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
//remove SD card if inserted

#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.1"); //download text
    client.println("Host: web.comporium.net");
    client.println("Connection: close");  //close 1.1 persistent connection  
    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

}

Well, I tried your code.

The first time I ran it I got the text from your web site plus a bunch of un-printable characters. So I change the data type of c to int and got a bunch of numbers. Then I changed the type back to char and got a perfectly good output:

Better client test 9/22/12
Send an e in serial monitor to test
connected
HTTP/1.1 200 OK
Date: Wed, 19 Mar 2014 04:56:25 GMT
Server: Apache/2.2.15 (CentOS)
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.

Hmmm very strange. Well I'll see if I can use this against some other sites. Eventually I will try to work in scanning one of the weather sites for local conditions.

Thanks for the help.

Are you sure they were unprintable characters? It wasn't a bunch of characters like this?
ÿÿÿÿÿÿÿÿÿÿ
Funny y = ÿ = 255 = no characters available

If so, that is due to this section of zoomkat's code:

  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 
  }

edit: The correct "Perfect World" version is this.

  while (client.connected()) {
    while(client.available()) {
      char c = client.read(); //gets byte from ethernet buffer
      Serial.print(c); //prints byte to serial monitor 
    }
  }

You can find the "Real World" version here. It has the timeout feature that prevents lockups if the connection breaks (fails) during the download.
http://playground.arduino.cc/Code/WebClient

Thanks. All of this is good.

I now ave my client working well.