Gawan
December 3, 2015, 10:27pm
1
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".
Gawan
December 3, 2015, 10:38pm
3
Oh ... yes of course !!
Just found out that the variable was originally declared globally.
... and again in setup ()
system
December 3, 2015, 11:44pm
4
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).
Gawan
December 4, 2015, 7:55am
5
Hi, I was not aware that String class is evil
Can it be taken as a final truth, that char array is always better than String ?
Robin2
December 4, 2015, 8:17am
6
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
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.