🛠 Récupérer une page web en HTTP simple
Objectives
Following the station connection from the previous tutorial, establish a raw TCP socket on port 80 using WiFiClient and manually write an HTTP/1.1 request (GET, Host header, Connection: close), then read the response byte by byte. This helps you understand what a "high-level" HTTP library does for you — and why HTTPS comes next.
Hardware
- 1 × Arduino MKR WiFi 1010 already capable of connecting to WiFi (scan/STA tutorial)
- Access to a public HTTP test site (e.g.,
example.com) - No certificate or key needed: everything is sent in plain text here, purely for learning purposes
Complete Code
#include <WiFiNINA.h>
const char* SSID = "votre-ssid";
const char* PASS = "votre-mot-de-passe";
WiFiClient client; // TCP simple (WiFiSSLClient pour TLS)
void setup {
Serial.begin(115200);
WiFi.begin(SSID, PASS);
while (WiFi.status != WL_CONNECTED) { delay(500); Serial.print("."); }
Serial.printf("\nIP: %s\n", WiFi.localIP.toString.c_str);
if (client.connect("example.com", 80)) {
client.println("GET / HTTP/1.1");
client.println("Host: example.com");
client.println("Connection: close");
client.println; // ligne vide = fin d'en-tetes
while (client.available)
Serial.write(client.read);
client.stop; // libere la connexion
} else {
Serial.println("Connexion TCP refusee");
}
}
void loop { delay(60000); } // une seule requette suffit
Expected Verification
The serial monitor dumps the complete HTML of the page (response headers + body). client.stop must be called to release the module's socket; otherwise, subsequent connections may remain blocked. On a captive network (hotel, corporate), the TCP connection succeeds but returns the authentication page instead.
Common Errors
- Forgetting the
Host:header: it is mandatory in HTTP/1.1, and virtual hosts return a 400 error without it. - Not reading the response before calling
client.stop: data is lost along with the socket. - A
while (client.available)loop that is too short: if the server sends data in fragments, also wait forclient.connectedwith a timeout, otherwise you truncate the body. - Targeting an HTTPS site with
WiFiClienton port 443: the handshake fails silently — this is the subject of the WiFiSSLClient tutorial. - Reusing the same
clientobject without callingstopbetween two requests: this exhausts the available sockets on the NINA module.
Other tutorials for this board
Code examples for this board
scan-wifi.inoArduinoconnexion-wpa2.inoArduinohttp-get.inoArduinoserveur-web-led.inoArduinohorloge-ntp.inoArduinoudp-diffusion.inoArduino