I am using the WiFi library and trying to store the IP address in a character buffer so that I can print it out to an lcd that only accepts character buffers. My code for doing this:
char * IPAD = new char[40
IPAddress ip = WiFi.localIP();
Serial.println(ip);
char * bufIP = new char[40]();
sprintf(bufIP, "%s", inet_ntop(ip));
lcd.string(ip);
But when I compile this I get the error:
"inet_ntop' was not declared in this scope"
Help?
-
try lcd.print(ip); lcd libraries use to implement Print and IPAddress implements PrintableJuraj– Juraj ♦10/26/2018 15:29:48Commented Oct 26, 2018 at 15:29
-
My LCD library only accepts char* elements :(garson– garson10/26/2018 15:49:54Commented Oct 26, 2018 at 15:49
-
1then use numbers ip[0], ip[1], ip[2], ip[3]Juraj– Juraj ♦10/26/2018 16:44:32Commented Oct 26, 2018 at 16:44
-
Which arduino are you using?Craig– Craig10/26/2018 21:28:25Commented Oct 26, 2018 at 21:28
2 Answers 2
An IPAddress
is a class. You can't use it like a number (although in certain circumstances you can assign it to a uint32_t
and it gives you the numeric representation).
Also, the inet_*
functions are only really relevant on a POSIX system with a full network stack - i.e., a computer, not a little Arduino without a proper networking stack.
You can, though, easily access the individual bytes of the IP address through the []
operator and place them into a char array with the right format:
sprintf(bufIP, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
char IP[] = "000.000.000.000";
WiFi.localIP().toString().toCharArray(IP, 16);
Serial.printf("Local IP Address: %s", IP);
-
2