Project

General

Profile

Arduino Ethercard - Hostname not resolving to ip address using ether.dnsLookup

Added by autefrum over 2 years ago

Hi,

i want to use an Arduino as a type of datalogger. For example, every 5 minutes send the read from a temperature sensor to data.sparkfun.com

I bought an ethernet card (http://www.jaycar.com.au/arduino-compatible-ethernet-interface-module/p/XC4436) which I got working (using the jeelabs ethercard example projects) as a web server, serving a page to my laptop browser, and it responds to pings.

However, I can't get it to resolve a host name to an IP address.

I use my modem/router as a DNS forwarder, and this works for all other devices on my network. I also tried using 8.8.8.8 as the DNS server but neither works.

The ether.dnsLookup(hostname) below returns false, and the hisip is set to 0.0.0.0. The print statements all return what I expect - the device's IP, subnet and gateway are set well.

Is there something fundamental I am missing?

here is my code:

#include <EtherCard.h>
#include "DHT.h"
#define DHTPIN 4     // what pin we're connected to
#define DHTTYPE DHT11   // DHT 11 gives reasonable temperature and humidity figures.
#define STATIC 1  // set to 1 to disable DHCP (adjust myip/gwip values below)

#if STATIC
static byte myip[] = { 172,16,97,222 };// ethernet interface ip address
static byte gwip[] = { 172,16,97,1 };// gateway ip address
//static byte dnsip[] = { 8, 8, 8, 8 };
static byte dnsip[] = { 172, 16, 97, 1 };
static byte mask[] = { 255,255,0,0 };
#endif

// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[700]; // tcp/ip send and receive buffer
static uint32_t timer;
DHT dht(DHTPIN, DHTTYPE);
const char hostname[] = "data.sparkfun.com";
static void my_callback (byte status, word off, word len) {
  Serial.println(">>>");
  Ethernet::buffer[off+300] = 0;
  Serial.print((const char*) Ethernet::buffer + off);
  Serial.println("...");
}
void setup(){
  Serial.begin(9600);
  dht.begin(); // start up temperature and humidity sensor
  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
  Serial.println( "Failed to access Ethernet controller");

  ether.staticSetup(myip, gwip, dnsip, mask);
  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);
  //char *dtostrf(double val, signed char width, unsigned char prec, char *s)
//static byte newdnsip[] = {8,8,8,8}; // 
static byte newdnsip[] = {172,16,97,1};
ether.copyIp(ether.dnsip, newdnsip);
ether.printIp("DNS IP: ", ether.dnsip);
  Serial.print("Looking up ");
  Serial.println(hostname);
// fails here - dnsLookup returns false:
  if (!ether.dnsLookup(hostname))
    Serial.println("DNS failed");
  ether.printIp("SRV: ", ether.hisip);
}

void loop(){
  ether.packetLoop(ether.packetReceive());

  if (millis() > timer) {
    Serial.println("Inside timer loop");
    timer = millis() + 5000;
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    char tempCharBuffer[5]="\0\0\0\0";
    char humidityCharBuffer[5]="\0\0\0\0";
    dtostrf(t,4,1, tempCharBuffer);
    dtostrf(h,4,1, humidityCharBuffer);
    String url = String("/input/7JOvr3rb7wsRRyKGKvZa?private_key=mzZqeWeCHANGED7jN&humidity=");
    char urlBuffer[200];
    url.concat(humidityCharBuffer);
    url.concat("&motion=0&temp=");
    //url.concat(tempCharBuffer);
    url.toCharArray(urlBuffer,200);
    Serial.println("");
    Serial.print("url:'");
    Serial.print(urlBuffer);
    Serial.println("'");
    Serial.println();
    Serial.print("<<< REQ ");

    ether.browseUrl((char *)urlBuffer, tempCharBuffer, hostname, my_callback);
  }
}

Replies (1)

RE: Arduino Ethercard - Hostname not resolving to ip address using ether.dnsLookup - Added by JohnO over 2 years ago

Hi autefrum,

I don't think that we have many people using the Ethernet library. Personally I use a Raspberry Pi to interface with the Internet.

    (1-1/1)