TFT_eSPI library Q: how to print IP address to TFT?

I want to display startup-messages to my TFT screen...
But displaying the IP address is a problem, if I use the code below, I get a long decimal number...
I tried to look into the library code but I still don't know how to solve this...?
A Google search was also unsuccessful.

On the Serial monitor I see: IP Address: 192.168.178.92
That is what I would like to see on the TFT...

I use the TFT_eSPI library!

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  
  tft.setTextColor(TFT_GREEN);
  tft.drawCentreString((String)ip, 64, 55, 1);
  
  Serial.print("IP Address: ");
  Serial.println(ip);

WiFi.localIP().toString();

best regards Stefan

I want to thank you for taking the time to reply!

I tried your solution but I get a error message

After looking for relevant keywords it came up with a rather ugly but working solution: <LINK>

I added this piece of code into the Wifi Status function:

void wifiStatus()
{
  // ===================================================================
  // ! added just to get the <beep> ip address printable
  // Write into a C string (a char array), a la sprintf().
  // Warning: there is no check for buffer overflow.
  class StringPrinter : public Print
  {
  public:
    StringPrinter(char *buffer) : buf(buffer), pos(0) {}
    virtual size_t write(uint8_t c)
    {
      buf[pos++] = c; // add the character to the string
      buf[pos] = 0;   // null perminator
      return 1;       // one character written
    }

  private:
    char *buf;
    size_t pos;
  };
  // ===================================================================

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();

  char ipChar[16];
  StringPrinter(ipChar).print(ip);
  String ipStr(ipChar);      //convert char array to string here.

  tft.setTextColor(TFT_CYAN);
  tft.drawCentreString("IP: " + ipStr, 64, 50, 1);

The better solution was also in the website I pointed to.
Only 3 extra lines of code...

It was hard to find but I in the end it worked out well!

  char ipChar[16];
  sprintf(ipChar, "%u.%u.%u.%u", ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, (ip >> 24) & 0xFF);
  String ipStr(ipChar); //convert char array to string here.

  tft.setTextColor(TFT_CYAN);
  tft.drawCentreString("IP: " + ipStr, 64, 50, 1);

if you convert somethimg to a String it must be stored in a variable of type String

You seem to have not yet understood what a variable-type is
Your line of code
IPAdress ip = WiFi.localIP().toString();

tries to assign a String to a variable of type IPAdress which is an array of bytes with 4 elements

an array of 4 bytes is something totally different from a String

tft.drawCentreString(WiFi.localIP().toString());

or at least
tft.drawCentreString(WiFi.localIP().toString()).c:str();

should work too

if you look up the definition of the function

drawCentreString(........)

inside the library you will see what kind of parameter it expects.
That serial.print() is able to print so many different variable-types is becaus they are all programmed.

best regards Stefan

Thank you Stephan, yes I have often look hard to get it right... I have limited knowledge of C++ but enough to have fun with it.

But beside that, "toString()" is not a member of the IPAddress.cpp I use. That is visible in the screenshot I made. I only found it in the IPAddress.cpp for an ESP8266 board. I use an Arduino Nano 33 IoT so they left it out I think?

This another reason why it is always good to write the details which microcontroller you are using

I have never used a Nano IoT. just ESP8266 and ESP32.

the ESP32's start at $6 240 MHz, 520 KB of SRAM 4, 8 or 16 MB flash
outstanding price/perfomance ratio

And somehow ironic a Nano IoT connects to the WLAN based on an ESP32
best regards Stefan

1 Like

Yes you are absolutely right to include more details!

The main reason for using Arduino is support for the community by buying genuine hardware and more importantly upload time of < 1 sec!

Because I am not an experienced programmer, I need to upload more often to see what will happen and then to have such incredible short upload time is great.

Thanks for your input!

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