I'm having some major issues with the IPAddress variable.
What I want to be able to do is get the IP address and change the last octet to 255 for broadcasting.
I have tried String(WiFi.localIP()) but that gives me a number 117483712.
The use of sprintf was suggested, but I get an errors with that.
Either I'm stupid, or there's next to nothing on the web about this.
Does anyone know how to do this?
I'm at wits end and close to destroying my Arduino.
SuferTim, I love you.
I didn't realise it was actually an array I could manipulate in that way.
My idea of a string was that I could use sprintf or similar on it, but that isn't needed now.
It's not really an array - it's a lot cleverer than that; it's a class which provides array dereference operators that let you treat the address like an array, and it also provides assignment and comparison operators which enable you to treat it like a primitive type, and methods to help you print it, and so on.
I believe what the OP was trying to do was convert the IPAddress variable to a c-string so it can be entered into a sprintf function?
c-strings typically consume less memory than C++ Strings (note the capical S) and do not cause memory fragmentation like Strings do.
If this was the case then where you had String(WiFi.localIP())
... rather use: WiFi.localIP().toString().c_str()
This has the advantage of being compatible with sprintf functions, these are old school I know but they have some advantages. For example, they allow you to format the entire debug message to one string, even with variables in the middle. eg:
char[100] buffer;
sprintf(buffer, "Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
Serial.println(buffer);
this will create a single string called buffer that contains something like:
... "Now listeneng at IP 192.168.1.201, UDP port 88"
Which is much easier when you want to log it to SD card, or send it via wifi... or both.
If you are using the latest Arduino IDE, they have now added a Serial.printf() command so can do it in one line without having to fragment your Serial.println() statements every time you insert a variable.
Here is a really good tutorial on the sprintf function.
While trying to print the IP Address to a string in a ESP8266 sketch I encountered the same error. The solution suggested by @michaelwardsystems was the proper one: WiFi.localIP().toString().c_str(). I also enjoyed the explanation given. Many thanks