Problems Using String/URL as Server Address [SOLVED]

Hey

I'm having a problem using a String as the URL (in place of an IP address) when connecting to a server. For example,

#include <SPI.h>
#include <Ethernet.h>
#include <Client.h>
#include <Server.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x89, 0xC9 };
byte ip[] = { 192, 168, 0, 14 };
char server[] = "www.wetdreams.org.uk";

But the following code always prints out the 'Failed Connection #2" string -

if(client.connect(server, 80)){
  Serial.println("connected");
  client.println("GET /UI/get_test.php?input_val=45 HTTP/1.0");
  client.println("HOST: www.wetdreams.org.uk");
  client.println();
}
else{
 Serial.println("Failed connection #2"); 
}

Having looked at the Arduino site, I don't think char[]'s can be used as an input parameter to client.connect(), however I saw some code on here cited as working that used a string, like what I have.

The issue that I have is that I can't use an IP address to connect to the server* (I have no idea why) so I'm trying a URL in its place, so in the event that a String can't be used, does anybody know of a workaround where I don't have to use the IP?

  • ping www.wetdreams.org.uk returns 193.238.80.90, but when you visit that IP, it isn't resolving to a URL. I have FTP access but nothing more, so don't know what to do.

Ps: The site is a kayaking website, but the URL alone might make it NSFW

How are you initializing the ethernet shield? Insure you use a valid dns server ip in the setup().

// your network setup stuff
IPAddress ip(192,168,0,14);
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0);

// the dns ip must be a valid dns server ip
IPAddress dns(192,168,0,1);

// then in the setup() function
   Ethernet.begin(mac,ip,dns,gateway,subnet);

sure you can use host name instead of ip address to connect.

if you look at source code for client.connect, your dns lookup failed

int EthernetClient::connect(const char* host, uint16_t port) {
  // Look up the host first
  int ret = 0;
  DNSClient dns;
  IPAddress remote_addr;

  dns.begin(Ethernet.dnsServerIP());
  ret = dns.getHostByName(host, remote_addr);
  if (ret == 1) {
    return connect(remote_addr, port);
  } else {
    return ret;
  }
}

if you used dhcp, the ethernet library will use the dns server returned by your dhcp server.
if you used static ip and did not explicitly set the dns server, it will use the same ip as your static ip but replace the last number with 1 (which usually defaults to the router/gateway).

Hey guys!

Thanks very much, working now - updating my MySQL DB from my Arduino :wink:

Thanks to 'doughboy' for the explanation and to 'SurferTim' for the solution.

New code;

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x89, 0xC9 };
//the IP address for the shield:
byte ip[] = { 192, 168, 0, 14 };
// the router's gateway address:
byte gateway[] = { 192, 168, 0, 1 };
// the subnet:
byte subnet[] = { 255, 255, 255, 0 };

byte dns[] = { 192, 168, 0, 1};

/* Wetdreams.org.uk - 193.238.80.90 */
char server[] = "www.wetdreams.org.uk";

EthernetClient client;

and

void setup()
{
  Serial.begin(9600);
// initialize the ethernet device
Ethernet.begin(mac, ip, dns, gateway, subnet); //just use the default gateway just now

//analogReference(INTERNAL1V1);

delay(1000);
Serial.println("Connecting.....");

if(client.connect(server, 80)){
  Serial.println("connected");
  client.println("GET **get request to DB** HTTP/1.0");
  client.println("HOST: www.wetdreams.org.uk");
  client.println();
}
else{
 Serial.println("Failed connection #2"); 
}

}

I've worked out what my problem was, I previously had a fairly complex Arduino WebServer (with JQuery and everything!) running, so when I went to experiment with the client here, I just copied my code across. Obviously the Client needs to go through the DHCP rigmarole whereas the Server just needs to go as far as the router. My Comms Networks professor wouldn't be happy with that mistake :wink:

Thanks for the help guys, I'm going to mark this as resolved and we can all move on with our lives.