I want to send an HTTP request every few seconds to a web server and read/parse the response but I can't seem to get it to work. I tested the HTTP request with a web browser so I'm sure it's a client-side issue. I'm using the Arduino Giga with the WiFi library. Thank you.
if (client.connect(server, 80)) {
client.println("GET /PRESSURE");
client.println("Host: IP");
client.println("Connection: close");
client.println();
} else {
Serial.println("error");
}
while (client.available()) {
char c = client.read();
Serial.write(c);
}
delay(5000);
Many examples that manually perform HTTP lie and say HTTP/1.1, and then request Connection: close. Seems simpler to be honest: GET /pressure HTTP/1.0, which does not support (the 1.1 default) keep-alive. With 1.0, you also don't need a Host (required with 1.1); unless you actually need one because the same IP is serving multiple hosts. (Also, paths are generally all-lowercase, which are easier to type and allow seeing more of the path in the browser's address bar. No rule against all-uppercase, but it's tacky.)
A second potential problem is that you have only a while-available loop. That means if there is any delay in the response, at the beginning or in the middle, it stops trying. You may end up with nothing. It's more robust to use a nested while-connected, if-available; since you expect the connection to close when the response is completed.