[Resolved] Ethernet Board, client.connect(URL, port) only works with IP?

Hi,

I've looked everywhere but I can't find someone with the same issue. Some people had this problem but it was because they didn't specified a host in their header. Is it something that I'm missing?

Basically, the ethernet library with the ethernet board doesn't seem to accept a URL (char array) in the method connect(); It works fine with an IP.

Here's a piece of code (the webclient example):

#include <SPI.h>

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>

/*
  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
 modified 9 Apr 2012
 by David A. Mellis
 
 */

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(173,194,41,184); // google.co.uk
IPAddress serverMM(69,89,31,106);
char serverName[] = "www.google.co.uk";  // google URL

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

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // 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(serverName, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
     client.println("GET /search?q=arduino HTTP/1.1");
   
    //client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com");
    //client.println("Connection: close");
    client.println();
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
    
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

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

    // do nothing forevermore:
    for(;;)
      ;
  }

  
}

When using a URL instead of an IP, the program just hang a the client.connect().

I've tried so many combination, even thought that maybe the example was missing libraries to enable URLs. If any one the answer that would be great, otherwise could that will be a bug I will report.

Cheers!

Client test code that uses a URL. Note that the HTTP:// part is not used.

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

}

Are you sure that your DHCP host provides a valid nameserver from which it can request the IP of that hostname?

Thanks Guys for the quick reply.

@ zoomkat: Trying it now and I will report back.

@ JohnHoward: I'm testing with google, bbc, etc... so that shouldn't be an issue.

Hi ZoomKat,

I've you tested your code recently? This code doesn't work on my side.
I don't see any differences with the Webclient client example I posted (in terms of using the client.connect()).

Cheers

Ok, that's weird and I don't think I saw any mention of this on the documentation of the Ethernet Lib.

I saw in your comments that the SD card needed to be removed. Which I did, and now it works.

I don't really understand it. Any idea?

I understand. You must disable or initialize the SD or it will trash up the SPI bus. Try this in your setup function. It disables the SD SPI. You should be able to insert the SD card again and it will work.

void setup() {
  Serial.begin(9600);

  // disable SD SPI
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);

  // now do the rest of your setup stuff.
}

SurferTim, you're the man!

I should have read the SD library doc in more details and realise that there's both using SPI communication.

So if I want to use the SD card to store information, I would need to switch on and off pin 4:

1/ switch off pin 4
2/ HTTP request
3/ store response in memory
4/ switch on pin 4
5/ write to card
(back to step 1)

Cool, I hope this will also help others. ( I can't believe how much time I wasted because of this).

Cheers!

You can use both devices without manipulating the slave select lines for each device after you get them initialized. That is accomplished in the low level read and write functions in the respective libraries. Here is a sketch I use to transfer files to and from my Arduino SD card. It uses both the w5100 and the SD card together. It shows how to initialize both so they both work.
http://playground.arduino.cc/Code/FTP

I'm so happy about this I've got to thank you guys again. That was driving me insane! It's one of those things, it's right in front of you but you've been looking at it for long that you can't even see it.

Thank you!

Cheers SurferTim,

I will check it out when I get to this part in my project. I will need some time to get my head around the code you putted on Playground.

Again, thank you very much for your help.