Concatenating two strings does not work ... strange

Hi,
i try to post the IP address of the sending arduino to my webserver.

To find out the IP I use the following code in setup():

// Print the IP address
  Serial.print("My IP is:  ");
  Serial.println(WiFi.localIP());

  IPAddress myAddr = WiFi.localIP();

  String ipaddress = String(myAddr[0])+ "." +String(myAddr[1]) + "." +String(myAddr[2]) + "." +String(myAddr[3]);
  Serial.println("Test:   " + ipaddress);

That works fine (in Setup).

If I do the same in loop() the ipaddress variable seems to be empty, although I don't touch it anywhere else in my code.

 Serial.println(ipaddress);
    String data = "param=Test&user="+ String(ipaddress) +"&value="+WiFi.RSSI()+"&p1=a&p2=b&p3=c";
    Serial.println(data);

Any idea ?

BR
Gawan

Variables declared inside a function are not normally accessible outside the function. Setup is a function. Variable IPAddress is "Out of scope".

Oh ... yes of course !!

Just found out that the variable was originally declared globally.
... and again in setup () :slight_smile:

You are uselessly pissing away resources using the String class. All that can be done using plain old char arrays and sprintf() (or other techniques, before any jumps all over me for recommending the sprintf() function).

Hi, I was not aware that String class is evil :slight_smile:

Can it be taken as a final truth, that char array is always better than String ?

Gawan:
Can it be taken as a final truth, that char array is always better than String ?

In the small memory of an Arduino, YES.

...R

Gawan:
Hi, I was not aware that String class is evil :slight_smile:

Can it be taken as a final truth, that char array is always better than String ?

And just in case any doubt remains, "yes" again. :slight_smile: