Failed to Configure Ethernet Using DHCP: Please Help

Hello,

I'm using my arduino Uno and a "RioRand (TM) Upgraded Ethernet Shield W5100 for Arduino UNO R3 & MEGA 2560 Duemilanove" with no SD card inserted. My goal is to read some raw surf data (swell height and angle ect.) from cdip.ucssd.edu. The website has a very friendly text interface that I am hoping to read data from: http://cdip.ucsd.edu/data_access/synopsis_pm.cdip. However, I cannot seem to connect to the site no matter what I try. I have tested my arduino using 2-3 different example codes and I am never successful in configuring Ethernet using DHCP. I will appreciate any help, but the quicker the better because this project is due in less than a week. Thank you!

This is my sketch:

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

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//IPAddress ip(192, 177, 1, 130);        //orig 118 at end

// Where Data comes from
char server[] = "cdip.ucsd.edu";
//byte server[] = {169,228,225,137};
String dataLocation = "/data_access/synopsis_pm.cdip HTTP/1.1";

// Initialize Ethernet client
EthernetClient client;
String currentLine = "";            // string for incoming serial data
String currSwell = "";
boolean readingSwell = false;       // is reading?
const int requestInterval = 900000; // ms delay between requests (15 min) 

// How often to look for new data
boolean requested;                  // whether I've made a request since conneting
long lastAttemptTime = 0;           // last time I connected to the server in ms

float Day = 0;
float Hr = 0;
float Minu = 0;
float DayUTC = 0;
float HrUTC = 0;
float MinuUTC = 0;
float HS = 0;
float TP = 0;
float DP = 0;
float TEMP = 0;

// Function prototypes
void connectToServer();
float strToFloat(String);
void sendGET();

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  // Start Ethernet Connection
  if (Ethernet.begin(mac) == 0)
  {
    // game over
    Serial.println("Failed to configure Ethernet using DHCP");
  }

  // Connect to server
  connectToServer();
  
}

void loop() {
  // Checking for new data
  if (client.connected())
  {
    if (client.available())
    {
      // read incoming bytes:
      char inChar = client.read();

      // add incoming byte to end of line:
      currentLine += inChar;
      Serial.println(inChar);

      // if I get a new line, clear the line:
      if (inChar == '\n') 
      {
        currentLine = "";
      }

      if (currentLine.endsWith("076 DIABLO CANYON, CA         ")) // beginning characters
      {
        readingSwell = true;
      }
      else if (readingSwell) 
      {
        if (!inChar == '\n') //(!currentLine.endsWith("\n")) // ending characters
        { 
          currSwell += inChar;
        }
        else 
        {
          readingSwell = false;
          String justSwell = currSwell.substring(0, currSwell.length());
          
          // Split justRates
          int firstSpaceIndex = justSwell.indexOf(" ");
          int secondSpaceIndex = justSwell.indexOf(" ", firstSpaceIndex+4);
          int thirdSpaceIndex = justSwell.indexOf(" ", secondSpaceIndex+1);
          int fourthSpaceIndex = justSwell.indexOf("  ", thirdSpaceIndex+4);
          int fifthSpaceIndex = justSwell.indexOf(" ", fourthSpaceIndex+2);
          int sixthSpaceIndex = justSwell.indexOf(" ", fifthSpaceIndex+2);
          int seventhSpaceIndex = justSwell.indexOf("       ", sixthSpaceIndex+2);
          
          String day = justSwell.substring(0, firstSpaceIndex);
          String hr = justSwell.substring(firstSpaceIndex+1, firstSpaceIndex+3);
          String minu = justSwell.substring(firstSpaceIndex+4, secondSpaceIndex);
          String dayUTC = justSwell.substring(secondSpaceIndex+1, thirdSpaceIndex);
          String hrUTC = justSwell.substring(thirdSpaceIndex+1, thirdSpaceIndex+3);
          String minuUTC = justSwell.substring(thirdSpaceIndex+4, fourthSpaceIndex);
          String Hs = justSwell.substring(fourthSpaceIndex+1, fifthSpaceIndex);
          String Tp = justSwell.substring(fifthSpaceIndex+1, sixthSpaceIndex);
          String Dp = justSwell.substring(sixthSpaceIndex+1, seventhSpaceIndex);
          String Temp = justSwell.substring(seventhSpaceIndex+1);

          // Convert strings to floats and store
          Day = strToFloat(day);
          Hr = strToFloat(hr);
          Minu = strToFloat(minu);
          DayUTC = strToFloat(dayUTC);
          HrUTC = strToFloat(hrUTC);
          MinuUTC = strToFloat(minuUTC);
          HS = strToFloat(Hs);
          TP = strToFloat(Tp);
          DP = strToFloat(Dp);
          TEMP = strToFloat(Temp);
        }
      }

      // Reset for next reading
      currSwell = "";
      client.stop();

    }
    else if (millis() - lastAttemptTime > requestInterval)
    {
      // if you're not connected, and requestInterval has passed since
      // your last connection, then attempt to connect again:
      connectToServer();
    }
    Serial.println(DP);
  }
}

// Connect to server
void connectToServer()
{
  // attempt to connect, and wait a millisecond
  Serial.println("connecting to server...");
  if (client.connect(server,80))
  {
    Serial.println("making HTTP request...");

    // make HTTP GET request to dataLocation:
    client.println("GET " + dataLocation);
    client.println("Host: cdip.ucsd.edu");
    client.println();
  }
  
  // note the time of the connect attempt
  lastAttemptTime = millis();
}

float strToFloat(String str) 
{
  char carray[str.length() + 1];           // determine size of array
  str.toCharArray(carray, sizeof(carray)); // put str into an array
  return atof(carray);
}

OGShaneTrain:
This is my sketch:

//IPAddress ip(192, 177, 1, 130);        //orig 118 at end

Why is this commented out?

I didn't think I needed that. When I was writing the script I was copying a lot from some other code and I pasted that in there in case I needed it.

It is not clear exactly what your network looks like. To have something get "configured with DHCP" requires something that your hardware is talking to that is a DHCP server and is able to hand out an IP address etc. Are you going through a home router (with DHCP enabled) or ? Does your board get any IP address at all ? DHCP should be handing out the IP address, gateway etc. If you are not getting a valid IP address for your network, you will never connect to anything else. First thing is to make sure it is getting a valid address (or you are configuring it for a static address, but if you do that make sure you are not picking the same address as something else on the same network).

Ok. I uncommented the line that declares the IP address. The first 3 numbers are the same as my router and the 4th number is unique. I am still not able to connect though.

Alright so here is just the setup loop of my script. It attempts to connect to the Ethernet using DHCP and if (when) that fails it attempts to configure using IP Address instead, which also fails.

Setup loop and configuration attempt (ignore the extra variables):

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

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 177, 1, 130);        //orig 118 at end

// Where Data comes from
char server[] = "cdip.ucsd.edu";
String dataLocation = "/data_access/synopsis_pm.cdip HTTP/1.1";

// Initialize Ethernet client
EthernetClient client;
String currentLine = "";            // string for incoming serial data
String currSwell = "";
boolean readingSwell = false;       // is reading?
const int requestInterval = 900000; // ms delay between requests (15 min) 

// How often to look for new data
boolean requested;                  // whether I've made a request since conneting
long lastAttemptTime = 0;           // last time I connected to the server in ms

float Day = 0;
float Hr = 0;
float Minu = 0;
float DayUTC = 0;
float HrUTC = 0;
float MinuUTC = 0;
float HS = 0;
float TP = 0;
float DP = 0;
float TEMP = 0;

// Function prototypes
void connectToServer();
float strToFloat(String);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  // Start Ethernet Connection
  if (Ethernet.begin(mac) == 0)
  {
    // game over
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }

  // give the Ethernet shield a second to initialize:
  delay(3000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    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");
  } 
}

And here is the output I get to my serial monitor:

Failed to configure Ethernet using DHCP
connecting...
connection failed

disconnecting.

I still don't understand what the issue could be. Im very new to the ethernet shield and library. Could it be that the website I'm trying to connect with is not configured in a way that allows my Arduino to talk to it? Thanks for the help so far.

I modified some client test code I have and it connects and downloads some of the data. You can try the below code unmodified to see if you can connect to the site.

//zoomkat 11-04-13
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test client GET
//for use with W5100 based ethernet shields
//remove SD card if inserted
//data from myIP server captured in readString 

#include <SPI.h>
#include <Ethernet.h>
String readString;

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

char serverName[] = "cdip.ucsd.edu"; // myIP server 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("client readString test 11/04/13"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
  Serial.println();
}

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 /data_access/synopsis_pm.cdip HTTP/1.1"); //download text
    client.println("Host: cdip.ucsd.edu");
    client.println("Connection: close");  //close 1.1 persistent connection  
    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
    readString += c; //places captured byte in readString
  }

  //Serial.println();
  client.stop(); //stop client
  Serial.println("client disconnected.");
  Serial.println("Data from server captured in readString:");
  Serial.println();
  Serial.print(readString); //prints readString to serial monitor 
  Serial.println();  
  Serial.println();
  Serial.println("End of readString");
  Serial.println("==================");
  Serial.println();
  readString=""; //clear readString variable

}

Thanks a lot zoomkat. You saved my ass for this lab report I have due on Friday. I ran your code unmodified and was able to read data for the first 10 buoys or so. I dont understand what I was doing inorrect, but at least I will be able to get this done.

Below is some very old buoy data code that may not work now (from the below site), but I counted the line feeds and captured the first line which contained the desired data. You may try the same to get what you want.

140.90.238.27/data/5day2/44013_5day.txt

//zoomkat 12-22-10
//simple ethernet client test code
//for use with IDE 0021 and W5100 ethernet shield
//modify the arduino lan ip address as needed
//open serial monitor to see what the arduino receives
//push the shield reset button to run client again

#include <SPI.h>
#include <Ethernet.h>
String readString, readString1;
int x=0;
char lf=10;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 102 };
byte server[] = { 140, 90, 238, 27 }; // NOAA

EthernetClient client; //(server, 80);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  Serial.println("starting simple arduino client test");
  Serial.println();
  Serial.println("connecting...");

  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /data/5day2/44013_5day.txt HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    //Serial.print(c);  // uncomment to see raw feed
    if (c==lf) x=(x+1);
    if (x==14) readString += c;
    //readString += c;
  }

  if (!client.connected()) {
     client.stop();

    Serial.println("Current data row:" );
    Serial.print(readString);
    Serial.println();
    readString1 = (readString.substring(41,43));
    Serial.println();
    Serial.print("DPD sec: ");
    Serial.println(readString1);
    Serial.println("done");

    for(;;);

    }
 }

Thanks! That will come in handy as well. Do you know what it is only downloading part of the text file? It isn't downloading enough data to read the buoy I'm interested in, buoy 076 by Diablo Canyon. Could it be limited memory space?

Could it be limited memory space?

Probably, that is why you may want to count lines received until you get to the desired line, then capture that line (check the second code posted).

Below is the first code I posted with the line counting added. Once the desired line is captured you can use the various String functions to get the desired data from it.

Edit, added x=0; to reset counter.

//zoomkat 11-04-13
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test client GET
//for use with W5100 based ethernet shields
//remove SD card if inserted
//data from myIP server captured in readString 

#include <SPI.h>
#include <Ethernet.h>
String readString;

int x=0;
char lf=10;

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

char serverName[] = "cdip.ucsd.edu"; // myIP server 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("client readString test 11/04/13"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
  Serial.println();
}

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.
{
x=0;
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /data_access/synopsis_pm.cdip HTTP/1.1"); //download text
    client.println("Host: cdip.ucsd.edu");
    client.println("Connection: close");  //close 1.1 persistent connection  
    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
    
    if (c==lf) x=(x+1);
    if (x==42) readString += c;
    //readString += c; //places captured byte in readString
    
  }

  //Serial.println();
  client.stop(); //stop client
  Serial.println("client disconnected.");
  Serial.println("Data from server captured in readString:");
  Serial.println();
  Serial.print(readString); //prints readString to serial monitor 
  Serial.println();  
  Serial.println();
  Serial.println("End of readString");
  Serial.println("==================");
  Serial.println();
  readString=""; //clear readString variable

}

I didn't see that last post until right now, but thanks! I finally got it to work! It took me forever because this crappy Ethernet shield I bought refuses to connect for like 20 minutes after I upload a new script. But here is the final code for anybody who is interested. It pulls swell angle, height, and period every minute from the Diablo Canyon buoy in CA and analyzes the surf conditions. The conditions are displayed via a tricolor led. Red is bad, blue is average, and green is firing! Thanks for all the help zoomkat.

#include <SPI.h>
#include <Ethernet.h>
#define red 6
#define green 5
#define blue 3

String readString;
int x = 0;
float Day = 0;
float Hr = 0;
float Minu = 0;
float DayUTC = 0;
float HrUTC = 0;
float MinuUTC = 0;
float HS = 300;
float TP = 14;
float DP = 275;
float TEMP = 0;
int greenPWM = 0;
int bluePWM = 0;
int redPWM = 0;
boolean readSwell = false;       // success?

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

char serverName[] = "cdip.ucsd.edu"; // myIP server test web page server
EthernetClient client;

// function prototypes
float strToFloat(String str);
void sendGET();
void ledPWM();

//////////////////////

void setup(){
  
  //client.println("Connection: close");  //close 1.1 persistent connection  
  //client.println(); //end of get request
  //client.stop(); //stop client
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  //pinmode
  pinMode(green,OUTPUT);
  pinMode(red,OUTPUT);
  pinMode(blue,OUTPUT);

  Serial.begin(9600);
  Serial.println("Initialized"); 
  //Serial.println("client readString test 11/04/13"); // so I can keep track of what is loaded
  //Serial.println("Send an e in serial monitor to test"); // what to do to test
  Serial.println();
}

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
    }
  }  
  */
  
  if (!readSwell) sendGET(); // if unsuccessful connection, try again
  else 
  {
    delay(30000);         // if successful connection, wait 1 minute
    readSwell = false;
  }

  // LED color specification
  ledPWM();
  analogWrite(green,greenPWM);
  analogWrite(red,redPWM);
  analogWrite(blue,bluePWM);
} 

//////////////////////////

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 /data_access/synopsis_pm.cdip HTTP/1.1"); //download text
    client.println("Host: cdip.ucsd.edu");
    client.println("Connection: close");  //close 1.1 persistent connection  
    client.println(); //end of get request
    readSwell = true;
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
    readSwell = false;
  }

  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); //see raw output
    if (c == '\n')
    {
      x++;
    }
    
    if (x == 42)
    {
      readString += c;
    }
    //delay(10); // helpful when looking at raw output
  }

  // parse out string
  String day = readString.substring(31, 33);
  String hr = readString.substring(34, 36);
  String minu = readString.substring(36, 38);
  String dayUTC = readString.substring(39, 41);
  String hrUTC = readString.substring(42, 44);
  String minuUTC = readString.substring(44, 46);
  String Hs = readString.substring(48, 51);
  String Tp = readString.substring(52, 56);
  String Dp = readString.substring(57, 60);
  String Temp = readString.substring(67, 71);

  // Convert strings to floats and store
  Day = strToFloat(day);
  Hr = strToFloat(hr);
  Minu = strToFloat(minu);
  DayUTC = strToFloat(dayUTC);
  HrUTC = strToFloat(hrUTC);
  MinuUTC = strToFloat(minuUTC);
  HS = strToFloat(Hs);
  TP = strToFloat(Tp);
  DP = strToFloat(Dp);
  TEMP = strToFloat(Temp);

  //Serial.println();
  client.stop(); //stop client
  Serial.println("client disconnected.");
  Serial.println("Data from server captured in readString:");
  Serial.println();
  Serial.print(readString); //prints readString to serial monitor 
  Serial.println();  
  Serial.println("End of readString");
  Serial.println("==================");
  Serial.println();  
  Serial.println("Wave Parameters for Diablo Canyon, CA:");
  Serial.println();
  Serial.print("Day = ");
  Serial.println(day);
  Serial.print("Time = ");
  Serial.print(hr);
  Serial.print(":");
  Serial.println(minu);
  Serial.print("Hs = ");
  Serial.println(Hs);
  Serial.print("Tp = ");
  Serial.println(Tp);
  Serial.print("Dp = ");
  Serial.println(Dp);
  Serial.print("Temp = ");
  Serial.println(Temp);
  Serial.println();
  Serial.println("End of Wave Parameters");
  Serial.println("==================");
  Serial.println();
  Serial.println("end");
  Serial.println();
  Serial.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  Serial.println();
  Serial.println();
  readString=""; //clear readString variable
  x = 1;         //reset x
}

//string 2 float
float strToFloat(String str) 
{
  char carray[str.length() + 1];           // determine size of array
  str.toCharArray(carray, sizeof(carray)); // put str into an array
  return atof(carray);
}

void ledPWM()
{
  if (DP >= 180 && DP < 270)
  {
    if (HS > 213.36 && TP > 15)
    {
      greenPWM = 200;
      bluePWM = 0;
      redPWM = 0;
    }
    else if (HS > 304.8 && TP > 17)
    {
      greenPWM = 0;
      bluePWM = 200;
      redPWM = 0;
    }
    else
    {
      greenPWM = 0;
      bluePWM = 0;
      redPWM = 200;
    }
  }
  else if (DP >= 270 && DP < 295)
  {
    if (HS > 152.4 && TP > 13)
    {
      greenPWM = 200;
      bluePWM = 0;
      redPWM = 0;
    }
    else if (HS > 213.36 && TP > 15)
    {
      greenPWM = 0;
      bluePWM = 200;
      redPWM = 0;
    }
    else
    {
      greenPWM = 0;
      bluePWM = 0;
      redPWM = 200;
    }
  }
  else if (DP >= 295 && DP < 305)
  {
    if (HS > 91.44 && TP > 13)
    {
      greenPWM = 200;
      bluePWM = 0;
      redPWM = 0;
    }
    else if (HS > 121.92 && TP > 13)
    {
      greenPWM = 0;
      bluePWM = 200;
      redPWM = 0;
    }
    else
    {
      greenPWM = 0;
      bluePWM = 0;
      redPWM = 200;
    }
  }
}