How to get IP address from String?

Hi

I make a simple request to http://www.ipify.org/ to get my wan ip address to update to no-ip.com. My project need DDNS from no-ip.com. I use subString to get String ip begin, but end of ip, there many character “ÿ” (from Serial monitor), please take a look to my Blynk’s screen shots

Can anyone help me, I just begin start with arduino and program about 2 months. I search google many time, but can not find some help, so I post here.

Sorry about my bad English



1 Like

It would really help if you posted your arduino code!

Chances are there’s a character at the end of the ip string, probably a newline \n, so you should be able to do something like this:

int end =  myString.indexOf('\n');
string justip = myString.substring(0, end);

If you’re stuck it might help to print your string byte by byte.

Hi

Thanks for your help, this is my code to get IP address:
void getIP() { if (client.connect("api.ipify.org", 80)) { terminal1.println("connected to api.ipify.org"); terminal1.println(); terminal1.flush(); client.println("GET / HTTP/1.0"); client.println("Host: api.ipify.org"); client.println(); } else { terminal1.println("connect to api.ipify.org failed"); terminal1.println(); terminal1.flush(); } while(client.connected() && !client.available()); while (client.connected() || client.available()) { char c = client.read(); //gets byte from ethernet buffer readString += c; //places captured byte in readString } client.stop(); //stop client newIP = readString.substring(153); Serial.println(newIP); Serial.println(); terminal1.println(newIP); terminal1.println(); terminal1.flush(); readString=""; }

I test in my arduino mega, It work correctly, it return new Ip as I want, no more strange Character after Ip address, everytime. But when I use same code on my nodeMCU, the String I got not only IP address, it plus some or many char " ÿ " on Serial Monitor, if I print that String to terminal , it show like my screenshot, I search Google and I found that " ÿ " is 255 (-1) , I tried to take indexOf “ÿ” to re use substring to get right String before first char “ÿ”, but didn’t work, I use int a = String.indexOf(“ÿ”); is that right code?

Sorry my bad English

This looks like the string has not been terminated - which is a little odd. Try adding this, just before your call to client.stop():

readString += '\0'; 

If that doesn’t work it would help to count how many bytes your client has read and print that out.

thanks for your help, I will try tonight

Hi

I sovled it by int char_remove = newIP.indexOf((char)-1);

Another problem, while hardware connect to getip server, it seam hardware lost connect with blynk sever, is that normal?

Can you give me or pm me complete ino file that works on mega???
Thanks in advance

Help ME …