Issues connecting to Ethernet with static IP

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:

#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:


  // 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) 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

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?

 Ethernet.begin(mac, ip, myDns);

try replacing the above with

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

upload the complete modified code?

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

/*
  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

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?

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

Yep, I got packets recieved

Terminal:

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

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

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

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"

therefore it looks like your code is OK
possibly the DNS is not working
try replacing "www.google.com" with the IP address, e.g.

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

can 192.168.1.1 resolve DNS for www.worldtimeapi.org?

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!

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

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