🛠 Client HTTP : récupérer une page en GET
Objectives
Connect the GIGA R1 WiFi to a WPA2 network in station mode, then send a raw HTTP GET request using WiFiClient and read the response. You will mainly learn the reflex specific to this board: always fully drain the TCP response body, because on the mbed core, unread TCP bodies poison subsequent connections (issue ArduinoCore-mbed #937).
Hardware
- Arduino GIGA R1 WiFi board with a screwed-on u.FL antenna
- A 2.4 GHz WPA2 WiFi access point (no 5 GHz)
- USB-C cable, IDE with the "Arduino Giga R1" core
Complete code
// GIGA R1 WiFi — client HTTP GET (HTTP simple, sans TLS : voir le tuto HTTPS)
#include <WiFi.h>
const char SSID[] = "your-ssid";
const char PASS[] = "your-pass"; // WPA2
const char SERVEUR[] = "example.org";
const int PORT = 80;
void connexion {
Serial.print("Connexion a "); Serial.print(SSID);
WiFi.begin(SSID, PASS);
while (WiFi.status != WL_CONNECTED) { Serial.print("."); delay(200); }
Serial.print(" OK — IP "); Serial.println(WiFi.localIP);
}
void setup {
Serial.begin(115200);
while (!Serial) {}
connexion;
WiFiClient client;
if (!client.connect(SERVEUR, PORT)) { Serial.println("Connexion TCP echouee"); return; }
client.print("GET / HTTP/1.0\r\n");
client.print("Host: "); client.println(SERVEUR);
client.print("Connection: close\r\n\r\n");
unsigned long debut = millis;
while (client.connected && millis - debut < 10000) {
while (client.available) {
Serial.write(client.read); // on TOUT lit : ne jamais laisser des octets
} // en attente (issue #937 sur le core mbed)
}
client.stop; // purge + fermeture propre
Serial.println("\n--- fin de la reponse ---");
}
void loop {
if (WiFi.status != WL_CONNECTED) WiFi.reconnect;
delay(5000);
}
Expected verification
After the line "Connecting to ... OK — IP 192.168.x.x", the serial monitor dumps the HTML returned by the server (the content of the example page), followed by "--- end of response ---". Run the sketch several times: each connection must succeed — this is exactly where fully draining the stream prevents the blocking issues from issue #937.
Common errors
- Unread response body: reading the headers and then calling
client.stopwhile leaving the body pending eventually blocks subsequent connections on the mbed core. Always read everything. - Implicit
Connection: keep-alive: in HTTP/1.0 without aConnection: closeheader, the socket remains open and the loop waits; close it properly withclient.stop. - Lingering internal DNS: if your network returns a fake IP via DNS, test with the direct IP in
client.connect. - WiFi 5 GHz: the 1DX module is strictly 2.4 GHz; a dual-band single SSID can be confusing — test with a 2.4 GHz guest SSID.
- Wrong core compiled: the code above works on both cores, but libraries do not mix; recompile if you change cores.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton.inoArduinorespiration.inoArduinoanalogique.inoArduinodac-onde.inoArduinooled-i2c.inoArduino