EthernetDNS problems

Ok, so I am at a loss. I am trying to get the EthernetDNS example sketches to work (gkaindl.com → software → arduino ethernet), and I just can’t seem to get it up an running. I have made no changes to the code other than plus in the values for my network. I know these values work as I have used NeoCats Twitter library (which also uses the EthernetDNS library) with great success. This is another reason why I can’t figure out why I cant get the example sketch working. EthernetDNS works inside of the Twitter library, what am I doing wrong?

Here is the source code. as far as I can tell, I am just supposed to type in a web address in the serial monitor and it should return it’s ip address. All it gives me is ‘Does not exist’. I am pretty sure that google.com exists. I am using a cradlepoint router and virgin mobile 3g broadband card for my internet connection if that matters at all.

#if defined(ARDUINO) && ARDUINO > 18
#include <SPI.h>
#endif
#include <Ethernet.h>
#include <EthernetDNS.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// substitute an address on your own network here
byte ip[] = { 192, 168, 0, 31 };
// substitute your DNS server ip address
byte dnsServerIp[] = { 192, 168, 0, 1};

// NOTE: Alternatively, you can use the EthernetDHCP library to configure your
//       Ethernet shield and find out the DNS server IP address.

const char* ip_to_str(const uint8_t*);

void setup()
{
  Ethernet.begin(mac, ip);
  
  Serial.begin(9600);
  Serial.println("Enter a host name via the Arduino Serial Monitor to have it "
                 "resolved.");
  
  // You will often want to set your own DNS server IP address (that is
  // reachable from your Arduino board) before doing any DNS queries. Per
  // default, the DNS server IP is set to one of Google's public DNS servers.
  EthernetDNS.setDNSServer(dnsServerIp);
}

void loop()
{ 
  char hostName[512];
  int length = 0;
  
  while (Serial.available()) {
    hostName[length] = Serial.read();
    length = (length+1) % 512;
    delay(50);
  }
  hostName[length] = '\0';
  
  if (length > 0) {
    
    byte ipAddr[4];
    
    Serial.print("Resolving ");
    Serial.print(hostName);
    Serial.print("...");
    
    // Let's send our DNS query. If anything other than DNSSuccess is returned,
    // an error has occurred. A full list of possible return values is
    // available in EthernetDNS.h
    DNSError err = EthernetDNS.sendDNSQuery(hostName);

    if (DNSSuccess == err) {
      do {
        // This will not wait for a reply, but return immediately if no reply
        // is available yet. In this case, the return value is DNSTryLater.
        // We can use this behavior to go on with our sketch while the DNS
        // server and network are busy finishing our request, rather than
        // being blocked and waiting.
        err = EthernetDNS.pollDNSReply(ipAddr);
			
        if (DNSTryLater == err) {
          // You could do real stuff here, or go on with a your loop(). I'm
          // just printing some dots to signal that the query is being
          // processed.
          delay(20);
          Serial.print(".");
        }
      } while (DNSTryLater == err);
    }

    Serial.println();

    // Finally, we have a result. We're just handling the most common errors
    // here (success, timed out, not found) and just print others as an
    // integer. A full listing of possible errors codes is available in
    // EthernetDNS.h
    if (DNSSuccess == err) {
      Serial.print("The IP address is ");
      Serial.print(ip_to_str(ipAddr));
      Serial.println(".");
    } else if (DNSTimedOut == err) {
      Serial.println("Timed out.");
    } else if (DNSNotFound == err) {
      Serial.println("Does not exist.");
    } else {
      Serial.print("Failed with error code ");
      Serial.print((int)err, DEC);
      Serial.println(".");
    }
  }  
}

// Just a utility function to nicely format an IP address.
const char* ip_to_str(const uint8_t* ipAddr)
{
  static char buf[16];
  sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
  return buf;
}

I tried the code and it works. I did change the ip addresses below to match my netgear router ip addressing setup.

// substitute an address on your own network here
byte ip[] = { 192, 168, 1, 102 };
// substitute your DNS server ip address
byte dnsServerIp[] = { 192, 168, 1, 1};

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

That won't be your DNS server address will it? That is a private address.

Most routers act as DNS servers (well forwarders anyway). This can be verified by the DNS server address the router's DHCP system doles out to your computer.

Yeah I was under the impression that I could use the routers address for a DNS server. But being that it is a 3g router, I even tried Googles 8.8.8.8 DNS server with the same results.

The below works for me using the google DNS server.

#if defined(ARDUINO) && ARDUINO > 18
#include <SPI.h>
#endif
#include <Ethernet.h>
#include <EthernetDNS.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// substitute an address on your own network here
byte ip[] = { 192, 168, 1, 102 };
// substitute your DNS server ip address
byte dnsServerIp[] = { 8, 8, 8, 8 };

// NOTE: Alternatively, you can use the EthernetDHCP library to configure your
//       Ethernet shield and find out the DNS server IP address.

const char* ip_to_str(const uint8_t*);

void setup()
{
  Ethernet.begin(mac, ip);
  
  Serial.begin(9600);
  Serial.println("Enter a host name via the Arduino Serial Monitor to have it "
                 " resolved.");
  
  // You will often want to set your own DNS server IP address (that is
  // reachable from your Arduino board) before doing any DNS queries. Per
  // default, the DNS server IP is set to one of Google's public DNS servers.
  EthernetDNS.setDNSServer(dnsServerIp);
}

void loop()
{ 
  char hostName[512];
  int length = 0;
  
  while (Serial.available()) {
    hostName[length] = Serial.read();
    length = (length+1) % 512;
    delay(50);
  }
  hostName[length] = '\0';
  
  if (length > 0) {
    
    byte ipAddr[4];
    
    Serial.print("Resolving ");
    Serial.print(hostName);
    Serial.println("...");
    
    // Resolve the host name and block until a result has been obtained.
    // This means that the call will not return until a result has been found
    // or the query times out. While it is less effort to write a query this
    // way, the problem is that the whole sketch will "hang", which might not
    // be what you want. If you want to retain control over the sketch while
    // the query is being processed, check out the PollingDNS example.
    DNSError err = EthernetDNS.resolveHostName(hostName, ipAddr);
    
    // Finally, we have a result. We're just handling the most common errors
    // here (success, timed out, not found) and just print others as an
    // integer. A full listing of possible errors codes is available in
    // EthernetDNS.h
    if (DNSSuccess == err) {
      Serial.print("The IP address is ");
      Serial.print(ip_to_str(ipAddr));
      Serial.println(".");
    } else if (DNSTimedOut == err) {
      Serial.println("Timed out.");
    } else if (DNSNotFound == err) {
      Serial.println("Does not exist.");
    } else {
      Serial.print("Failed with error code ");
      Serial.print((int)err, DEC);
      Serial.println(".");
    }
  }  
}

// Just a utility function to nicely format an IP address.
const char* ip_to_str(const uint8_t* ipAddr)
{
  static char buf[16];
  sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
  return buf;
}