Ethernet.localIP() returns '0.0.0.0' in sketch, despite returning localIP with WebServer

Hi everyone,

I am encountering something very weird. I am currently building a WebSocketServer using the example set out in the mWebSockets library (GitHub - skaarj1989/mWebSockets: WebSockets for microcontrollers) for my Arduino Uno R3 SMD (ATmega328P) and Ethernet Shield 2 (W5500). I am also running Arduino 1.8.15 on a Linux OpenSUSE Leap 15.2 desktop.

The sketch looks like this:

#include <WebSocketServer.h>
using namespace net;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);

constexpr uint16_t port = 3000;
WebSocketServer wss{port};

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
  }
  Serial.println("Ethernet WebServer Example");

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  // 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.");
  }

  // start the server
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());

  wss.onConnection([](WebSocket & ws) {
    const auto protocol = ws.getProtocol();
    if (protocol) {
      Serial.print(F("Client protocol: "));
      Serial.println(protocol);
    }

    ws.onMessage([](WebSocket & ws, const WebSocket::DataType dataType,
    const char *message, uint16_t length) {
      switch (dataType) {
        case WebSocket::DataType::TEXT:
          Serial.print(F("Received: "));
          Serial.println(message);
          break;
        case WebSocket::DataType::BINARY:
          Serial.println(F("Received binary data"));
          break;
      }

      ws.send(dataType, message, length);
    });

    ws.onClose([](WebSocket &, const WebSocket::CloseCode, const char *,
    uint16_t) {
      Serial.println(F("Disconnected"));
    });

    Serial.print(F("New client: "));
    Serial.println(ws.getRemoteIP());

    const char message[] {"Hello from Arduino server!"};
    ws.send(WebSocket::DataType::TEXT, message, strlen(message));
  });

  wss.begin();
}

void loop() {
  wss.listen();
}

However if I open my Serial Monitor, I see:

Initializing...
Server running at 0.0.0.0:80

This is particularly strange given that, if I upload the WebServer example to my Arduino, the static IP seems to be correctly assigned:

Ethernet WebServer Example
server is at 198.162.1.177

With the example of the WebServer, I can successfully ping the Arduino, indicating my Ethernet connection also works, but for reference, here are the settings of my Ethernet connection:
- IPv4 communication
- IP Address: 198.162.1.21
- Netmask: 255.255.255.0
- Gateway: 0.0.0.0

Whilst my sketch compiles, I do keep getting an error, but I am unsure how to interpret this:

/home/user/Arduino/libraries/WebSocketServer.cpp:8:1: note: '__comp_dtor' was previously declared here
WebSocketServer::~WebSocketServer() { shutdown(); }
^
/home/user/Arduino/libraries/mWebSocketServer.cpp:8:1: note: code may be misoptimised unless -fno-strict-aliasing is used

Why is Ethernet.localIP() managing to find the IP address successfully for WebServer example, whereas for my sketch produces an IP address of 0.0.0.0 despite assigning the IP previously?

Try also configuring the other network options, DNS, gateway and subnet mask. It could well be that the function returns an ip address for those with a completed set of arguments first. If you use DHCP, I bet it will return the IP address provided by the DHCP server.

I have now added the extra information you suggested, including the gateway, DNSserver and subnet. This now works and assigns the appropriate IP address. In addition, it can now be pinged from the PC. Thank you very much for your help!

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