Ethernet shield W5500: Failed to obtain an IP address using DHCP

Hello again,,

I'm using this Ethernet Shield
With Arduino Uno.

Basically, i have some values that I will get from sensors and I want to post them to a database using a PHP script that was pre-uploaded to the database. My PHP script works fine, my problem is only with working with the shield.

I tried to combine this example from arduino with my own HTTP GET and every time i get this response: Failed to obtain an IP address using DHCP

I think my problem is with initializing the ethernet connection, I'm using an ethernet cable in my office that works fine when i connect to my laptop, it basically gives me internet when used with the laptop, and all i want is to make the Arduino capable of connection with the online server to send my data to the database



#include <SPI.h>
#include <Ethernet.h>
EthernetClient client;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

int HTTP_PORT = 80; 
String HTTP_METHOD = "GET"; 
char HOST_NAME[] = "realtimepv.000webhostapp.com";
String PATH_NAME = "Insert.php?";

int voltage = 10;
int current = 10;
int lux = 10;
String queryString = String("voltage="+String(voltage)+String("&current=")+String(current)+String("&lux=")+String(lux));
//the voltage must be saved into a variable called voltage and before the querystring
//the current must be saved into a variable called current and before the querystring
//the lux must be saved into a variable called lux and before the querystring


void setup() {
   Serial.begin(9600);
// initialize the Ethernet shield using DHCP:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to obtaining an IP address using DHCP");
    while(true);
  }
  
 // connect to web server on port 80:
  if(client.connect(HOST_NAME, HTTP_PORT)) {
    // if connected:
    Serial.println("Connected to server");
    // make a HTTP request:
    // send HTTP header
    client.println(HTTP_METHOD + " " + PATH_NAME + queryString + " HTTP/1.1");
    client.println("Host: " + String(HOST_NAME));
    client.println("Connection: close");
    client.println(); // end HTTP header

    while(client.connected()) {
      if(client.available()){
        // read an incoming byte from the server and print it to serial monitor:
        char c = client.read();
        Serial.print(c);
      }
    }

    // the server's disconnected, stop the client:
    client.stop();
    Serial.println();
    Serial.println("disconnected");
  } else {// if not connected:
    Serial.println("connection failed");
  }
}
  


void loop() {
  // put your main code here, to run repeatedly:

}

does your office implement some sort of ethernet client filtering (for example only known Mac addresses can connect to the network) or more secure techniques (certificate based)?

1 Like

Not at all, in fact any random device can just plug in that wire and enjoy the internet

You said

They mention using this library GitHub - Wiznet/WIZ_Ethernet_Library: WIZnet Ethernet Library

Try replacing that with this code from the Ethernet -> WebClient example. It will let you know if the hardware is being detected.

  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      while (true) {
        delay(1); // do nothing, no point running without Ethernet hardware
      }
    }

The hardware is not detected actually, any ideas why?

It doesn't work
It's for IDE 1.5.x
Mine is IDE 1.8.x

I still downloaded it and tried, didn't work, had errors running the example

OK - and the Ethernet library you are using is really GitHub - arduino-libraries/Ethernet: Ethernet Library for Arduino

My guess is that the default CS pin is not the one your Wiznet chip is connected to. Use Ethernet.init(10) to select the correct CS pin before calling Ethernet.begin(mac).

1 Like

I did, still same

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.