-1

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?

asked Oct 26, 2018 at 15:24
4
  • try lcd.print(ip); lcd libraries use to implement Print and IPAddress implements Printable Commented Oct 26, 2018 at 15:29
  • My LCD library only accepts char* elements :( Commented Oct 26, 2018 at 15:49
  • 1
    then use numbers ip[0], ip[1], ip[2], ip[3] Commented Oct 26, 2018 at 16:44
  • Which arduino are you using? Commented Oct 26, 2018 at 21:28

2 Answers 2

3

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]);
answered Oct 26, 2018 at 23:09
1
char IP[] = "000.000.000.000";
WiFi.localIP().toString().toCharArray(IP, 16);
Serial.printf("Local IP Address: %s", IP);
answered Apr 29 at 0:48
1
  • 2
    Please explain how it works. Commented Apr 29 at 2:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.