Ethernet library not initializing IP correctly

Trying the Web Server example with W5500 (Ethernet2 library).

  Web Server

 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield.

 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 modified 02 Sept 2015
 by Arturo Guadalupi

#include <SPI.h>
#include <Ethernet2.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x2C, 0xF7, 0xF1, 0x08, 0x36, 0x71 };  
// change network settings to yours
IPAddress ip( 192, 168, 21, 245 );    
IPAddress gateway( 192, 168, 21, 15 );
IPAddress dnss( 192, 168, 20, 14 );
IPAddress subnet( 255, 255, 255, 0 );

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
  // Open serial communications and wait for port to open:
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, dnss, gateway, subnet);
  Serial.print("server is at ");

void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c =;
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println("<!DOCTYPE HTML>");
          // add a meta refresh tag, so the browser pulls again every 5 seconds:
          client.println("<meta http-equiv=\"refresh\" content=\"5\">");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(" is ");
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
    // give the web browser time to receive the data
    // close the connection:
    Serial.println("client disonnected");


server is at

But is initialized as
Looks like a problem with 2nd, 3rd and 4th octet in the library.

Help is appreciated.

PS Yes, my DNS server is on another subnet, that’s not a typo. We’re dealing with initialization of device’s own IP for now.

I also tried it with W5100 (Ethernet library)…


server is at

During build I saw these warnings:

In file included from C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.39.0_x86__mdqgnx93n4wtt\libraries\Ethernet\src\Dns.cpp:8:0:
C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.39.0_x86__mdqgnx93n4wtt\libraries\Ethernet\src\Dns.cpp: In member function ‘uint16_t DNSClient::BuildRequest(const char*)’:
C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.39.0_x86__mdqgnx93n4wtt\libraries\Ethernet\src\utility/w5100.h:457:25: warning: result of ‘(256 << 8)’ requires 18 bits to represent, but ‘int’ only has 16 bits [-Wshift-overflow=]
#define htons(x) ( (((x)<<8)&0xFF00) | (((x)>>8)&0xFF) )

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.39.0_x86__mdqgnx93n4wtt\libraries\Ethernet\src\Dns.cpp:164:18: note: in expansion of macro 'htons'

Is my version of Arduino IDE bugged? I downloaded the latest .cc version from Microsoft Store.

Yeah, it's bugged.

I tried the nightly build, same thing.

Then I downgraded to 1.8.0 and W5100 with the regular Ethernet library now works.

W5500 still has a problem with wrong IP being initialized.


Got W5500 examples working with Arduino IDE 1.8.0 and Ethernet2 library version 1.0.2

Using Ethernet2 library version 1.0.2 with Arduino IDE 1.8.13 (current) allows HTTP webserver example to work, but SMTP send example does not, because there are bugs in building of DNS code in Arduino IDE 1.8.13
Ethernet2 library versions 1.0.3 and 1.0.4 have the IP initialization problems.

I don't know if there are any versions between 1.8.0 and 1.8.13 that are free of these problems.

Ugh. This isn't how "getting started" should go.