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...
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);
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.
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?
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.