# Issues connecting to Ethernet with static IP

**URL:** <https://forum.arduino.cc/t/issues-connecting-to-ethernet-with-static-ip/971796>\
**Category:** Networking, Protocols, and Devices\
**Created:** [March 21, 2022, 4:25pm UTC](https://forum.arduino.cc/t/issues-connecting-to-ethernet-with-static-ip/971796 "2022-03-21T16:25:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![akinian](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@akinian](https://forum.arduino.cc/u/akinian)\
**Post date:** [March 21, 2022, 4:25pm UTC](https://forum.arduino.cc/t/issues-connecting-to-ethernet-with-static-ip/971796/1 "2022-03-21T16:25:59Z")

</div>

Hello, I am currently working on an Arduino project to remotely control electrovalves for irrigation purposes. I have made a database which I can access perfectly fine in a local server through the Ethernet Shield, with either static or dhcp IP addresses. However, and in order to save myself some money, I tried to also connect through a world time api and save myself buying jumpers and an rtc module. However, when working with the code I ran into a weird problem.

This is the normal WebClient Ethernet example provided in the Arduino IDE (with altered _valid_ MAC and IP Adresses:

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

// Enter a MAC address for your controller below.
byte mac[] = { 0x1E, 0xE8, 0x09, 0xE7, 0x56, 0xD8 };

char server[] = "www.worldtimeapi.org"; // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 33);
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() {
  // 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:");
  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
      }
    }
    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip, myDns);
  } else {
    Serial.print(" DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.print("connecting to ");
  Serial.print(server);
  Serial.println("...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.print("connected to ");
    Serial.println(client.remoteIP());
    // Make a HTTP request:
    client.println("GET /api/timezone/America/Tegucigalpa HTTP/1.1");
    client.println("Host: www.worldtimeapi.org");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  beginMicros = micros();
}

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);
    }
  }
}

```

For this example, the code works fine. The IP Address is assigned through DHCP (In this case, it normally assigns to 192.168.1.221 ) and the rest of the code works fine (I get a response from the server, which is printed in the Serial Model. However, and for better control purposes, the IT boss wants the Ethernet Shield to be assigned to a static IP (192.168.1.33), so I comment out this whole section of code:

```cpp

  // 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
      }
    }
    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip, myDns);
  } else {
    Serial.print(" DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }*/
  Ethernet.begin(mac, ip, myDns);
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.print("connecting to ");
  Serial.print(server);
  Serial.println("...");

```

And when running the code again, it does not work. It always prints out connection failed.

I've been reading a lot of posts in the forums for an answer, so, this are bits of information I think are important:

- The IP and MAC addresses _are_ valid, unused addresses. Our IT boss has a record of all used and unused IP Address in our workplace and we double checked the list, even pinged the IP address beforehand it is completely free. And I dont think the MAC address is not valid, since when I insert a used MAC, it doesnt even assign a DHCP IP.
- The API was/is accessible at all times during our testing.
- I tried the original example without changing the server ([arduino - Google Search](http://www.google.com/search?q=arduino)) and it still does not communicate.
- Even when assigning the SAME IP that DHCP does automatically, I get no communication.
- The Ethernet cable is not connected to my PC, is directly connected to a repeater connected an Access Point. My Computer is connected to Wi Fi
- I am very new to Arduino and informatic, so anything you share will help me

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [March 21, 2022, 4:31pm UTC](https://forum.arduino.cc/t/issues-connecting-to-ethernet-with-static-ip/971796/2 "2022-03-21T16:31:16Z")

</div>

can you ping 192.168.1.33  
you only give use part of the modified code - what are values of mac, ip and myDns - did you change them?

```cpp
 Ethernet.begin(mac, ip, myDns);

```

try replacing the above with

```cpp
  Ethernet.begin(mac, ip);
  delay(1000);
  Serial.println(Ethernet.localIP());

```

upload the complete modified code?

---

<div class="post-metadata">

**Author:** ![akinian](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@akinian](https://forum.arduino.cc/u/akinian)\
**Post date:** [March 21, 2022, 4:36pm UTC](https://forum.arduino.cc/t/issues-connecting-to-ethernet-with-static-ip/971796/3 "2022-03-21T16:36:19Z")

</div>

Im sorry, this is the whole code in the second attempt:

```cpp
/*
  Web client

 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe, based on work by Adrian McEwen

 */

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

// Enter a MAC address for your controller below.
byte mac[] = { 0x1E, 0xE8, 0x09, 0xE7, 0x56, 0xD8 };

char server[] = "www.google.com"; // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 33);
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:");
  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
      }
    }
    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip, myDns);
  } else {
    Serial.print(" DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }*/
  Ethernet.begin(mac, ip, myDns);
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.print("connecting to ");
  Serial.print(server);
  Serial.println("...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.print("connected to ");
    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();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  beginMicros = micros();
}

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);
    }
  }
}

```

And yes, I have tried without Dns, it still doesn't work

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [March 21, 2022, 4:56pm UTC](https://forum.arduino.cc/t/issues-connecting-to-ethernet-with-static-ip/971796/4 "2022-03-21T16:56:29Z")

</div>

what Ethernet shield is it?  
my EthernetSheildV1 which is fairly old will not work with my latest router  
can you ping 192, 168, 1, 33?

---

<div class="post-metadata">

**Author:** ![akinian](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@akinian](https://forum.arduino.cc/u/akinian)\
**Post date:** [March 21, 2022, 5:18pm UTC](https://forum.arduino.cc/t/issues-connecting-to-ethernet-with-static-ip/971796/5 "2022-03-21T17:18:41Z")

</div>

Ethernet Shield V1, it is a reused from a discarded project which worked with the Ethernet library bundled with the Arduino IDE.

> [@](#):
>
> can you ping 192, 168, 1, 33?

Yep, I got packets recieved

Terminal:

> [@](#):
>
> Pinging 192.168.1.33 with 32 bytes of data:  
> Response from 192.168.1.33: bytes=32 time=127ms TTL=128  
> Reply from 192.168.1.33: bytes=32 time=5ms TTL=128  
> Reply from 192.168.1.33: bytes=32 time=3ms TTL=128  
> Reply from 192.168.1.33: bytes=32 time=4ms TTL=128
> 
> Ping statistics for 192.168.1.33:  
> Packets: sent=4, received=4, lost=0.  
> (0% lost),  
> Approximate round-trip times in milliseconds:  
> Minimum = 3ms, Maximum = 127ms, Average = 34ms.

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [March 21, 2022, 6:21pm UTC](https://forum.arduino.cc/t/issues-connecting-to-ethernet-with-static-ip/971796/6 "2022-03-21T18:21:27Z")

</div>

connected up an old router and run your code of post #3 on a UNO with EthernetShield1  
I can ping 192.168.1.33 OK

I ran a Java server on my PC IP address 192.168.1.2  
therefore I made one modification to you code

```cpp
char server[] = "192.168.1.2";//"www.google.com"; // name address for Google (using DNS)

```

the Uno connects to the server and Serial Monitor displays

```cpp
connecting to 192.168.1.2...
connected to 192.168.1.2
hello test 1
hello test 2
test 3
test 4

```

the Java server displays

```cpp
F:\Programming\Ardunio\Networking\EthernetSheildV1\TCPchatClient>java TCPchatServerScanner
TCP chat server: IP address BB-DELL2/192.168.1.2 port 80
TCP server starting: IP address BB-DELL2/192.168.1.2 port 80
Client connect from IP address /192.168.1.33 port 49153
thread to receive
Server received: 'GET /search?q=arduino HTTP/1.1'
Server received: 'Host: www.google.com'
Server received: 'Connection: close'
Server received: ''
hello test 1
Sending: 'hello test 1'
hello test 2
Sending: 'hello test 2'
test 3
Sending: 'test 3'
test 4
Sending: 'test 4'

```

you can see I am typing text into the Java server which is appearing on the UNO Serial Monitor

the router does not have an internet connection so I cannot test connection to "[www.google.com](http://www.google.com)"

therefore it looks like your code is OK  
possibly the DNS is not working  
try replacing "[www.google.com](http://www.google.com)" with the IP address, e.g.

```cpp
Host name: www.google.com
IP address: 216.58.213.4

```

---

<div class="post-metadata">

**Author:** ![Juraj](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juraj/32/238785_2.png) [@Juraj](https://forum.arduino.cc/u/Juraj)\
**Post date:** [March 21, 2022, 6:32pm UTC](https://forum.arduino.cc/t/issues-connecting-to-ethernet-with-static-ip/971796/7 "2022-03-21T18:32:47Z")

</div>

can 192.168.1.1 resolve DNS for [www.worldtimeapi.org](http://www.worldtimeapi.org)?

---

<div class="post-metadata">

**Author:** ![akinian](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@akinian](https://forum.arduino.cc/u/akinian)\
**Post date:** [March 21, 2022, 7:17pm UTC](https://forum.arduino.cc/t/issues-connecting-to-ethernet-with-static-ip/971796/8 "2022-03-21T19:17:49Z")

</div>

> [@horace](#):
>
> therefore it looks like your code is OK  
> possibly the DNS is not working  
> try replacing "[www.google.com](http://www.google.com)" with the IP address, e.g.
> 
> ```arduino
> Host name: www.google.com
> IP address: 216.58.213.4
> 
> ```

Incredibly enough I never though about replacing the url with the IP address, partly due to the fact that it says you can use a URL if you specify the DNS... but it worked like a charm. Maybe the DNS was not working like you mentioned. Thank you so much!

---

<div class="post-metadata">

**Author:** ![akinian](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@akinian](https://forum.arduino.cc/u/akinian)\
**Post date:** [March 21, 2022, 7:23pm UTC](https://forum.arduino.cc/t/issues-connecting-to-ethernet-with-static-ip/971796/9 "2022-03-21T19:23:10Z")

</div>

Correct. I think the DNS I used was incorret. The comment above worked just fine

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 17, 2022, 7:23pm UTC](https://forum.arduino.cc/t/issues-connecting-to-ethernet-with-static-ip/971796/10 "2022-09-17T19:23:44Z")

</div>

Closed on September 17, 2022, 7:23pm UTC

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