I know it's bad to use Strings due to the memory problems that can occur. I've tried to remove all instances of strings from my code. But I'm not sure about this line:
http.getString().toCharArray(ipAddressBuffer, lengthOfIPAddress);
Is calling toCharArray()
enough to prevent memory problems? Or is there a string still being created on the heap?
If it's still created on the heap, how can I modify the code so it only deals with cstrings?
Here's my code:
void setIPAddress(char* ipAddressBuffer){
HTTPClient http;
http.begin("http://bot.whatismyipaddress.com/"); //Only allowed to call once per 5 mins.
http.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
int httpCode = http.GET();
if (httpCode > 0) {
http.getString().toCharArray(ipAddressBuffer, lengthOfIPAddress);
}
else {
Serial.println("Error on HTTP request");
}
http.end();
}
I'm using the HTTPClient.h library btw.
HttpClient
objects, not forHTTPClient