Search code examples
arduinoethernet

Arduino Display Ethernet.localIP()


I'm trying to assign the IP Address of the device to a String variable. When I use Serial.println(Ethernet.localIP()) to test it displays the IP Address in octets. If I use String(Ethernet.localIP()); then it displays it as a decimal.

Is there a way to assign the octet format to a variable?

 String MyIpAddress;

 void StartNetwork()
 {
     Print("Initializing Network");
     if (Ethernet.begin(mac) == 0) {
     while (1) {
       Print("Failed to configure Ethernet using DHCP");
       delay(10000);
     }
   }
   Serial.println(Ethernet.localIP());        //displays: 192.168.80.134
   MyIpAddress = String(Ethernet.localIP());
   Serial.println(MyIpAddress);               //displays: 2253433024
 }

Solution

  • Turns out that the IPAddress property is an array. One simple way to display the IP address is as follows:

    String DisplayAddress(IPAddress address)
    {
     return String(address[0]) + "." + 
            String(address[1]) + "." + 
            String(address[2]) + "." + 
            String(address[3]);
    }