WiFi101 : Make a request inside the loop() method

Hello,

I'll try to make a GET request inside the loop() method with the WiFiClient.
Unfortunatly I never get the response from the server. The request seems ok because the connection to the server is ok.

I noticed in all the sketch exemple, the request was made inside the setup() method and the read of the response is made inside the loop() method. Why ?

Here is an exemple :

#include <SPI.h>
#include <WiFi101.h>

char ssid[] = "";
char pass[] = "";

int status = WL_IDLE_STATUS;

char server[] = "api.timezonedb.com";

WiFiClient client;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ;
  }

  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true);
  }

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    WiFi.end();
    status = WiFi.begin(ssid, pass);

    delay(5000);
  }
  Serial.println("Connected to wifi");
  printWiFiStatus();
}

void loop() {
  Serial.println("\nStarting connection to server...");
  if (client.connect("google.com", 80)) {
    Serial.println("connected to server");
    // Make a HTTP request:
    client.println("GET /search?q=test HTTP/1.1");
    client.println("Host: google.com");
    client.println("Connection: close");
    client.println();
  } else {
    Serial.println("Can't connect to server");
  }
  Serial.println("Wait response");
  delay(2000);

  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting from server.");
    client.stop();

    while (true);
  }

  delay(2000);
}

In this example :

  • The connection to the wifi AP is ok
  • The connection to server (google.com) is ok
  • I never had a response

Here is my env :

  • MacOS
  • Arduino IDE 1.8.10
  • Wifi101 library 0.16.0
  • Wifi firmware 19.6.1
  • MKR1000 Model B

I have the same problem on my 2 MKR1000.

Does anyone have any clue ?

Thanks.

don't connect if connected. now you connect again in every loop. but the while (client.available()) { requires more loops to complete