Example of EthernetClient from Arduino refeferences

Hi,

I tried in the last couple of hours to send an e-mail using my arduino. Without any success, I tried to come back and make something basic : Upload an example sketch from arduino to see if my client.connect
(if (client.connect(server, 80)) )would work. This is where my last code to send an email would not connect, never.

After uploading this sketch, client.connect(server.80) would still not work, any idea why?

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


byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
byte ip[] = { 208, 92, 17, 232 };
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(;;)
      ;
  }
}

First, I upload the sketch DHCP adress printer with mac = 00,aa,bb,cc,de,02 and it would give me the IP 208.92.17.232.

These are the only informations I change from the example.

Is there something wrong am I doing?

Simple client code with DNS you can try to see if your ethernet shield is working. Also remove SD card if inserted.

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

}

Is there something wrong am I doing?

One thing is assuming that the IP address that DHCP assigned is still available. You should not do that.

Have you verifies that the Google IP address IS correct? I'm sure Google gets tired of being everyone's guinea pig.

IPAddress server( 74, 125, 227, 16); // Google

This worked the last time I tried running a client (12/15/2012).

Thank you zoomkat, your code worked well. First I had my SD card plugged-in and it wasnt working. After taking it out, everything work fine...why??

PaulS: What do you mean by is still available? You should not do that?

This worked well for all of my other projects...

I'll check on that for google...

Just to be safe, I change the Ip of google to 74.125.224.72. This is a working one.

If I removed the mac adress, everything is working fine! Why?

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


//byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
byte ip[] = { 208, 92, 17, 232 };
byte server[] = { 74, 125, 224, 72 }; // Google

EthernetClient client;

void setup()
{
  Ethernet.begin(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(;;)
      ;
  }
}

johnyboy: Thank you zoomkat, your code worked well. First I had my SD card plugged-in and it wasnt working. After taking it out, everything work fine...why??

You need to add code like below if you will be having a SD card inserted to prevent a conflict between the SD card and ethernet shield.

void setup(){

  Serial.begin(9600);

  // disable w5100 while setting up SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  Serial.print("Starting SD..");
  if(!SD.begin(4)) Serial.println("failed");
  else Serial.println("ok");

  Ethernet.begin(mac, ip, gateway, gateway, subnet);

  //delay(2000);
  server.begin();
  Serial.println("Ready");