🛠 Serveur web embarqué pour piloter une LED

Arduino GIGA R1 WiFi · intermediaire · Schéma : IMG-giga-r1-wifi-intermediaire-6-serveur-web-led.svg — Navigateur web sur un téléphone se connectant à l'adresse IP de la GIGA R1 WiFi qui héberge une page avec boutons ON/OFF pour 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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials