🛠 Serveur web embarqué pour piloter une LED
Objectives
Transform the GIGA R1 WiFi into an embedded web server: the board listens on port 80 of the local network and serves a mini HTML page with two links that turn an LED on and off. This is the local variant of the official SimpleWebServerWiFi examples provided with the core's WiFi library. You will learn how to parse an HTTP GET request and return a correct response.
Hardware
- Arduino GIGA R1 WiFi board + u.FL antenna
- 2.4 GHz WPA2 WiFi (the board and the phone must be on the same network)
- 1 LED + 330 Ω resistor on pin 5 (reminder: 8 mA max per pin)
Complete code
// GIGA R1 WiFi — mini serveur web LED (inspiré de SimpleWebServerWiFi du core)
#include <WiFi.h>
const char SSID[] = "your-ssid";
const char PASS[] = "your-pass";
const int LED_PIN = 5; // LED + 330 ohms vers GND
WiFiServer serveur(80);
void setup {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
WiFi.begin(SSID, PASS);
while (WiFi.status != WL_CONNECTED) { Serial.print("."); delay(200); }
Serial.print("Ouvez http://"); Serial.println(WiFi.localIP);
serveur.begin;
}
void loop {
if (WiFi.status != WL_CONNECTED) { WiFi.reconnect; delay(2000); return; }
WiFiClient client = serveur.available; // attend une connexion entrante
if (!client) return;
String requete = "";
unsigned long debut = millis;
while (client.connected && millis - debut < 3000) {
while (client.available) {
char c = client.read;
if (requete.length < 200) requete += c; // on ne garde que la 1re ligne utile
}
if (requete.indexOf('\r\n\r\n') >= 0) break; // fin des en-tetes recues
}
if (requete.startsWith("GET /on")) { digitalWrite(LED_PIN, HIGH); Serial.println("LED ON"); }
if (requete.startsWith("GET /off")) { digitalWrite(LED_PIN, LOW); Serial.println("LED OFF"); }
client.print("HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n");
client.print("Connection: close\r\n\r\n");
client.print("<html><body style='font-family:sans-serif'>");
client.print("<h2>GIGA R1 WiFi — LED broche 5</h2>");
client.print("<a href='/on'><button>OUI</button></a> ");
client.print("<a href='/off'><button>NON</button></a></body></html>");
client.stop; // ferme la socket proprement
}
Expected verification
The monitor displays "Open http://192.168.x.x". From a phone connected to the same WiFi, this URL displays a page with two buttons; a press prints "LED ON" / "LED OFF" on the board side and actually turns the LED on/off. The browser button reloads the page on each press, which is normal for this design without JavaScript.
Common errors
- Inaccessible from the network: some routers block communications between clients (client isolation / AP isolation) — disable it or test from the programming PC.
- Malformed HTTP response: a missing
\r\nbetween headers and body, or forgetting the final empty line, results in a blank page or a browser that keeps waiting. - Socket left open: without
client.stop, the browser spins freely and the server saturates after a few requests; also remember to clear the received request (reflex #937 on the mbed core). - One client at a time: this sequential design serves only one connection at a time; a phone opening 6 parallel connections (favicon, etc.) may seem to block — it is temporary.
- Changing IP address: note the IP displayed at startup, or reserve it in the router's DHCP lease.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton.inoArduinorespiration.inoArduinoanalogique.inoArduinodac-onde.inoArduinooled-i2c.inoArduino