Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By schufti
#27214 Hi,
to me it seems to be in your variable definitions:
Code: Select all  String ServerToConnectTo;
  String PageToGet;
  ServerToConnectTo = URLtoGet.substring(0, URLtoGet.indexOf("/"));
  PageToGet = URLtoGet.substring(URLtoGet.indexOf("/"));
.
.
if (client.connect(ServerToConnectTo , 80))

"client.connect()" needs a "const char* ServerToConnectTo" definition for the host
in the .h there is no definition matching ".connect(string, int)" only ".connect(const char*, int)"

Code: Select all  virtual int connect(IPAddress ip, uint16_t port);
  virtual int connect(const char *host, uint16_t port);
User avatar
By martinayotte
#27236 The String API allows to convert to "char *" using c_str() :

Code: Select allif (client.connect(ServerToConnectTo.cstr() , 80))


BTW, in the code above, we can see that the URL still have HOST hard-coded to "Host: api.ipify.org", that needs to be modified, and the "?format=json" removed ...
User avatar
By Mmiscool
#27268 So now the function is just spitting out numbers.


Code: Select allString FetchWebUrl(String URLtoGet)
{
  String str;
  String ServerToConnectTo;
  String PageToGet;
  ServerToConnectTo = URLtoGet.substring(0, URLtoGet.indexOf("/"));
  PageToGet = URLtoGet.substring(URLtoGet.indexOf("/"));

  Serial.println(ServerToConnectTo);
  Serial.println(PageToGet);

  WiFiClient client;
  if (client.connect(ServerToConnectTo.c_str() , 80))
  {
    client.print(String("GET " + PageToGet + " HTTP/1.1\r\nHost: " + ServerToConnectTo + "\r\n\r\n"));
    delay(100);
    str = "";
    while (client.available())
    {
      str += String(client.read());
      delay(0);
    }
    return str;
  }
  return "";
}
User avatar
By martinayotte
#27273 The client.read() is returning a "int", but String class constructor for "int" think that you wish to display numbers.
If you wish to display the character corresponding the this ASCII code, you need to cast it to "const char".

Code: Select allstr += String((const char)client.read());