serveur-web.ino

ESP32-C3 · File : serveur-web.ino · Language : Arduino · Use case : Allumer une LED depuis un navigateur sur le réseau local

The connected classic: the board serves an HTML page on the local network, two links toggle the LED, and an ADC shows live. No external infrastructure — the server runs inside the board.

Hardware required

An ESP32-C3 board (RISC-V 160 MHz, Wi-Fi + BLE 5) such as a DevKitM-1 or SuperMini, powered over USB (native on the C3). 2.4 GHz Wi-Fi access (not enterprise WPA3); optional potentiometer on an ADC.

Wiring

LED on GPIO2 through 330 Ω; potentiometer wiper on GPIO34 (input-only, 3.3 V max) between 3.3 V and GND.

How it works

WiFi.begin joins the access point, WebServer registers routes /, /on, /off and handleClient() serves requests as they come. The refresh tag keeps the page live every 5 s. In the Arduino IDE (esp32 package), select « ESP32C3 Dev Module », enable « USB CDC On Boot » if needed, then Upload. GPIO9 is often the BOOT button; keep GPIO8 free at boot.

Going further

Add a password (server.authenticate), or publish the same values to an MQTT broker instead of displaying them.

serveur-web.ino

// Serveur web embarque : page de controle LED GPIO2 + etat analogique.
#include <WiFi.h>
#include <WebServer.h>

const char *SSID = "votre-wifi";
const char *CLE  = "votre-cle";
const int LED = 2;

WebServer serveur(80);

void page() {
  String h = "<h1>ESP32 locale</h1>"
    "<p><a href=on>LED ON</a> | <a href=off>LED OFF</a></p>"
    "<p>GPIO34 : " + String(analogRead(34)) + "</p>"
    "<meta http-equiv=refresh content=5>";
  serveur.send(200, "text/html", h);
}

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(SSID, CLE);
  while (WiFi.status() != WL_CONNECTED) { delay(300); Serial.print("."); }
  Serial.printf("\nhttp://%s/\n", WiFi.localIP().toString().c_str());
  serveur.on("/", page);
  serveur.on("/on", []() { digitalWrite(LED, HIGH); page(); });
  serveur.on("/off", []() { digitalWrite(LED, LOW); page(); });
  serveur.begin();
}

void loop() { serveur.handleClient(); }

← Back to the board