Hi,
I have never used Sprintf before and had to make this from various places on the internet, althought this works and give me IP = 192.168.1.77
char myIpString[14];
IPAddress myIp = WiFi.localIP();
sprintf(myIpString, "IP = " "%d.%d.%d.%d", myIp[0], myIp[1], myIp[2], myIp[3]);
It looks to me as though I may have missed a comma or something after "IP = " ?
Also, so that I can better understand how would this have been achieved without Sprintf .
Regards
Gaz
Apart from anything else you should make the buffer at least 16 characters in length to allow for an IP address of maximum length plus the zero terminator.
this works and give me IP = 192.168.1.77
What did you expect to get ?
Do you actually need the quotes around the spaces ?
What are you doing with the buffer once you have the data in it ?
If you are just printing it you can do
Serial.print("IP = ";
for (int x = 0; x < 3;x++)
{
Serial.print(myip[x]);
Serial.print(".");
}
Serial.println(myip[3];
fortunately Arduino has not IP6 yet as that would increase the buffer quite a bit..
Hi,
myIpString actually is used as an output to be sent via MQTT and i am using PubSubClient.
The spaces were added just to make it clear to myself that one part was text I had entered.
It did give me the desired result.
But between
sprintf(myIpString, "IP = "
and
"%d.%d.%d.%d", myIp[0], myIp[1], myIp[2], myIp[3]);
just looks wrong as if it should be seperated with a comma or something ? or is that how you should use sprintf
The code:
void MQTT_module_Info()
{
char myIpString[14];
IPAddress myIp = WiFi.localIP();
sprintf(myIpString, "IP = " "%d.%d.%d.%d", myIp[0], myIp[1], myIp[2], myIp[3]);
WiFi_client.publish (Topic_01, myIpString);
}
Regards
Gaz
Koepel
December 12, 2015, 10:57am
5
It is allowed to have two strings "Hello " "World". But you can make it one string "Hello World".
The format "IP = xxx.xxx.xxx.xxx" plus zero terminator is 21 bytes (count them !).
char buffer[30];
IPAddress myIp = WiFi.localIP();
sprintf(buffer, "IP = %d.%d.%d.%d", myIp[0], myIp[1], myIp[2], myIp[3]);
Or a (big) lookup table ..