🛠 Client HTTP : récupérer une page en GET

Arduino GIGA R1 WiFi · intermediaire · Schéma : IMG-giga-r1-wifi-intermediaire-4-client-http-get.svg — GIGA R1 WiFi connectée au routeur puis à un serveur web distant, requête GET partant vers le cloud et réponse HTML revenant vers le moniteur série

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials