Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Nico van der Dussen
#22839 Hi,

I want to convert an IPAddress to a string in the Arduino IDE

I've tried the following

Code: Select all 
  IPAddress ipno;
char ipno2[26] ;

  ipno = WiFi.localIP();
  sprintf(ipno2, "%d.%d.%d.%d", ipno[0], ipno[1], ipno[2], ipno[3]);


This gives the error:

Code: Select allArduino: 1.6.5 (Windows 8.1), Board: "Generic ESP8266 Module, 80 MHz, 115200, 512K (64K SPIFFS)"

<cut a lot of noise>

AdvancedWebServerMMA.ino:78:2: error: cannot pass objects of non-trivially-copyable type 'class IPAddress' through '...'
cannot pass objects of non-trivially-copyable type 'class IPAddress' through '...'


What is the right way of doing this?

Thanks
User avatar
By martinayotte
#22845 Personally, I've added a new method in IPAddress class.
In IPAddress.h
Code: Select allchar * toCharArray();

In IPAddress.cpp
Code: Select allchar *IPAddress::toCharArray()
{
    static char szRet[20];
    String str = String(_address.bytes[0]);
    str += ".";
    str += String(_address.bytes[1]);
    str += ".";
    str += String(_address.bytes[2]);
    str += ".";
    str += String(_address.bytes[3]);
    str.toCharArray(szRet, 20);
    return szRet;
}

So, you can easily print IP under almost and Printable, such as Serial.println(Wifi.localIP().toCharArray());