Failed to configure ethernet using DHCP

Hi everyone!
I have a project to make the arduino remote switch, so I can turn on/off my server (aka my computer) when I am not at home. I have a HanRun HR911105A Ethernet shield that I use for my project. I was thinking that I have a good code and everything, but when I uploaded my project, I got the "Failed to configure Ethernet using DHCP" error. I need to read a file that i host on 000webhost.com service, so I know is a "on" or "off" word in a file, so I can turn on or off my server using relay, but that is not part of the problem. I want to know is the problem on the 000webhost side, or on my arduino coding side.

These are my project specifications:
I use 000webhost free hosting service
This is the file I need to read: http://spoz.website/control.txt
I use HanRun HR911105A Ethernet shield for arduino
There are no errors on compiling sketch part of arduino uploading proccess
Below ethernet shield is the normal Arduino UNO board.

Here is my source code:

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x49 };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "www.spoz.website";    // name address for Google (using DNS)
bool conn=false;

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  pinMode(9,OUTPUT);
  pinMode(8,OUTPUT);

  digitalWrite(8,HIGH);
  digitalWrite(9,HIGH);
  
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    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(1000);

  digitalWrite(8,LOW);
  digitalWrite(9,LOW);

}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  String readString = String(100);

  Serial.println("connecting...");
  conn = client.connect(server, 80);

  // if you get a connection, report back via serial:
  if (conn) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /control.txt HTTP/1.1");
    client.println("Host: www.spoz.website");
    //client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  
  if (client.available()) {

    char c = 'X';
    
    while (client.available())
    {
       c = client.read();

       readString.concat(c);
    
       Serial.print(c);
    }
  }

  Serial.println(readString);


  delay(1000);

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    //while (true);
    delay(2000);
  }


}

If you know that error is in the code can you please correct my code and send it on the forum, if you can of course. I was searching on the internet, but I couldn't find the answer. I hope you can help me.

HanRun HR911105A Ethernet

HanRun usually is the manufacturer of the RJ45 connector. Why is there no link to the device you actually bought?

I want to know is the problem on the 000webhost side, or on my arduino coding side.

It's not on the 000webhost side. It might be your network, your ethernet shield or because you use the String class in your code.

this is HR911105A

Faraday Member, yeah you are right. I have that ethernet shield

DHCP is a local service from your internet connected router. If other devices in your network can get an IP address, then it should be working.
Anyway, it does not matter about DHCP if you have defined your own static IP address :

IPAddress ip(192, 168, 1, 177 ) ;

If other devices in the network have similar addresses i.e. beginning with 192.168.1 (and a standard netmask is in use) then that should work.

Even after that message it shows "connecting..." and failed to connect, maybe thats not a problem. But it doesnt get a response from a server, so thats a problem. It show failed to connect error after everything. I am mobile now so maybe there are errors in my typing.

Hi again everyone!
Maybe you remember my post where I was having a DHCP problem. There is no DHCP problem anymore, but I have another problem. Every time everything works correctly except the connecting part. My arduino can't connect to google, even with IP address variable or with character address! I have some delays to let it connect, but it is not connecting. I removed the 'if', so it shows the message "connected to" every time, so when it can't connect it shows "Connected to", that delay so it has enough time to load everything,"0.0.0.0"! Than goes the message that I received 0 bytes and everything... I have the HanRun HR911105A Ethernet Shield that I am using with Arduino UNO. Here is my code:

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(8,8,8,8);  // numeric IP for Google (no DNS)
//char server[] = "www.google.com";    // name address for Google (using DNS)
//String server ="8,8,8,8";
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 1, 1);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true;  // set to false for better speed measurement

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip, myDns);
      beginMicros = micros();
      Serial.println("Delay incoming");
      client.connect(server, 80);
           delay(10000);
    Serial.print("connected to ");
           delay(10000);
    Serial.println(client.remoteIP());
    // 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();
      
       
    
  
  Serial.println("End of the setup!!!");
  }

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  int len = client.available();
  if (len > 0) {
    byte buffer[80];
    if (len > 80) len = 80;
    client.read(buffer, len);
    if (printWebData) {
      Serial.write(buffer, len); // show in the serial monitor (slows some boards)
    }
    byteCount = byteCount + len;
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    endMicros = micros();
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    Serial.print("Received ");
    Serial.print(byteCount);
    Serial.print(" bytes in ");
    float seconds = (float)(endMicros - beginMicros) / 1000000.0;
    Serial.print(seconds, 4);
    float rate = (float)byteCount / seconds / 1000.0;
    Serial.print(", rate = ");
    Serial.print(rate);
    Serial.print(" kbytes/second");
    Serial.println();

    // do nothing forevermore:
    while (true) {
      delay(1);
    }
  }
}

If you know that the problem is in code, can you modify it and send it here, please? I hope to get help soon! Everyone who replied, thanks for help even if it works or not!

I do t know if it’s your problem, but if you need to configure CS pin, you should probably un-comment one those lines.

AHMHA:

void setup() {

// You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);  // MKR ETH shield
  //Ethernet.init(0);  // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

IPAddress server(8,8,8,8);  // numeric IP for Google (no DNS)

8.8.8.8 is Google's DNS server. I doubt a webserver will answer there.

IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 1, 1);

If the DNS server and the own IP address are in different C-networks you have to provide a network mask (if the network is bigger than a /24) and/or a gateway address.

BTW: Google doesn't start searches on HTTP anymore. You must request the search over HTTPS but AVR Arduinos with an Ethernet Shield cannot do HTTPS requests.

Faraday Member, yeah you are right. I have that ethernet shield

No you have that connector. Do you really see a shield on that picture?

Even after that message it shows "connecting..." and failed to connect, maybe thats not a problem. But it doesnt get a response from a server, so thats a problem. It show failed to connect error after everything. I am mobile now so maybe there are errors in my typing.

Are you sure you have a WizNet based Ethernet Shield? Because using your code with absolutely nothing connected would give the same result...

you didn't solve anything, you only forced IP address to library.
as pylon wrote, with no shield connected it would give the same result.

@AHMHA

DO NOT cross post as it wastes peoples time and effort to help you.

Please READ THIS before you post anything else.

Bob.