3

How can i convert a String to IPAddress on arduino / esp ?

etc. "192.168.1.2" -> IPAddress(192, 168, 1, 2)

Tried this

void setup() {

  Serial.begin(115200);

  IPAddress apip;

  const char *apipch;
  apipch = "192.168.4.1";

Serial.println(apip.fromString(apipch));

}

void loop() {
  // put your main code here, to run repeatedly:
}

But i just get an output "1"

  • There are two possibilities in the link, which one did you use? If it didnt work, you did something wrong, but I can't guess what you did do. Post your code, perferably the shortest possible bit - it does not have to be functional, just the the bit where you get your string (is it a String or a char[] ?) how you implemneted the convert code, and where you call the ip() function. – Msquare Sep 10 '19 at 16:50
  • please do not say didn't work ... instead say this is what happened .... .... "didn't work" is almost meanigless – jsotola Sep 10 '19 at 16:59
  • ip.fromString(s.c_str()); – Juraj Sep 10 '19 at 19:54
  • @Msquare have added an example tried both codes but they gave me a result of 1 – strange_esp Sep 11 '19 at 9:02
  • 1
    because you print the return value of fromString and not the IP address. Serial.println(apip); – Juraj Sep 11 '19 at 9:14
2

The IPAddress class has a member function bool fromString(const char *address).

| improve this answer | |
  • This just gives me a result of 1 sadly – strange_esp Sep 11 '19 at 9:00
  • 1
    that is because you don't use it like, don't expect the return value to hold the desired ip, the method operates on a previously defined IPAddress object. – Sim Son Sep 13 '19 at 23:57
0

Please check the function into IPAddress.cpp file of line 70 :

bool IPAddress::fromString(const char *address) { 
// TODO: (IPv4) add support for "a", "a.b", "a.b.c" formats    
.... 
}

I think you need to drop the parentheses from the IPAdress ip() in your code, The code should be like below :

Void yourMethod (const char *addr, String ipStr){

IPAdreess ip;
bool i;

i = ip.fromString(ipStr);
    if (i) {
        for (int a = 0; a < 4; a++) {
            addr[a] = ip[a];
          ....
        }
    }
}

Hope this helps.

| improve this answer | |
0
IPAddress str2IP(String str) {

    IPAddress ret( getIpBlock(0,str),getIpBlock(1,str),getIpBlock(2,str),getIpBlock(3,str) );
    return ret;

}

int getIpBlock(int index, String str) {
    char separator = '.';
    int found = 0;
    int strIndex[] = {0, -1};
    int maxIndex = str.length()-1;
  
    for(int i=0; i<=maxIndex && found<=index; i++){
      if(str.charAt(i)==separator || i==maxIndex){
          found++;
          strIndex[0] = strIndex[1]+1;
          strIndex[1] = (i == maxIndex) ? i+1 : i;
      }
    }
    
    return found>index ? str.substring(strIndex[0], strIndex[1]).toInt() : 0;
}
| improve this answer | |
-1

You could do this:

String myip="192.168.1.2"
IPAddress local_IP;
bool x= local_IP.fromString(myip);
| improve this answer | |
  • 1
    This adds nothing new to answers and comments already given. – the busybee Jun 24 at 6:50
  • the guy above have shown the function but not how to use it :) – TuLiO Jun 26 at 22:40
  • If you mean the answer by abu-amed al-katiri, no. This answer even shows more context than yours. – the busybee Jun 27 at 17:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.