WiFiClient problem

Hi!

This code works the first time you start it, not the second time.
What can I do with it? ESP8266

void GetExternalIP() {
  WiFiClient client;
  if (!client.connect("api.ipify.org", 80)) {
    Serial.println("Failed to connect with 'api.ipify.org' !");
    }
    else {
    int timeout = millis() + 5000;
    client.print("GET /?format=text HTTP/1.1\r\nHost: api.ipify.org\r\n\r\n");
    while (client.available() == 0) {
      if (timeout - millis() < 0) {
        Serial.println(">>> Client Timeout !");
        client.stop();
        return;
        }
      }

    int nbytes;
    int ii;
    while ((nbytes = client.available()) > 0) {
    char* IpifyReply = (char*)malloc(nbytes +1);
    for (ii = 0; ii < nbytes; ii++) { 
      IpifyReply[ii] = client.read();
      }
    IpifyReply[ii] = 0;
    while (IpifyReply[--nbytes] != 10) { }
    PublIP = IpifyReply + nbytes + 1;
    client.stop();
    }
  }
} 

Please post a complete sketch so that we can try it for ourselves

51Kb

If you are worried about the size of the sketch (I'm not) then create a much smaller one that illustrates the problem

A description of what you mean by

would also be helpful. How do you start it the first time and how do you start it the second time, for instance. What do you mean by it not working ? What should it do ? What does it actually do ?


void setup() {
  GetExternalIP();
  Serial.println(PublIP);
  waitgpio[21].attach(ipup, updateip);
}

void updateip() {
  GetExternalIP();
}


11:15:41.586 -> Boot Start
11:15:41.586 -> 
11:15:45.633 -> Firmware: v1.82
11:15:45.633 -> Password: 
11:15:45.633 -> ChipID HEX: 16058613
11:15:45.633 -> EEprom HEX: 1FF2A8F1
11:15:45.633 -> SPIFFS: 1905 Kbytes free.
11:15:45.633 -> Free RAM: 36024kb
11:15:45.633 -> HTML header: 
11:15:46.141 -> . . . . . 
11:15:48.117 -> Mode: Client
11:15:48.117 -> SSID: PCSZERVIZ_70/505-66-20
11:15:48.150 -> WPA: 
11:15:48.150 -> Channel: 11
11:15:48.150 -> IP: 192.168.0.88
11:15:48.150 -> Gateway: 192.168.0.1
11:15:48.150 -> SubNet: 255.255.255.0
11:15:48.150 -> DNS: 1.1.1.1
11:15:48.150 -> STA MAC Address: A4:CF:12:F5:08:F5
11:15:48.150 -> HTTP PORT: 80
11:15:48.150 -> Hostname: EspOS
11:15:48.150 -> Public IP: 94.21.48.205
11:15:48.462 -> 
11:15:48.531 -> Boot: OK
11:15:48.531 -> 
11:16:08.562 -> Failed to connect with 'api.ipify.org' !
11:16:28.518 -> Failed to connect with 'api.ipify.org' !
11:16:48.560 -> Failed to connect with 'api.ipify.org' !
11:17:08.551 -> Failed to connect with 'api.ipify.org' !
11:17:28.517 -> Failed to connect with 'api.ipify.org' !

The code that you posted does not compile, let alone produce the output that you posted

I really would like to try it for myself

The code is not public. Sorry.

The Ticker library calls x time the function.
Querie public IP.

That is a shame because the problem is in the code that you have not posted

does the node need to connect/re-connect before each request? shouldn't it just connect once? is the server getting confused with multiple connections (i.e. ports)?

I realized that this function only works when started from setup.
Is there a solution?

what about something like this (untested)

WiFiClient client;

void GetExternalIP() {
    if (! client.connected()) {
        if (!client.connect("api.ipify.org", 80)) {
            Serial.println("Failed to connect with 'api.ipify.org' !");
            return;
        }
    }

    int timeout = millis() + 5000;
    client.print("GET /?format=text HTTP/1.1\r\nHost: api.ipify.org\r\n\r\n");
    while (client.available() == 0) {
        if (timeout - millis() < 0) {
            Serial.println(">>> Client Timeout !");
            client.stop();
            return;
        }
    }

    int nbytes;
    int ii;
    while ((nbytes = client.available()) > 0) {
        char* IpifyReply = (char*)malloc(nbytes +1);
        for (ii = 0; ii < nbytes; ii++) {
            IpifyReply[ii] = client.read();
        }
        IpifyReply[ii] = 0;
        while (IpifyReply[--nbytes] != 10) { }
        PublIP = IpifyReply + nbytes + 1;
        client.stop();
    }
}

looks like a memory leak. where is free()?

Unchanged.
Free ram: 30Kb