client.connect

Hello

I have this code from an exemple

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

byte mac[] = { 0x90, 0xa2, 0xDA, 0x0F, 0x45, 0x1A };
byte ip[] = { 172, 20, 30, 50 };
byte server[] = { 64, 233, 187, 99 }; // Google

EthernetClient client;

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

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(;:wink:
;
}
}

And it dont pass the client.connect

can you help me?

This is not my code, but in my code I have the same problem.

The client only works in Void loop? not in void setup?

I take the code from here:

This is not my code

Then why did you say it was?

I have this code

Make up your mind.

The client only works in Void loop? not in void setup?

There are NO voids in your code. There are FUNCTIONS.

That IP is no longer a Google server. It does not respond.

// replace this
byte server[] = { 64, 233, 187, 99 }; // Google
// with this
byte server[] = {74,125,227,137 }; // Google

PaulS:

This is not my code

Then why did you say it was?

I have this code

Make up your mind.

The client only works in Void loop? not in void setup?

There are NO voids in your code. There are FUNCTIONS.

Thanks for the great help, was amazing how it helped me!

I said that I had this code but was not mine because I am using it like everyone can because it is available on the internet. So I have it on my laptop, But it's not mine because it was not me who created it.
Savvy?

SurferTim:
That IP is no longer a Google server. It does not respond.

// replace this

byte server[] = { 64, 233, 187, 99 }; // Google
// with this
byte server[] = {74,125,227,137 }; // Google

Thank you SurferTim,

I know, I already have done that, but I prefer put here the original code.

I test again with that server that you told me and I only get

"
connecting...
connection failed

disconnecting.
"

Can anyone help me?

Are you certain your network settings are correct? Is this in the range of your localnet?
byte ip[] = { 172, 20, 30, 50 };

edit: This presumes your router IP is 172.20.30.1. Is that the router IP the Arduino is connected to?

SurferTim:
Are you certain your network settings are correct? Is this in the range of your localnet?
byte ip[] = { 172, 20, 30, 50 };

edit: This presumes your router IP is 172.20.30.1. Is that the router IP the Arduino is connected to?

The Ip is ok, I can ping the arduino if I give him that ip add from my laptop.

The router ip is 172.20.10.59

That's just PaulS making sure you know he's smarter than you. It's apparently his primary form of entertainment. He does it constantly.

Placido:

PaulS:

This is not my code

Then why did you say it was?

I have this code

Make up your mind.

The client only works in Void loop? not in void setup?

There are NO voids in your code. There are FUNCTIONS.

Thanks for the great help, was amazing how it helped me!

I said that I had this code but was not mine because I am using it like everyone can because it is available on the internet. So I have it on my laptop, But it's not mine because it was not me who created it.
Savvy?

Then your gateway (router IP) is unreachable (out of range).

The router ip is 172.20.10.59

byte ip[] = { 172, 20, 30, 50 };

If you have 172.20.30.50 assigned to the Arduino without specifying a subnet mask, then the only reachable IPs are 172.20.30.1 to 172.20.30.255.

Yes, I understand.

How can i solve it without changing the ip add?

Thank you! XD

The laptop must have an IP within that range. Look at the network settings for the laptop, and duplicate all but the IP in the Arduino settings. If you have questions about those settings, post them here and I will try to help you.

I will try to do that

Thank you for your time :smiley:

Below is some basic client test code. If it works, then your basic network is working ok and issues are probably in your code setup.

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

}

@zoomkat: Haven't you figured out this is not correct? If the client is still connected and there are no characters available in the socket rx buffer, it will print the funny y (ÿ) on the serial monitor. The client.read() function will return -1 (ÿ) if nothing in the rx buffer.

  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, even if there aren't any and it is ÿ
    Serial.print(c); //prints byte to serial monitor, even if ÿ
  }

It should look like this:

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

@zoomkat: Haven't you figured out this is not correct? If the client is still connected and there are no characters available in the socket rx buffer, it will print the funny y (ÿ) on the serial monitor. The client.read() function will return -1 (ÿ) if nothing in the rx buffer.

I tried the code before I posted it and it worked as expected with my setup. If you got funny characters, then you may have network stability issues that need to be investigated. I think the way my code is written prevents the loss of a socket when an orphan byte is left in the input buffer. YMMV

I tested your code on a busy network with a busy server. No instability issues, just some funny ys in the output when the network and the server are busy. On a localnet with no other server traffic, it seems to work fine as long as the server doesn't stall.

edit: If the connection breaks or the server stalls during the download, your code locks up until you restart the Arduino. :frowning:

SurferTim:
I tested your code on a busy network with a busy server. No instability issues, just some funny ys in the output when the network and the server are busy. On a localnet with no other server traffic, it seems to work fine as long as the server doesn't stall.

edit: If the connection breaks or the server stalls during the download, your code locks up until you restart the Arduino. :frowning:

For this simple test, the server seems fairly robust and I've not needed to add timeout code or other workarounds for bad connections and such. If the posted code does not work for the OP, then maybe he has network issues too.

zoomkat:
If the posted code does not work for the OP, then maybe he has network issues too.

The OP has network issues.

  1. Subnet mask has not been assigned.

  2. A default gateway has not been assigned.

  3. I haven't checked what happens when a subnet mask is not assigned to the shield. If it defaults to 0.0.0.0, it could still respond to pings on the local subnet. The OP has indicated the mask needs to be no more than 19 bits, for the subnet to encompass the router and the IP specified for the Arduino shield.

  4. If you don't set a gateway address, traffic cannot be routed off the local subnet, onto the wide area (Internet). This will definitely stop you accessing a google server.

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

byte mac[] = { 0x90, 0xa2, 0xDA, 0x0F, 0x45, 0x1A };
byte ip[] = { 172, 20, 30, 50 };
byte subnet[] = {255, 255, 224, 0};  // <-- add these
byte gateway[] = {172, 20, 10, 59};  // <-- parameters 
byte dns[] = {8.8.8.8};  //google dns server

byte server[] = { 212, 56, 71, 154 }; // www.google

EthernetClient client;
void setup(void) {
  Ethernet.begin(mac, ip, dns, gateway, subnet); // <--- should fix it
  Serial.begin(9600);
  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");
  }
}
// etc.

Yes, it worked.

Better client test 9/22/12
Send an e in serial monitor to test
connected
HTTP/1.1 200 OK
Date: Thu, 03 Jul 2014 11:55:11 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.
==================

MattS-UK:

zoomkat:
If the posted code does not work for the OP, then maybe he has network issues too.

The OP has network issues.

  1. Subnet mask has not been assigned.

  2. A default gateway has not been assigned.

  3. I haven't checked what happens when a subnet mask is not assigned to the shield. If it defaults to 0.0.0.0, it could still respond to pings on the local subnet. The OP has indicated the mask needs to be no more than 19 bits, for the subnet to encompass the router and the IP specified for the Arduino shield.

  4. If you don't set a gateway address, traffic cannot be routed off the local subnet, onto the wide area (Internet). This will definitely stop you accessing a google server.

  1. If you do not assign a subnet mask, the library uses 255.255.255.0 by default.
  2. If you do not assign a dns server or gateway, the library uses the assigned IP first three octets and sets the last octet to .1 by default.

edit: It is probably a network setting problem as I stated in replies #5 and #8.